Linux if statement and or

How to do a logical OR operation for integer comparison in shell scripting?

I am trying to do a simple condition check, but it doesn’t seem to work. If $# is equal to 0 or is greater than 1 then say hello. I have tried the following syntax with no success:

if [ "$#" == 0 -o "$#" > 1 ] ; then echo "hello" fi if [ "$#" == 0 ] || [ "$#" > 1 ] ; then echo "hello" fi 

For information on Bash comparisons, please see my answers to previous questions here and here. For additional information regarding brackets, double brackets and double parentheses, see my answer here.

8 Answers 8

#!/bin/bash if [ "$#" -eq 0 ] || [ "$#" -gt 1 ] ; then echo "hello" fi 

I’m not sure if this is different in other shells but if you wish to use , you need to put them inside double parenthesis like so:

I’m not sure if it’s different in another shell but in bash, if you wish to use > or

@Doug: It’s not that unix doesn’t use them, it’s that bash and all the other shells I know use them for input/output redirection.

#!/bin/sh argc=$# echo $argc if [ $argc -eq 0 -o $argc -eq 1 ]; then echo "foo" else echo "bar" fi 

I don’t think sh supports «==». Use » mt24″>

Greater than is -gt, see my answer. The official documentation is in man test as jbremnant has pointed out.

Just as an fyi for others, depending on your shell, most support == (now a days) as an comparison operator, however most of the time it is the same as the = operator. I would imagine that if you are using -gt and -ne for comparisons that it would be better practice to use -eq for == or = to keep your scripting style consistent. Happy Bashing 🙂

If you are using the bash exit code status $? as variable, it’s better to do this:

if [ $? -eq 4 -o $? -eq 8 ] ; then echo ". " fi 
if [ $? -eq 4 ] || [ $? -eq 8 ] ; then 

The left part of the OR alters the $? variable, so the right part of the OR doesn’t have the original $? value.

$# returns the number of arguments passed to the script. Useful for checking correct usage. It doesn’t seem like the asker is using exit codes.

Sometimes you need to use double brackets, otherwise you get an error like too many arguments

if [[ $OUTMERGE == *"fatal"* ]] || [[ $OUTMERGE == *"Aborting"* ]] then fi 

This is useful because it’s the only thing here that I see showing an example of non-integer comparisons, which are slightly different in syntax.

If [[ $input -gt number || $input -lt number ]] then echo . else echo . fi exit 

have you tried something like this:

if [ $# -eq 0 ] || [ $# -gt 1 ] then echo "$#" fi 

#
($#) Expands to the number of positional parameters in decimal.

Therefore, $# will always be either 0 or a bigger integer.

Читайте также:  Linux flash file systems

So if you want to do something whenever $# is either 0 or bigger than 1, you just have to check if $# is or is not 1 :

[ $# -eq 1 ] && echo "1 positional param" || echo "0 or more than 1" 

@premek.v thanks for the comment. My answer was very badly explained, I just edited it in the hope that it sheds more light to the topic.

And in Bash

 line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1` vpid=`ps -ef| grep wowzaDataSync | grep -v grep | awk ''` echo "-------->"$ if [ -z $line1 ] && [ ! -z $vpid ] then echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: "Process Is Working Fine" else echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: "Prcess Hanging Due To Exception With PID :"$ fi 
line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1` vpid=`ps -ef| grep wowzaDataSync | grep -v grep | awk ''` echo "-------->"$ if [ -z $line1 ] || [ ! -z $vpid ] then echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: "Process Is Working Fine" else echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: "Prcess Hanging Due To Exception With PID :"$ fi 

Источник

Bash: If, Else If, Else Examples

In this Linux Hint tutorial we will teach Bash scripting conditionals using IF, ELIF and ELSE commands. The IF condition is the most basic way to take conditional action in a bash scripts so its important to have a firm grasp on its syntax and options to be productive. When using IF conditions in Bash, you can optionally introduce ELIF conditions that will provide different actions depending on which of a group of conditions is TRUE. Additionally the ELSE condition can be optionally included, which will provide code to execute if none of the conditions in the group is met.

Specific Examples Shown in this Article Include:

General Syntax of Bash IF ELIF ELSE

Below is the general syntax of IF ELIF and ELSE in bash:

if CONDITION-TO-TEST; then
CODE-TO-EXECUTE- 1
elif NEXT-CONDITION-TO-TEST; then
CODE-TO-EXECUTE- 2
elif NEXT-CONDITION-TO-TEST; then
CODE-TO-EXECUTE- 2
else
CODE-TO-EXECUTE- 2
fi

Note: In the above general syntax that you can include zero, one or multiple ELIF conditions in the code block and the ELSE condition is optional.

Construction of the actual conditions has many options because the syntax of bash has so many different features and options, but a very basic template for your bash scripts would be to use the [[ double brack syntax as shown in the general syntax example and specific examples below.

if [ [ CONDITION-TO-TEST ] ] ; then
CODE-TO-EXECUTE- 1
elif [ [ NEXT-CONDITION-TO-TEST ] ] ; then
CODE-TO-EXECUTE- 2
elif [ [ NEXT-CONDITION-TO-TEST ] ] ; then
CODE-TO-EXECUTE- 2
else
CODE-TO-EXECUTE- 2
fi

Note also: That depending on your code formatting aesthetics you may like the then on the same line of the conditions or you may opt to put it in the next line as shown below:

if [ [ CONDITION-TO-TEST ] ]
then
CODE-TO-EXECUTE- 1
elif [ [ NEXT-CONDITION-TO-TEST ] ]
then
CODE-TO-EXECUTE- 2
elif [ [ NEXT-CONDITION-TO-TEST ] ]
then
CODE-TO-EXECUTE- 2
else
CODE-TO-EXECUTE- 2
fi

Now lets show specific examples of the basic variations of the IF ELIF and ELSE conditions in working examples below.

Example 1: If statement in bash on string equality

#!/bin/bash
read -p «Enter a match word: » USER_INPUT
if [ [ $USER_INPUT == «hello» ] ] ; then
echo «Hello to you as well»
fi

Читайте также:  Монтируем ntfs раздел linux

In this example above we use the basic IF condition with a single test of string equality and then print some output in case the condition is true based on input from the user. Note, you can place the THEN statement on the next line if you prefer that style. See the example output below:

linuxhint @ :~$ bash t1.sh
Enter a match word: hello
Hello to you as well
linuxhint @ :~$ bash t1.sh
Enter a match word: bye
linuxhint @ :~$

Example 2: If statement in bash on number equality

#!/bin/bash
read -p «Enter a number: » USER_INPUT
if [ [ $USER_INPUT -eq 7 ] ] ; then
echo «7 is a lucky number»
fi

In this example above, again we do a simple IF condition but in this case we demonstrate how to do a test comparison on numeric values. You can see the output below with both the matching and non matching conditions:

linuxhint @ :~$ bash t2.sh
Enter a number: 7
7 is a lucky number
linuxhint @ :~$ bash t2.sh
Enter a number: 8
linuxhint @ :~$

Example 3: If statement in bash on less than a number

In addition to -eq for equality you can also try -lt -le -gt -ge test conditions for less than, less than or equal, greater than or greater then or equal respectively. Below is a similar example with less then operator -lt:

#!/bin/bash
read -p «Enter a number: » USER_INPUT
if [ [ $USER_INPUT -lt 100 ] ] ; then
echo «Your entry is less than one hundred»
fi

Output for this script will be able to test if the input is less than 100 and run specific code on that condition:

linuxhint @ :~$ bash t3.sh
Enter a number: 99
Your entry is less than one hundred
linuxhint @ :~$ bash t3.sh
Enter a number: 101
linuxhint @ :~$

Example 4: If statement in bash with logical AND operator

This example will show how to combine multiple conditions with a logical AND condition. You can also use logical OR conditions in your IF statement constructs. Logical AND in bash code is written with double ampersand &&. Below is an example of logical AND in a bash IF condition:

#!/bin/bash
read -p «Enter a number: » USER_INPUT
if [ [ $USER_INPUT -gt 9 && $USER_INPUT -lt 100 ] ] ; then
echo «Your entry is two digits»
fi

We will test this with various outputs to prove that only if both the first and the second condition are true the output will be printed:

linuxhint @ :~$ bash t4.sh
Enter a number: 22
Your entry is two digits
linuxhint @ :~$ bash t4.sh
Enter a number: 8
linuxhint @ :~$ bash t4.sh
Enter a number: 100
linuxhint @ :~$ bash t4.sh
Enter a number: 99
Your entry is two digits

Example 5: If statement in bash with logical OR operator

Logical OR in bash scripts is written with double vertical bars ||. Let’s try the previous example again with a slight variation to test out an example of the logical OR operator:

#!/bin/bash
read -p «Enter a number: » USER_INPUT
if [ [ $USER_INPUT -lt 10 || $USER_INPUT -ge 100 ] ] ; then
echo «Your entry is NOT two digits»
fi

In this example the logical OR operator combines two conditions and only one of them or both can be true for the condition to be overall true and the code to execute. We also demonstrate the -ge operator for greater or equal numeric comparison in the above example. The script is working as expected in the output below:

Читайте также:  Вывести содержимое всех файлов линукс

linuxhint @ :~$ bash t5.sh
Enter a number: 3
Your entry is NOT two digits
linuxhint @ :~$ bash t5.sh
Enter a number: 10
linuxhint @ :~$ bash t5.sh
Enter a number: 99
linuxhint @ :~$ bash t5.sh
Enter a number: 100
Your entry is NOT two digits
linuxhint @ :~$ bash t5.sh
Enter a number: 101
Your entry is NOT two digits

Example 6: elif condition in bash

We will now provide an example for multiple if conditions in the code block with ELIF. You can have zero, one, or multipe ELIF conditions in a code block. For this example we will provide two ELIF conditions as a demonstration, but you can use any number of ELIF blocks in practice.

#!/bin/bash
read -p «Enter a number: » USER_INPUT
if [ [ $USER_INPUT -lt 0 ] ] ; then
echo «Not a valid positive number input»
elif [ [ $USER_INPUT -gt 0 && $USER_INPUT -lt 10 ] ] ; then
echo «Valid 1 digit number entered»
elif [ [ $USER_INPUT -gt 9 && $USER_INPUT -lt 100 ] ] ; then
echo «Valid 2 digit number entered»
fi

We will test this code with a variety of numbers to demonstrate which if or ELIF condition is triggered:

linuxhint @ :~$ bash t6.sh
Enter a number: -1
Not a valid positive number input
linuxhint @ :~$ bash t6.sh
Enter a number: 1
Valid 1 digit number entered
linuxhint @ :~$ bash t6.sh
Enter a number: 14
Valid 2 digit number entered
linuxhint @ :~$ bash t6.sh
Enter a number: 101
linuxhint @ :~$

Example 7: If Else Statement in Bash Scripting

We will now demonstrate the ELSE condition. The ELSE condition is used in the case where the IF condition or all the ELIF conditions do not exist then the ELSE condition will be used and the code in the ELSE condition will be executed. Let’s add an example of ELSE condition to the previous example but to be clear ELIF is not required to leverage the ELSE condition. The ELSE condition can be used with a IF condition and no ELIF condition.

#!/bin/bash
read -p «Enter a number: » USER_INPUT
if [ [ $USER_INPUT -lt 0 ] ] ; then
echo «Not a valid positive number input»
elif [ [ $USER_INPUT -gt 0 && $USER_INPUT -lt 10 ] ] ; then
echo «Valid 1 digit number entered»
elif [ [ $USER_INPUT -gt 9 && $USER_INPUT -lt 100 ] ] ; then
echo «Valid 2 digit number entered»
else
echo «A valid 2 digit number was not entered»
fi

Let’s test the example code with a simple test case that should fall into the ELSE condition:

linuxhint @ :~$ bash t7.sh
Enter a number: 199
A valid 2 digit number was not entered
linuxhint @ :~$

CONCLUSION

We have seen how to use IF, ELIF and ELSE conditions to form conditional execution in many combinations in bash scripts and bash programs. For next steps you can study test conditions and the variations and varieties available for testing a condition. You can also consider the bash case statement as an alternate for conditional logic in bash scripts. Finally the GNU Bash Reference Manual is a good source of truth when looking for exact syntax details.

About the author

Linux Wolfman

Linux Wolfman is interested in Operating Systems, File Systems, Databases and Analytics and always watching for new technologies and trends. Reach me by tweeting to @linuxhint and ask for the Wolfman.

Источник

Оцените статью
Adblock
detector