1. if 语句
来看 一个例子(myprog)
#!/bin/sh
if [ $# = 0 ]
then
echo No arg
elif [ $# = 1 ]
then
echo thereis only 1 arg
echo it is $1
else
echo there are more than 1 arg
echo they are $*
fi
运行./myprog,结果为:
No arg
运行./myprog a,结果为:
thereis only 1 arg
it is a
运行./myprog a b c d e,结果为:
there are more than 1 arg
they are a b c d e
2. case语句
case string in
str1)
... ;; #注意结束是一对;;
str2)
... ;;
.
.
.
esac
str1,str2...可以用通配符.
来看一个例子(myprog):
#!/bin/sh read str case str in a) echo option a;; b) echo option b;; *) echo you inputed "$str";; esac
运行./myprog,输入a,结果为:
you inputed a
3. for语句
for var in list
do
...
done
来看一个例子(myprog)
#!/bin/sh for i in $(seq 1 10) do echo $i done
运行完之后结果为
1 2 3 4 5 6 7 8 9 104. while 及 until语句
while exp
do
...
done
until exp
do
...
done