Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX Shell if... Else statement


May 23, 2021 UNIX Getting started


Table of contents


Shell judgment statement

When you write a shell script, there may be a situation where you need to select one of the two paths. So you need to use conditional statements to make sure your program makes the right decisions and does the right thing.

UNIX Shell supports conditional statements that are based on different conditions and are used to perform different operations. Here, we'll cover two decision statements:

  • if...... Else statement
  • case...... The esac statement

if...... Else statement:

if...... Else statements are very useful decision statements that can be used to select an option from a given set of options.

Unix Shell supports the following forms of if... The statement of else:

  • if... fi statement
  • if... e lse... fi statement
  • if... e lif... e lse... fi statement

Most if statements use relationship operators to examine relationships, as discussed in the previous chapter.

case...... The esac statement

You can use multiple if... T he elif statement executes a multiple branch. However, this is not always the best solution, especially when all branches depend on the value of a single variable.

Unix Shell supports case... e sac statement, which can handle this situation more precisely than repeating if... Elif statements are more effective.

case...esac statement, detailed as follows:

  • case... esac statement

Unix Shell's case... T he esac statement is very similar to switch... c ase statement, switch... Case statements are implemented in other programming languages, such as C or C+ and PERL.