Single-branch if statement:
if test condition
Then
Code Branch
Fi
Two-branch if statement:
if test condition; Then
Branch that is executed when the condition is true
Else
Branch that is executed when the condition is false
Fi
Example: Pass a user name to the script through a parameter, add it when this user is not saved;
#!/bin/bash#if [$#-lt 1]; Then echo "At least one username." Exit 2fiif grep "^$1\>"/etc/passwd &>/dev/null; Then echo "User $ exists." Else Useradd echo $ | passwd--stdin $ &>/dev/null echo "ADD user $ finished." Fi
Exercise 1: Given two numbers by command-line arguments, output a larger number;
#!/bin/bash#if [$#-lt 2]; Then echo "the integers." Exit 2fiif [$1-ge]; Then echo "Max Number: $." else echo "Max number: $." Fiecho "Max number: $max."
This article is from the "Wang Liming" blog, make sure to keep this source http://afterdawn.blog.51cto.com/7503144/1915972
Shell script Programming selection execution if statement