#if判断结构
if expression;then
command
fi
#if/else判断结构
if expression;then
command
else
command
fi
#嵌套if
if expression1;then
command1
elif expression2;then
command2
elif expression3;then
command3
fi
#case判断
case VAR in
var1) command1;;
var2) command2;;
var3) command3;;
*) command;;
esac
2.循环
#带列表的for循环
for VARIABLE in (list)
do
commadn
done
#不带列表的for循环
for VARIABLE
doprin
command
done
#类c的for循环
for ((expression;expression2;expression3))
do
command
done
#while循环
while expression
do
command
done
#util循环
until expression
do
command
donef
#打印乘法口诀表
1 #!/bin/sh
2 #fun print 9*9
3 for ((i=0;i<=9;i++))
4 do
5 for ((j=1;j<=9;j++))
6 do
7 if [[ ${i} -ge ${j} ]]; then
8 let mul=${i}*${j}
9 echo -n "${i}*${j}=${mul} "
10 else
11 break
12 fi
13 done
14 echo
15 done
3.函数
function FUNCTION_NAME(){
command1
command2
}
FUNCTION_NAME(){
command1
command2
}
1 #!/bin/sh
2 #usage : *.sh arg1
3 #fun : print arg1 file colmns
4 arguJudge(){
5 echo "Usage: *.sh fileName "
6 return 1
7 }
8
9 if [[ $# -lt 1 ]]; then
10 arguJudge
11 fi
12
13 FILE=$1
14 countLine(){
15 local i=0
16 while read line
17 do
18 let ++i
19 done < ${FILE}
#求和
#!/bin/bash
TOTAL=0
until [ $# -eq 0 ]
do
let "TOTAL=TOTAL+$1"
shift
done
echo "$TOTAL"
4.加载函数库
函数库11
#函数库 libbase.sh
#!/bin/bash
_checkFileExit_B_L(){
if [ -f $1 ]; then
echo "Files: $1 exits"
else
echo "Files: $1 not exits"
fi
}
#test libbase.sh
#!/bin/bash
source ./Leolib/libbase.sh
_checkFileExit_B_L /etc/passwd
_checkFileExit_B_L /var/log/message