#!/bin/sh
#if.sh
a='10' #定义一个变量a
b='20' #定义一个变量b if[ $a -lt $b ] #判断变量a是否小于变量b
then
echo "$a is less than $b" else
echo "$a is great or equan to $b"
fi #if 结束
#!/bin/sh
#grepif.sh
ls /home | grep "wei">/dev/null 2>&1 #执行这条命令 if[ $? -eq 0 ] #判断上一条命令的推出状态(由'$?'保存)是否等于0,
then
echo "wei is here" else
echo "wei is not here"
fi
#!/bin/sh
#测试 cp 这条命令执行是否成功。cp命令的自己的输出打印到其他地方 if cp myfile myfile.bak >/dev/null 2>&1
then
echo "copy good" else
echo "copy not successfully"
fi
case用法:
#!/bin/sh
echo -n "please enter a number from 1 to 5 :"
read ANS
case $ANS in
1) echo "you entered 1"
;;
2) echo "you entered 2"
;;
3) echo "you entered 3"
;;
4) echo "you entered 4"
;;
5) echo "you entered 5"
;; *) echo "`basename $0` : this is not between 1 and 5 " 2>&1
exit
;;
esac
for用法: for 变量名 in 列表
do
命令1
命令2..
done
#!/bin/sh
# for.sh
for loop in"my name is zieckey"
do
echo "Result:$loop"
done
运行结果:$ ./for.sh
Result:my name is zieckey
利用命令行参数:
#!/bin/sh
# forfind.sh
for loop in $*
do
find /-name $loop -print
done
把一个文件(参数1)复制10次到另一个文件中(参数2)
#!/bin/sh
# forcat.sh
for(( i = 0; i < 10; i++))
do
cat $1 >> $2
done
例如:
$ ./forcat.sh keywords.n keywords.10n