select语句的用法举例:
1.编写select1.sh脚本,内容如下:
#!/bin/bash
echo "What color you want to select?"
select color in "blue" "red" "green" "orange"
do
break
done
if [ $color = "blue" ]
then
echo "$color"
else
echo "other"
fi
2.编写select2.sh脚本,内容如下,执行的时候,向脚本传递参数(例sh select2.sh blue color green )
#!/bin/bash
echo "What color you want to select?"
select color
do
break
done
echo "You select color is $color"
for循环嵌套举例:
1.打印9*9棋盘,脚本如下:
#!/bin/bash
for((i=1;i<10;i++))
do
for((j=1;j<10;j++))
do
let "sum=$i+$j"
let "temp=$sum%2"
if [ $temp -eq 0 ]
then
echo -e -n "\033[47m " //打印白色
else
echo -e -n "\033[40m " //打印黑色
fi
done
echo -e -n "\033[40m"
echo " "
done
2.打印*号阶梯,脚本如下:
#!/bin/bash
for((i=1;i<10;i++))
do
for((j=1;j<=i;j++))
do
echo -n "*"
done
echo " "
done
3.打印9*9乘法表,脚本如下:
#!/bin/bash
for((i=1;i<=9;i++))
do
for((j=1;j<=i;j++))
do
temp=$(($i*$j))
echo -n "$i X $j=$temp "
done
echo " "
done