Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have test.sh which has multiple return condition and test1.sh just echo statement.When i run test2.sh my logic should run test.sh process the file i.e "File successfully" and call test1.sh script .It should not run the test1.sh script when other else condition was executed.i.e "File not successfully", "Input file doesn't exists in directory"

The problem i am facing is when it is executing other condition like "File not successfully", "Input file doesn't exists in directory" it is not retuning "1" as specified exit code but in turn returning 0 i.e from the OS means job was successful. So i am getting "0" from test.sh for all the different condition so test1 .sh is getting called irrespective if the file processed failed etc.Pls advice with the return code

test.sh
FILES=/export/home/input.txt
cat $FILES | nawk -F '|' '{print $1 "|" $2 "|" }' $f > output.unl
if [[ -f $FILES ]]; 
then if [[ $? -eq 0 ]]; then
 echo "File successfully" 
else
 echo "File not successfully"
 exit 1 
fi
 else 
echo "Input file doesn't exists in directory" exit 1 fi

========================================================================

test1.sh
 echo "Input file exists in directory"

test2.sh

echo "In test2 script"
./test.sh
echo "return code" $?
if [[ $? -eq  0 ]]; then
 echo "inside"
./test1.sh
fi

share|improve this question
    
test.sh doesn't appear to be properly formatted (see last line). Also, how is $FILES initialized? Further, testing $? in test.sh seems unnecessary since you're already testing for the existence of $FILES using -f. –  ajk Feb 20 '14 at 2:57
    
Your test.sh is syntactically invalid - the nawk program is missing a closing single quote and the last line won't work unless you put at least fi on a separate line. If you indeed have exit 1 on the same line as your echo statement, then it would simply have become another output string, which would explain why the exit code is not set. –  mklement0 Feb 20 '14 at 3:46
    
This sort of problem often goes away when you avoid explicitly testing $?. Instead of testing $?, just check the actual call. In other words, just write if ./test.sh; then echo inside; ...; fi –  William Pursell Feb 20 '14 at 5:33
    
The sample code you have shown is clearly not your actual code (do you really "echo ... exit 1 fi" which leaves the if unclosed and merely echoes exit 1 rather than executing an exit?), so any answer is purely speculative, but I think perhaps your exit is being called from within a subshell. –  William Pursell Feb 20 '14 at 5:37

1 Answer 1

You're overwriting $? when you use it in echo - after that, it contains the exit code of echo itself. Store it in a variable to avoid this.

echo "In test2 script"
./test.sh
testresult=$?
echo "return code" $testresult
if [[ $testresult -eq  0 ]]; then
  echo "inside"
  ./test1.sh
fi

Edited to add: it's hard to tell what you want from test.sh as the code you pasted is incomplete and doesn't even run. It looks like you meant the cat to be inside the if, because otherwise it errors when the input file is missing, and your $? test does nothing. So I rearranged it like this:

FILES=input.txt

if [[ -f $FILES ]]; then
  cat $FILES | awk -F '|' '/bad/{ exit 1 }'
  if [[ $? -eq 0 ]]; then
    echo "File processed successfully"
  else
    echo "File processing failed"
    exit 1
  fi
else
  echo "Input file doesn't exist in directory"
  exit 1
fi

I've changed the awk script to demonstrate the conditions all work: now, if I put the word bad in input.txt you'll see the "File processing failed" message, otherwise you see success; remove the file and you'll see the input file doesn't exist message.

share|improve this answer
    
I tried that option but it didn't worked.It is still returning "0" for all the else condition –  user3326101 Feb 20 '14 at 3:10
    
I presume you mean the else conditions in test.sh. I've updated the answer to fix that one too, but it's hard to tell what you were trying to do there. –  bazzargh Feb 20 '14 at 3:48
    
@hi All -I have updated the script to understand what i am doing but that doen't make much sense.If cat command is success it is going to return "0" all the time .My question if if it is going to any of the else block why it is still retuning "0". –  user3326101 Feb 20 '14 at 4:00
    
Your latest update still makes no sense. Now theres a variable $f that's never set, and your code still doesn't even run, because of syntax errors due to missing newlines. But if I insert newlines in the obvious places, your code does not return 0 from the else blocks, it returns 1. Either the code in your test2.sh is still wrong (as I said), or there's more code you've not posted which is also wrong. –  bazzargh Feb 20 '14 at 4:35
    
@bazzargh-You are not getting the context.i am saying still returning the "O" in all the else block –  user3326101 Feb 20 '14 at 4:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.