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
test.sh
doesn't appear to be properly formatted (see last line). Also, how is$FILES
initialized? Further, testing$?
intest.sh
seems unnecessary since you're already testing for the existence of$FILES
using-f
. – ajk Feb 20 '14 at 2:57test.sh
is syntactically invalid - thenawk
program is missing a closing single quote and the last line won't work unless you put at leastfi
on a separate line. If you indeed haveexit 1
on the same line as yourecho
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$?
. Instead of testing$?
, just check the actual call. In other words, just writeif ./test.sh; then echo inside; ...; fi
– William Pursell Feb 20 '14 at 5:33if
unclosed and merely echoesexit 1
rather than executing anexit
?), so any answer is purely speculative, but I think perhaps yourexit
is being called from within a subshell. – William Pursell Feb 20 '14 at 5:37