If statementSyntax 1
If condition
then command (set)
fi
The condition here is to use the test statement or the bracketed statement (the previous article has been summed up)
Note If statement must terminate with FI
Practice:
#if test
if ["-lt"] # "13" before a space, "13" after a space.
then
echo "Yes less then"
else
echo "NO"
fi
Syntax 2
If condition 1
then
command 1
elif condition 2
then
command 2
Else
command 3
fi
#!/bin/bash
#if Test
#this is a comment line
echo "Enter your filename:"
read myfile
if [-e $myf Ile]
then
if [-S $myfile];then
echo $myfile exist and size greater than zero '
else
Echo ' $MYF Ile exist but size is zero "
fi
Else
echo" File no exist "
fi
Case Multiple selection statement
Case multiple Choice Statement format:
Case variable/Expression in
mode 1)
command 1
;;
Mode 2)
command 2
;;
Esac
The case must be followed by a word in; each pattern must end with a closing parenthesis. The value can be a variable or a constant. After a match finds that the value is in accordance with a pattern, all subsequent commands begin to execute until; Pattern Matching * denotes any character;? [..] (Note: Only two points) represent any character in a class or range
#!/bin/bash
#case Select
echo-n "Enter a number from 1 to 3:"
read num
box $num in
1)
echo "Y OU select 1 "
;;
2)
echo "you select 2"
;;
3)
echo "you select 3"
;;
y| Y)
echo "You select Y"
;;
*)
echo "' BaseName $ ': This is not between 1 and 3" >&2
exit;
;; Esac
All patterns have precedence, and the order-preference pattern that appears can be written as an expression representing a range such as y| Y cannot write multiple patterns that perform the same operation, like C switch-case, if multiple patterns perform the same operation, combine them with | to indicate that one of them is matched to perform the corresponding branch operation.