• Bash if.. else Statement
  • Original author: Linuxize
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Hoarfroster
  • Proofread by: Zenblo, flying-yogurt, Lsvih

In this tutorial, we’ll drill down into the basics of the if statement in Bash and walk you through how to use the if statement in Shell scripts.

Decision making, one of the most fundamental elements of a computer program. As with other programming languages, this is done by using if, if.. Else, an if.. elif.. Else and nested if statements that we can execute within Bash based on certain conditions.

ifstatements

If conditional statements in Bash have different implementation formats. For example, the most basic form:

if TEST-COMMAND
then
  STATEMENTS
fi
Copy the code

The if statement is led by the if keyword, followed by a conditional expression, followed by the then keyword, and terminated by the FI keyword.

If test-command is true, STATEMENTS will be executed accordingly. If false, STATEMENTS are ignored.

In most cases, keeping your code with good indentation and space lines is a good practice to make your code more readable and structured. Most people use indentation with 4 Spaces or 2 Spaces.

Let’s look at the following code to check if a given number is greater than 10:

#! /bin/zsh

echo -n "Enter a number:"
read VAR

if [[ $VAR -gt 10 ]]
then
  echo "This variable is greater than 10."
fi
Copy the code

Save the above code and execute it on the command line:

Note that you may need to grant permissions to this code before running it.
# chmod +x test.sh
# ./test.sh

bash test.sh
Copy the code

This script will ask you to enter a number, for example, if you enter 15, then test-command [[$var-gt 10]] equals true, and the echo statement in the then clause will be executed.

So the output of the above code is:

This variable is greater than 10.Copy the code

if.. elsestatements

Bash the if.. The else statement looks like this:

if TEST-COMMAND
then
  STATEMENTS1
else
  STATEMENTS2
fi
Copy the code

If test-command is true, STATEMENT1 is executed; If false, STATEMENT2 will be executed. For each if statement, there can be only one else statement.

Let’s add an else statement to the previous example:

#! /bin/bash echo -n "Enter a number:" read VAR if [[$VAR -gt 10]] then echo "This variable is greater than 10. Else echo "This variable is less than or equal to 10." fiCopy the code

If you execute the code above and enter a number, the script will output different information about the size relationship between the number you enter and 10.

if.. elif.. elsestatements

Bash if.. elif.. The else statement looks like this:

if TEST-COMMAND1
then
  STATEMENTS1
elif TEST-COMMAND2
then
  STATEMENTS2
else
  STATEMENTS3
fi
Copy the code

If the test-command1 condition is true, STATEMENTS1 will be executed. If false, proceed as follows: If the test-command2 condition is true, STATEMENTS2 will be executed. Otherwise, STATEMENTS3 will be executed.

You can have one or more elif statements in a single statement, and else statements are optional.

The conditional statements are executed sequentially. Once one of the conditional clauses is true, the rest of the statements are not executed, and the program jumps to the end of the if paragraph to execute the next piece of code.

Let’s add the elif statement to the previous example:

#! /bin/bash echo -n "Enter a number:" read VAR if [[$VAR -gt 10]] then echo "This variable is greater than 10. Elif [[$VAR -eq 10]] then echo" Else echo "This variable is less than 10." fiCopy the code

nestedifstatements

Bash allows one or more if statements to be nested within an if statement.

The following script will ask you to enter three numbers and print the largest of them:

#! /bin/bash echo -n "Enter a number:" read VAR1 echo -n "Enter a number:" read VAR2 echo -n "Enter a number: "Read VAR3 if [[$VAR1 -ge $VAR2]] && [[$VAR1 -ge $VAR3]] then echo "$VAR1 is the largest number." Elif [[$VAR2 -ge $VAR1]] && [[$VAR2 -ge $VAR3]] then echo "$VAR2 is the largest number." Else echo "$VAR3 is the largest number." fiCopy the code

And the output looks like this:

Seven is the largest number.Copy the code

In general, it is more efficient to use case statements than if statements.

Multiple conditions

The logical operators OR AND allow you to use multiple conditions in an if statement.

Here’s another way to get the largest number of the three numbers. In this version, we use the logical AND (&&) operator instead of nested if statements.

#! /bin/bash echo -n "Enter a number:" read VAR1 echo -n "Enter a number:" read VAR2 echo -n "Enter a number: "Read VAR3 if [[$VAR1 - ge $VAR2]] && [[$VAR1 - ge $VAR3]] then echo" $VAR1 is one of the biggest digital elif "[[$VAR2 - ge $VAR1]] && [[ $VAR2 -ge $VAR3]] then echo "$VAR2 = 1 "else echo "$VAR3 = 1Copy the code

testoperation

In Bash, the test statement could look something like this:

test EXPRESSION [ EXPRESSION ] [[ EXPRESSION ]]

To make our script portable, it is best to use older versions that are compatible with all POSIX shells […] Statements. Most modern systems that use Bash, Zsh, and Ksh as their default shells support the [[…]] (double bracketed) new syntax.

To negate the test expression, use the logic NOT (!) Operator. Always use single or double quotation marks when comparing strings to avoid word splitting and garbled characters.

Here are some of the most commonly used operators:

  • n VAR– ifVARIs true if the length of the.
  • z VAR– ifVARNull, true.
  • STRING1 = STRING2– ifSTRING1STRING2Equal, true.
  • STRING1 ! = STRING2– ifSTRING1STRING2If it is not equal, it is true.
  • INTEGER1 -eq INTEGER2– ifINTEGER1INTEGER2Equal is true.
  • INTEGER1 -gt INTEGER2– ifINTEGER1Is greater thanINTEGER2, is true.
  • INTEGER1 -lt INTEGER2– ifINTEGER1Less thanINTEGER2, is true.
  • INTEGER1 -ge INTEGER2– ifINTEGER1Equal to or greater thanINTEGER2, is true.
  • INTEGER1 -le INTEGER2– ifINTEGER1Equal to or less thanINTEGER2, is true.
  • h FILE– ifFILETrue if it exists and is a symbolic link.
  • r FILE– ifFILETrue if it exists and is readable.
  • w FILE– ifFILETrue if it exists and is writable.
  • x FILE– ifFILETrue if it exists and can be executed.
  • d FILE– ifFILETrue if it exists and is a directory.
  • e FILE– ifFILETrue if it exists and is a file regardless of its type (whether node, directory, or socket).
  • f FILE– ifFILEThere areIs true if it is a regular file (not a directory or device, etc.).

conclusion

If, if… The else and if.. elif.. The else statement allows us to control the execution of logical statements in Bash scripts based on different judgment criteria.

If you have any questions or feedback, please feel free to comment.

  • This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.