一、条件选择、判断
1、条件选择if
1)用法格式
if 判断条件 1 ;
then
条件为真的分支代码
elif 判断条件 2 ;
then
条件为真的分支代码
elif 判断条件 3 ;
then
if 判断条件 1 ;
then
条件为真的分支代码
elif 判断条件 2 ;
then
条件为真的分支代码
elif 判断条件 3 ;
then
#判断年纪
#!/bin/bash
read -p 'Please input
your age: '
age
if [[ $age =~
[^0-9] ]]
;then
echo
'please input a
int'
exit
10
elif [ $age -ge
150
];then
echo 'your
age is
wrong'
exit
20
elif [ $age -gt
18
];then
echo 'good
good work,day day
up'
else
echo 'good
good study,day day
up'
fi
#判断分数
#!/bin/bash
read -p 'Please input
your score: '
score
if [[ $score =~
[^0-9] ]]
;then
echo
'please input a
int'
exit
10
elif [ $score -gt
100
];then
echo 'Your
score is
wrong'
exit
20
elif [ $score -ge
85
];then
echo 'Your
score is very
good'
elif [ $score -ge
60
];then
echo 'Your
score is
soso'
else
echo 'You
are loser'
fi
case $name
in;
PART1)
cmd
;;
PART2)
cmd
;;
*)
cmd
;;
esac
注意:case 支持glob
风格的通配符:
*:
任意长度任意字符
?: 任意单个字符
[]
:指定范围内的任意单个字符
a|b: a
或b
#判断yes or no
#!/bin/bash
read -p 'Please input
yes or no: '
anw
case $anw
in
[yY][eE][sS]|[yY])
echo
yes
;;
[nN][oO]|[nN])
echo
no
;;
*)
echo
false
;;
esac
for name in
列表
;do
循环体
done
for (( exp1; exp2;
exp3 ))
;do
cmd
done
#求出(1+2+...+n)的总和
sum=0