My shell script looks something like this...
if [[ $uptime -lt 0 ]];then
some code
fi
if [[ $questions -lt 1 ]];then
some code
fi
if [[ $slow -gt 10 ]];then
some code
fi
How do I use OR and have a single if clause?
You should be able to use || or -o I think as follows:
if [ $uptime -lt 0 ] || [ $questions -lt 1 ] || [ $slow -gt 10 ]; then
some code
fi
if [ $uptime -lt 0 -o $questions -lt 1 -o $slow -gt 10 ] ; then
some code
fi
See man test for available syntax and options. The [ operator is just shorthand for test, so the above code is equivalent to:
if test $uptime -lt 0 -o $questions -lt 1 -o $slow -gt 10 ; then
some code
fi
Related
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 years ago.
I'm trying to do test automation with a bash script using if then else statements but I'm running into a few errors. For one, when I try to execute it I'm doing something wrong with the variable assignment with j and k, because it tells me that the command j and the command k aren't found when I try to execute. How do you correctly create variables?
The most confusing thing though is when I try to execute the script I get an error telling me I have an unexpected token near fi, and then it just says 'fi'. What am I doing wrong here?
#!/bin/bash
j = 0
k = 0
echo Test1:
echo -ne "0\nIn\nUG\n" | /u/cgi_web/Tuition/cost
echo Test2:
echo -ne "0\nOut\nUG\n" | /u/cgi_web/Tuition/cost
echo Test3:
echo -ne "0\nIn\nGR\n" | /u/cgi_web/Tuition/cost
echo Test4:
echo -ne "0\nOut\nGR\n" | /u/cgi_web/Tuition/cost
for i in {1..17}
do
echo Test$((i+4)):
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
$j = 1
echo -ne "$i\nIn\nUG\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 0 ] then
$k = 1
echo -ne "$i\nIn\nGR\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 1 ] && [ "$k" -eq 1 ] then
$j = 0
echo -ne "$i\nOut\nUG\n" | /u/cgi_web/Tuition/cost
elif[ "$j" -eq 0 ] && [ "$k" -eq 1 ] then
$k = 0
echo -ne "$i\nOut\nGR\n" | /u/cgi_web/Tuition/cost
fi
done
EDIT: I figure out the variable issue with j and k, I had to remove the spaces in the statement.
Bash if statements require a semi-colon before the then:
if [ condition ] || [ condition ]; then
# code
elif [ condition ] && [ condition ]; then
# code
fi
For example.
To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others.
To start the variable assignment:
j = 0
you can't have spaces in between, so it would be:
j=0
Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement
if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then
becomes
if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then
or instead of a semicolon you can have a new line between the bracket, so it would become
if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then
I am trying to test if a number is in the interval [1;100] here is what I did:
var=10
if [ $["$var" -gt "1" ] -a $["$var" -lt "100"] ] ; then
echo "yes"
else
echo "no"
fi
however when I run the script I get the error message:
./yourscript:line 2 10 -gt 1:error syntax in expression ,any ideas why?
delete unnecessaries and use &&:
var=10
if [ $var -gt 1 ] && [ $var -lt 100 ] ; then #or with -a if [ $var -gt 1 -a $var -lt 100 ] ;
echo "yes"
else
echo "no"
fi
Here is my script:
age=119
if [[$age -gt 99 ]]; then
age_3digits=$age
elif [[$age -gt 9]]; then
age_3digits=0$age
else
age_3digits=00$age
fi
z_grid=${age_3digits}Ma.grd
echo $z_grip
output: 00119Ma.grd
how come?? I am new to bash, thanks so much
You need a space after [[ and before ]]. Change to:
if [[ $age -gt 99 ]]; then
age_3digits=$age
elif [[ $age -gt 9 ]]; then
age_3digits=0$age
else
age_3digits=00$age
fi
It's also better to use arithmetic expressions because it makes your code more readable, like this:
if (( age > 99 )); then
age_3digits=$age
elif (( age > 9 )); then
age_3digits=0$age
else
age_3digits=00$age
fi
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 =.