#!/bin/bash
#测试成绩
echo -n "please input a score:"
read SCORE
if [ ${SCORE} -lt 60 ]
then
echo "不及格"
elif [ ${SCORE} -eq 60 ]
then
echo "刚刚及格啊"
elif [ ${SCORE} -gt 60 -a ${SCORE} -lt 80 ]
then
echo "合格"
elif [ "$SCORE" -ge 80 -a "$SCORE" -lt 90 ]
then
echo "良好"
elif [ $SCORE -ge 90 ]
then
echo "优秀"
fi
1.2case
注意格式 不能给少结束符号esac 和;;
#!/bin/bash
#测试季节
echo -n "please input a season:"
read season
case $season in
spring) echo "fresh";;
summer) echo "hot";;
autumn) echo "bleak";;
winter) echo "cold";;
*) echo "it is ok";;
esac
二、循环
2.1for
#!/bin/bash
#测试季节
num="1 2 3 4 5 5"
name="lily tom mon rich phibe chiller"
echo "第一个"
for var in $num
do
echo $var
done
echo "第二个"
for name1 in $name
do
echo $name1
done
echo "第三个"
for id in {1..5}
do
echo $id
done
echo "第四个"
for idcardno in `seq 1 10`
do
echo $idcardno
done
# for循环没有循环列表,列表必须通过脚本参数传入
for variable
do
echo $variable
done
# for循环没有循环列表,列表必须通过脚本参数传入
for variable
do
echo $variable
done
for variable $@
do
echo $variable
done
for((i=1;i<9;i++))
do
echo "dddd"
done
2.2while
测试和判断用[[expression]] ,可以避免转义
1、正常循环语句结构
#!/bin/bash
conter=5
while [ $conter -gt 0 ]
do
echo -n "$conter"
let "conter-=1"
done
#while操作文件
方式一
while read LINE
do
name=`echo $LINE|awk '{print $1}'`
age=`echo $LINE|awk '{print $2}'`
sex=`echo $LINE|awk '{print $3}'`
echo "$name $age $sex"
done<student.txt
方式二
cat student.txt|while read LINE
do
name=`echo $LINE|awk '{print $1}'`
age=`echo $LINE|awk '{print $2}'`
sex=`echo $LINE|awk '{print $3}'`
echo "$name $age $sex"
done
2、无限循环
方式一
while true
do
echo "1"
done
方式二
while ((1))
do
echo "2"
done
2.3until
1、正常循环结构
#!/bin/bash
unitl [ expression ]
do
command
done
2、无限循环
until false
do
command
done
until ((0))
do
command
done
2.4select
交互使用,菜单选择,输入菜单前的数字
#!/bin/bash
select menu in GK MZ XY FC
do
case $menu in
GK) echo "hello GK" ;;
MZ) echo "hello MZ";;
XY) echo "hell XY";;
*) echo "hdhdlh";;
esac
done
(base) wings27-MacBookPro:Documents be$ ./testautomerge.sh
1) GK
2) MZ
3) XY
4) FC
#? 1
hello GK
#?