if case for while
#!/bin/bash
a=$1
if [ $a ] #判断$1是否为空
then #非空
echo "the input is No:$a"
exit 0
else #空
read -p "input a Nov:" a
#a=$b
fi
case $a in
"1")
echo "your input is No:1"
;;
"2")
echo "your input is No:2"
;;
*)
echo "dont know"
;;
esac
~~~~~~~~~~~~~~~~
#!/bin/bash
ip=(
a
b
c
d
) #ip=("aa" "bb" "cc") 也一样
for i in ${ip[*]} ; do #注意不是ip[$*]
echo $i
done
for ((i=0;i<10;i++));do
echo -n $i
done
#!/bin/bash
name=("Tom" "Tomy" "John")
for i in 0 1 2
do
echo $i:${name[$i]} # name[$*] 也是可以的 注意不是name[*]
done
~~~~~~~~~~~~~~~~~~~~~~~~~~~
while true;do #while 死循环
echo “just test”
done
#!/bin/bash
while [ $# -gt 0 ];do #当$#大于0就执行 until与while相反 until [ $# -lt 1 ];do 执行,直到 $#小于1时 停止
echo $*
shift
done
结果
zzx@ubuntu:~$ sh shift.sh 1 2 3 4 5
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
while true ; do ### shell没do while 相当于 do……while 循环
if [];then
echo " test"
break ####break 退出while循环 继续执行剩下的脚本 如果是exit就退出整个shell 不在执行剩下脚本
fi
done
- while expression
- do
- command
- command
- ```
- done
- 1 #!/bin/sh
- 2 int=1
- 3 while(( $int<=5 ))
- 4 do
- 5 echo $int
- 6 let "int++"
- 7 done
- 1 #用脚本演示使用结束标记控制while循环实现猜1~10内的数
- 2 #!/bin/sh
- 3
- 4 echo "Please input the num (1~~10): "
- 5 read num
- 6 while [[ $num != 4 ]]
- 7 do
- 8 if [ $num -lt 4 ]
- 9 then
- 10 echo "Too small ,Try again.."
- 11 read num
- 12 elif [ $num -gt 4 ]
- 13 then
- 14 echo "Too big ,Try again.. "
- 15 read num
- 16 else
- 17 exit 0
- 18 fi
- 19 done
- 20 echo "Yes ,you are right !!"
- 1 #!/bin/sh
- 2 echo "Please input the num:"
- 3 read num
- 4 sum=0
- 5 i=1
- 6 signal=0
- 7 while [[ $signal != 1 ]]
- 8 do
- 9 if [ $i -eq $num ]
- 10 then
- 11 let "signal=1"
- 12 let "sum+=i"
- 13 echo "1+2、、、+$num=$sum"
- 14 else
- 15 let "sum=sum+i"
- 16 let "i++"
- 17 fi
- 18 done
- 1 #!/bin/sh
- 2
- 3 echo "Please input arguements is $# "
- 4 echo "What you input : "
- 5 while [[ $* != "" ]]
- 6 do
- 7 echo $1
- 8 shift
- 9 done