#!/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
#!/bin/sh
WP=`pwd` #通过反引号(键盘1键左边的那个)告示shell这是一个命令,并且将命令执行的输出赋值给WP变量
echo $WP #输出WP变量
#!/bin/sh
#paramif
if [ $# -lt 3 ] #如果输入的参数个数小于3(不包括脚本名字)
then
echo "usage : $0 arg1 arg2 arg3" # ‘$0’保存了执行进程的名字(这里就是本脚本的文件名)
else
echo "arg1 = $1"
echo "arg2 = $2"
echo "arg3 = $3"
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
|