Bash passing a variable from function - bash

I'm trying to find out whether there is a Screen running or not with a function. To make it more simple ive written a little test script:
#! /bin/bash
function status()
{
if [ $a==1 ]
then
echo 1
else
echo 0
fi
}
a=1
echo $(status)
a=0
echo $(status)
status
if [ $(status)==0 ]
then
echo "Success"
else
echo "Fail"
fi
The Output is: 1;1;Sucess
But it should be: 1;0;Sucess
What am I doing wrong?
thanks alot, chrys

You need spaces around ==
so: if [ $a==1 ] should be:
if [ "$a" == 1 ]
and
if [ $(status) == 0 ]

Try this:
Remove spaces around == , included quotes and removed a call for status
#! /bin/bash
function status()
{
if [ $a == '1' ]
then
echo 1
else
echo 0
fi
}
a=1
echo $(status)
a=0
echo $(status)
if [ $(status) == '0' ]
then
echo "Success"
else
echo "Fail"
fi

Related

If statement returning error in shell script? [duplicate]

I would like to write in one line this:
if [$SERVICESTATEID$ -eq 2]; then echo "CRITICAL"; else echo "OK"; fi
So to do a test in my shell I did:
if [2 -eq 3]; then echo "CRITICAL"; else echo "OK"; fi
The result is
-bash: [2: command not found
OK
So it doesn't work.
Space -- the final frontier. This works:
if [ $SERVICESTATEID -eq 2 ]; then echo "CRITICAL"; else echo "OK"; fi
Note spaces after [ and before ] -- [ is a command name! And I removed an extra $ at the end of $SERVICESTATEID.
An alternative is to spell out test. Then you don't need the final ], which is what I prefer:
if test $SERVICESTATEID -eq 2; then echo "CRITICAL"; else echo "OK"; fi
Write like this, space is required before and after [ and ] in shell
if [ 2 -eq 3 ]; then echo "CRITICAL"; else echo "OK"; fi
Shorter format.
( [ 2 -eq 3 ] && echo "CRITICAL" ) || echo "OK"
Regex pattern type numbers : 10,12.1,+3.33,-1,0004,-48.9
Oneliner attacks again!
( [ `echo $number 2>/dev/null | grep -E "^[ ]*(\+|\-){0,1}[0-9]+(\.[0-9]+)?$"` ] && echo "NUMBER" ) || echo "NOT NUMBER"

Complicated if-then-else statement

So, I simplified this code. Every time it runs, else or $msg4 is always executed. How do I change it so it only does else if the $nick part doesn't match?
if [ "$who" = "$nick1" ]
then echo $msg1
fi
if [ "$who" = "$nick2" ]
then echo $msg2
fi
if [ "$who" = "$nick3" ]
then echo $msg3
else $msg4
fi
Here you can read how Bash if statements work: https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#Conditional-Constructs
There you can see there is an elif which you should use to chain multiple if - else things together so that the final else is only executed if none of the if statements match. Result:
if [ "$who" = "$nick1" ]
then
echo $msg1
elif [ "$who" = "$nick2" ]
then
echo $msg2
elif [ "$who" = "$nick3" ]
then
echo $msg3
else
echo $msg4
fi
You can also write the then on the same line as if if you add a ; before then:
if [ "$who" = "$nick1" ]; then
echo $msg1
elif [ "$who" = "$nick2" ]; then
echo $msg2
elif [ "$who" = "$nick3" ]; then
echo $msg3
else
echo $msg4
fi
This is often easier to read.
Use case .. esac
case "$who" in
"$nick1") echo "$msg1";;
"$nick2") echo "$msg2";;
"$nick3") echo "$msg3";;
*) echo "$msg4";;
esac

bash string not matching in if clause

I am running into a strange issue. In the below piece of code the echo statement works fine and in success.txt I get b4 running=false with RUNNING:false which means $RUNNING = false.
But it is not going into the if block.
echo "b4 running=false with RUNNING:"$RUNNING >> /tmp/success.txt
if [[ $RUNNING == "false" ]]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
I also tried
if [[ $RUNNING == false ]]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
if [ "$RUNNING" == "false" ]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
if [ "$RUNNING" == false]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
if [ "$RUNNING" == "false" ]; then
echo "in running=false" >> /tmp/success.txt
exit 2
fi
None of these is working. I am sure I am missing something very small here.
When I try
RUNNING=false
if [[ $RUNNING == false ]]; then
echo 1
fi
if [ "$RUNNING" == "false" ]; then
echo 2
fi
if [ "$RUNNING" == false]; then
echo 3
fi
if [ "$RUNNING" == "false" ]; then
echo 4
fi
I get
1
2
/tmp/a.sh: line 10: [: missing `]'
4
Fixing the third one from
if [ "$RUNNING" == false]; then
to
if [ "$RUNNING" == false ]; then
Note the additional space between false and ].
Now I get
1
2
3
4
So all four are valid, if $RUNNING is false. The difference might be a trailing new line character or some white space around false/$RUNNING, if the value is captured from the output of some command.
See Conditional Constructs and Bash Conditional Expressions for more details.
have you tried the following:
if [ $RUNNING == "false" ]; then
echo "test"
fi
That seems to work for me.
Cheers

How to write if else in one line in shell?

I would like to write in one line this:
if [$SERVICESTATEID$ -eq 2]; then echo "CRITICAL"; else echo "OK"; fi
So to do a test in my shell I did:
if [2 -eq 3]; then echo "CRITICAL"; else echo "OK"; fi
The result is
-bash: [2: command not found
OK
So it doesn't work.
Space -- the final frontier. This works:
if [ $SERVICESTATEID -eq 2 ]; then echo "CRITICAL"; else echo "OK"; fi
Note spaces after [ and before ] -- [ is a command name! And I removed an extra $ at the end of $SERVICESTATEID.
An alternative is to spell out test. Then you don't need the final ], which is what I prefer:
if test $SERVICESTATEID -eq 2; then echo "CRITICAL"; else echo "OK"; fi
Write like this, space is required before and after [ and ] in shell
if [ 2 -eq 3 ]; then echo "CRITICAL"; else echo "OK"; fi
Shorter format.
( [ 2 -eq 3 ] && echo "CRITICAL" ) || echo "OK"
Regex pattern type numbers : 10,12.1,+3.33,-1,0004,-48.9
Oneliner attacks again!
( [ `echo $number 2>/dev/null | grep -E "^[ ]*(\+|\-){0,1}[0-9]+(\.[0-9]+)?$"` ] && echo "NUMBER" ) || echo "NOT NUMBER"

bash : Illegal number

When I run this bash script :
if [ [$EUID -ne 0] ]; then
echo "This script must be run as root" 1>&2
exit 1
else
printf " whathever "
exit 0
fi
I have this error :
./myScript: 15: [: Illegal number: [
Do you see any problem ?
You have syntax error in your if condition, use this if condition:
if [ "$EUID" -ne 0 ];
OR using [[ and ]]
if [[ "$EUID" -ne 0 ]];
You have syntax error in your if condition, use this if condition:
if [ "$EUID" -ne 0 ];
OR using [[ and ]]
if [[ "$EUID" -ne 0 ]];
If you use the KSH88+/Bash 3+ internal instruction [[, it's not necessary to use doubles quotes around the variables operands :
[ ~/test]$ [[ $var2 = "string with spaces" ]] && echo "OK" || echo "KO"
OK
Instead of the external command test or his fork [ :
[ ~/test]$ [ $var2 = "string with spaces" ] && echo "OK" || echo "KO"
bash: [: too many arguments
KO
[ ~/test]$ [ "$var2" = "string with spaces" ] && echo "OK" || echo "KO"
OK
Of course, you also have to choose the operators according to the type of operands :
[ ~/test]$ var1="01"
[ ~/test]$ [ "$var1" = "1" ] && echo "OK" || echo "KO"
KO
[ ~/test]$ [ "$var1" -eq "1" ] && echo "OK" || echo "KO"
OK
two suggestions apart from what everyone else has pointed out already.
rather than doing else [bunch of code because we are root] fi, just replace the else with fi. once you've tested for the failure condition you are concerned about and taken appropriate action, no need to continue to be within the body of the conditional.
$EUID is a bashism, if you would like to make this portable to shells such as ksh, replacing it with:
if [ $(id -u) -ne 0 ]; then echo "ur not root bro"; exit 1; fi
would be a good way to do it.
using
sudo bash shell_script.sh
instead of
sudo sh shell_script.sh
solved in my case.

Resources