Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using OR in shell script

Tags:

shell

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?

like image 309
shantanuo Avatar asked Nov 19 '10 08:11

shantanuo


People also ask

What is $() in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

Can I use && in shell script?

The behavior of && in a shell script It is important to note that && is used not only for checking for tests/conditions, but also to run two or more commands. Thus, the commands after && will only run if the commands before && return as "True".

What is $@ and $* in shell script?

"$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...). So basically, $# is a number of arguments given when your script was executed. $* is a string containing all arguments. For example, $1 is the first argument and so on.

What is $? == 0 in shell script?

$? is the exit status of the most recently-executed command; by convention, 0 means success and anything else indicates failure. That line is testing whether the grep command succeeded. The grep manpage states: The exit status is 0 if selected lines are found, and 1 if not found.


1 Answers

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 
like image 127
DarkDust Avatar answered Oct 19 '22 15:10

DarkDust