A condition selection if statements
- The order of execution: one by one run;
- Select execution:
- Code has a branch: will be performed when conditions are met;
- Two or more branches: branch will execute wherein a satisfies the condition;
- The loop executes:
snippets (loop) 0, 1 or more to perform back and forth;
Choose to perform:
# single branch if statement:
if the test condition; then
the condition is true branch code
fi
# dual branch if statement:
if determination condition; then the condition is true branch code else condition is false branch code fi Example: passing the parameter a user name to the script, when the user does not exist, add the; ! # / bin / the bash IF [$ # -LT-. 1] ; the then ehco "AT Lease One username" Fi IF ID $. 1 &> / dev / dull; then # judge sentences is an order, not the order itself but rather an expression of time must be added [], when it comes to itself as the command does not need to add [] echo "$ 1 exits the User" the else useradd $ 1 [$ -? 0 EQ] && echo "$. 1" | $. 1 & --stdin the passwd> / dev / Dell echo "User $. 1 WAS Finished" Fi
Two, read
Used to read the input values assigned to one or more shell variable
-p prompt to be displayed
-s muting input password generally used
-n designation input character length N N
-d 'character' end symbol
-t N TIMEOUT is N seconds
echo a b c > file1
read x y z <file1
echo $x
a
echo $y
b
echo $z
c
也可以
read x y z <<< "i j k" #!/bin/bash read -p "please input your name: " name echo your name is $name #!/bin/bash read -p "please input your name: " name read -s -p "please input your passwd: " passwd echo your name is $name echo your passwd is $passwd
Experimental: chickens and rabbits with cage; an input total number of the total number of heads and feet, is calculated chickens, rabbits few respectively.
#!/bin/bash
read -p "please input heads num: " H
read -p "please input feets num: " F
C=$[(4*H-F)/2]
R=$[(F-2*H)/2]
echo "the chicken num is $C"
echo "the rabbir num is $R"