I am trying to stop processing a shell script with an IF condition.
output7=${output7##*[[:space:]]}
if [[ $output4 -gt 0 && $output5 -gt 0 && $output6 -gt 0 && $output7 -gt 0 ]]
then echo 'Success'
else echo 'Failure'|| exit 0
fi
echo 'Process Completed'
It is printing the following message but the next statement is also being executed. The output is:
Failure
Process Completed
Can anyone pls tell me why it is not exiting the script.
The problem here is that you are saying that "if echo 'Failure' fails, then exit 0", which is not what you want. You'll want
echo 'Failure'
exit 1
instead. Idiomatic Bash style for the whole section would be:
if [[ $output4 -gt 0 ]] && [[ $output5 -gt 0 ]] && [[ $output6 -gt 0 ]] && [[ $output7 -gt 0 ]]
then
echo 'Success'
else
echo 'Failure'
exit 1
fi
Related
I have a bash script that runs many conditional statements, but when I run the script, it only hits the following line: if [ $? -eq 0 ]
touch /tmp/converted.json
tool test --json --all-projects | tee >> /tmp/converted.json
ret=$?
set -o pipefail
#no vulns
if ((ret == 0)); then
echo "Hooray!"
#found vulns
elif ((ret == 1)); then
then
#check single project
if [[ $firstchar == "{" ]]
then
echo "SINGLE PROJECT"
elif [[ $firstchar == "[" ]]
then
echo "MULTIPROJECT"
else
echo "didnt get a single or multiproject"
fi
#error with your command
elif ((ret == 2)); then
then
echo "An error occurred"
#no manifest
elif ((ret == 3)); then
then
echo "Ensure your repo has a proper required manifest file"
else
echo "Something else occurred"
fi
Thus, why does this continue to hit if((ret==0)) condition?
Thanks
$? has the exit status of the last command executed. The last command at elif [ $? -eq 1 ] was [ executed at if [ $? -eq 0 ].
if [ $? -eq 0 ] # executes `[`
then
: dont care
elif [ $? -eq 1 ] # $? here has a nonzero the exit status of `[`
Just save the exit status to a variable before calling any command right after executing the command you are interested in. In bash you may use arithmetic expansion ((...)).
some_command
ret=$?
if ((ret == 0)); then
: dont care
elif ((ret == 1)); then
Without having to type out another block of if/else statements, how can I accomplish this within the string?
I want to avoid having to set the wording based on the boolean. The output from the script takes in cronjob_status as a boolean, which in turns does things based on the true/false value. I need to perserve the boolean that's being passed in via stdin for cronjob_status and I want to print the correct word.
if [ "${cronjob_status}" == True ]; then
cronjob_status_display = "enable"
else
cronjob_status_display = "disable"
fi
if [ $? -eq 0 ]
then
echo "Successfully ${cronjob_status_display} the cronjob"
else
echo "Could not create cronjob" >&2
fi
Add a short circuit evaluation, &&:
if [ $? -eq 0 ] && [ "${cronjob_status}" == True ]; then
echo "OK"
else
echo "NOT OK" >&2
fi
If you use the bash builtin [[, then this can also be written as :
if [[ $? -eq 0 && ${cronjob_status} == True ]]; then
echo "OK"
else
echo "NOT OK" >&2
fi
As a side note, you don't necessarily need to check the exit status with $?, you can run the command and check at once after that, for example if the command is e.g. /foo/bar, you could do:
if /foo/bar &>/dev/null && [[ ${cronjob_status} == True ]]; then
echo "OK"
else
echo "NOT OK" >&2
fi
You can use:
[[ $cronjob_status == True ]] &&
cronjob_status_display=enable || cronjob_status_display=disable
A one liner:
[[ $? -eq 0 ]] && echo "Successfully ${cronjob_status} the cronjob" || echo "Could not create cronjob" >&2
I have file "sleepdaemon" which launches python program as service
I run this script:
sudo /etc/init.d/sleepdaemon start
but when I check the state $?=0 always
this is how i check
if [ "$?" -eq 0 ]; then
echo "Process is running"
fi
if [ "$?" -eq 1 ]; then
echo "Process is not running"
fi
if [ "$?" -eq 2 ]; then
echo "Process is not running"
fi
What's the problem ?
The value of $? gets reset after every test, so you either need to save it in a variable before testing, or use a case ... esac statement.
sudo returns the result of the command, or 1 if it can't run it. ? is set for EVERY command:
if (( $? == 0 )); then
echo "Process is running"
elif (( $? == 1 )); then
echo "Process is not running" >&2
elif (( $? == 2 )); then
echo "Process is not running" >&2
else
echo "Unknown exit code $?" >&2
fi
Use (( ... )) for arithmetic comparisons - it is easier and safer. You should always have a default long-stop, and you should report errors to stderr, not stdout.
If $? is always zero, then check the documentation for sleepdaemon (I tried, but can't find a return code documented anywhere).
I'm trying to write a script that will check two error flags, and in case one flag (or both) are changed it'll echo-- error happened. My script:
my_error_flag=0
my_error_flag_o=0
do something.....
if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then
echo "$my_error_flag"
else
echo "no flag"
fi
Basically, it should be, something along:
if ((a=1 or b=2) or (a=1 and b=2))
then
display error
else
no error
fi
The error I get is:
line 26: conditional binary operator expected
line 26: syntax error near `]'
line 26: `if [[ "$my_error_flag"=="1" || "$my_error_flag_o"=="2" ] || [ "$my_error_flag"="1" && "$my_error_flag_o"="2" ]]; then'
Are my brackets messed up?
Use -a (for and) and -o (for or) operations.
tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
Update
Actually you could still use && and || with the -eq operation. So your script would be like this:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ] || ([ $my_error_flag -eq 1 ] && [ $my_error_flag_o -eq 2 ]); then
echo "$my_error_flag"
else
echo "no flag"
fi
Although in your case you can discard the last two expressions and just stick with one or operation like this:
my_error_flag=1
my_error_flag_o=1
if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ]; then
echo "$my_error_flag"
else
echo "no flag"
fi
You can use either [[ or (( keyword. When you use [[ keyword, you have to use string operators such as -eq, -lt. I think, (( is most preferred for arithmetic, because you can directly use operators such as ==, < and >.
Using [[ operator
a=$1
b=$2
if [[ a -eq 1 || b -eq 2 ]] || [[ a -eq 3 && b -eq 4 ]]
then
echo "Error"
else
echo "No Error"
fi
Using (( operator
a=$1
b=$2
if (( a == 1 || b == 2 )) || (( a == 3 && b == 4 ))
then
echo "Error"
else
echo "No Error"
fi
Do not use -a or -o operators Since it is not Portable.
Please try following
if ([ $dateR -ge 234 ] && [ $dateR -lt 238 ]) || ([ $dateR -ge 834 ] && [ $dateR -lt 838 ]) || ([ $dateR -ge 1434 ] && [ $dateR -lt 1438 ]) || ([ $dateR -ge 2034 ] && [ $dateR -lt 2038 ]) ;
then
echo "WORKING"
else
echo "Out of range!"
You can get some inspiration by reading an entrypoint.sh script written by the contributors from MySQL that checks whether the specified variables were set.
As the script shows, you can pipe them with -a, e.g.:
if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
...
fi
I was modifying an script didn't know how to write more than one condition in an if statement. I want to connect the two condition with an AND.
if [ envoi1 -eq 2 ];then
if [ envoi2 -eq 0 ];then
echo 'Ahora mismo.'
envoi = 1
fi
else
if [ envoi2 -eq 1 ];then
if [ envoi1 -eq 1 ];then
echo 'Situacion Normal.'
envoi = 1
fi
else
echo 'Raruno'
envoi=`expr $envoi1 + envoi2`
fi
fi
Now i use nested if to do the same but the code it's not so clear for me.
try this:
if [ $envoi1 -eq 2 ] && [ $envoi2 -eq 0 ] ; then
envoi = 1
fi
In bash, you can use [[ as follows:
if [[ $envoi2 -eq 1 && $envoi1 -eq 1 ]]; then
echo "Situacion Normal."
envoi=1
fi
However, [[ is not POSIX and will not work if you are using the /bin/sh shell. So if portability is desired use:
if [ $envoi2 -eq 1 -a $envoi1 -eq 1 ]; then
echo "Situacion Normal."
envoi=1
fi
Also note that when assigning variables you should not have any spaces on either side of the =.