LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   am not getting any result when am using 'if', without getting result for below script (https://www.linuxquestions.org/questions/linux-general-1/am-not-getting-any-result-when-am-using-if-without-getting-result-for-below-script-4175433836/)

alavala53 10-24-2012 07:36 AM

am not getting any result when am using 'if', without getting result for below script
 
when i am executing below script usisg if , not getting any result for below script.without 'if' getting result is 1 for below script.
if anybody know, plese post me ans.
str1="GOOd boy"
str2="Bad boy"
if test "$str1" = "$str2"
then
echo $?
fi

MensaWater 10-24-2012 08:14 AM

The return code for the test is being sucked in by if so doing the echo of return code in your then is meaningless.

You'd have to do something like:
Code:

str1="GOOd boy"
str2="Bad boy"
if test "$str1" = "$str2"
then echo 0
    exit 0
else echo 1
    exit 1
fi

The echo is literal (0 = true, 1 = false) rather than being something you got from the if itself. The "then" occurs when it is "true" so doing the literal echo there is OK. You can then set the return code (exit status) within the then or within the else so the overall script exits with the return code you want. If you run the above script then run "echo $?" at command line immediately it will show you "1".

chrism01 10-24-2012 08:27 PM

Some useful links
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Habitual 10-25-2012 06:00 PM

http://mywiki.wooledge.org/BashFAQ/031


All times are GMT -5. The time now is 05:39 PM.