How to write if else in one line in shell? - 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"

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 passing a variable from function

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

shell script: integer expression expected

#!/bin/bash
if [$# -ne 1];
then
echo "/root/script.sh a|b"
else if [$1 ='a'];
then
echo "b"
else if [$1 ='b']; then
echo "a"
else
echo "/root/script.sh a|b"
fi
I'm getting below error while run above script in Linux.
bar.sh: line 2: [: S#: integer expression expected
a
Could you please help to remove this error?
if [$# -ne 1];
[ and ] requires spacing. Example:
if [ $# -ne 1 ];
And else if should be elif
#!/bin/bash
if [ "$#" -ne 1 ];
then
echo "/root/script.sh a|b"
elif [ "$1" ='a' ];
then
echo "b"
elif [ "$1" ='b' ]; then
echo "a"
else
echo "/root/script.sh a|b"
fi
Do not forget to quote variables. It is not every time necessary, but recommended.
Question: Why do i have -1?
Bash doesn't allow else if. Instead, use elif.
Also, you need spacing within your [...] expression.
#!/bin/bash
if [ $# -ne 1 ];
then
echo "/root/script.sh a|b"
elif [ $1 ='a' ];
then
echo "b"
elif [ $1 ='b' ]; then
echo "a"
else
echo "/root/script.sh a|b"
fi

search a string in Shell script variable

I have a shell script variable
$a = "Hello i am pass"
now i want to search for "pass" in variable $a.
if ["$a" == "pass"]; then
echo "`Success`"
else
echo "`fail`"
fi
Please give me shell script for searching pass keyword to use in above code.
Try with this,
#!/bin/bash
a="Hello i am pass";
if [ `echo $a | grep -c "pass" ` -gt 0 ]
then
echo "Success"
else
echo "Fail";
fi
flag=`echo $a|awk '{print match($0,"pass")}'`;
if [ $flag -gt 0 ];then
echo "Success";
else
echo "fail";
fi
Here is a more compact way to write this:
echo "$a" | grep "pass" && echo "Found." || echo "Not found."
You can use braces to put in multiple instructions instead of a simple echo:
echo "$a" | grep "pass" && {echo "Found.";exit 0} || {echo "Not found.";exit 1}
today i used extended choice parameter and using execute shell for the string match.
if someone will find same problem anyone can use this
if [ `echo $test| grep -c "abc" ` -gt 0 ]
then
echo "Success"
else
echo "Fail";
fi
Try this
a="Hello I am Pass";
a1="Hello, Passed my First Attempt"
a2="Passed in First Attempt"
if [[ ${a,,} =~ 'pass' ]]; then echo 'Success'; else echo 'First Attempt in Learning'; fi
Short command:
OPC="test"
PROD="This is a test"
[[ ${PROD,,} =~ $OPC ]] && echo -n 1 || echo -n 0
The answer will be "1" when find "OPC" in "PROD".

Resources