例1–ex1.sh:
#!/bin/sh
#This is to show what a example looks like.
echo "My First Shell!"
echo "This is current directory."
/bin/pwd
echo
echo "This is files."
/bin/ls
执行结果:
My first shell!
This is current directroy.
/test
This is files.
a.sh a.tar.gz a.zip ex1.sh ex2.info ex2.sh ex3.sh shelldir
例2–ex2.sh:
#!/bin/sh
/bin/date +%F >> /test/shelldir/ex2.info
echo "disk info:" >> /test/shelldir/ex2.info
/bin/df -h >> /test/shelldir/ex2.info
echo >> /test/shelldir/ex2.info
echo "online users:" >> /test/shelldir/ex2.info
/usr/bin/who | /bin/grep -v root >> /test/shelldir/ex2.info
echo "memory info:" >> /test/shelldir/ex2.info
/usr/bin/free -m >> /test/shelldir/ex2.info
echo >> /test/shelldir/ex2.info
#write root
/usr/bin/write root < /test/shelldir/ex2.info && /bin/rm /test/shelldir/ex2.info
#crontab -e 定时任务
#0 9 * * 1-5 /bin /sh /test/ex2.sh
执行结果:
意思是把上述的所有信息都输出到/test/shelldir/ex2.info 然后定时发送给root 用户
例3–ex3.sh
#!/bin/sh
DATE=`/bin/date +%Y%m%d` ``为命令替换符
echo "TODAY IS $DATE"
/bin/ls -l $1 $1 $2 $3为占位符,也就是执行该命令的时候要传的参数
/bin/ls -l $2
/bin/ls -l $3
执行结果:sh ex3.sh /usr/local /usr/local/shelldir /usr/local/redis
TODY IS 20180727
total 52
drwxr-xr-x. 2 root root 4096 Jul 26 23:28 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 2 root root 4096 Sep 23 2011 include
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib64
drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec
drwxr-xr-x. 4 root root 4096 Jul 26 23:31 redis
drwxrwxr-x. 6 root root 4096 Jan 13 2015 redis-3.0.3
drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin
drwxr-xr-x. 5 root root 4096 Jul 25 04:14 share
drwxr-xr-x. 2 root root 4096 Jul 26 23:23 software
drwxr-xr-x. 2 root root 4096 Sep 23 2011 src
例4–ex4.sh:
特殊变量:
$* 这个程序的所有参数
$# 这个程序的参数个数
$$ 这个程序的PID
$! 执行上一个后台命令的PID
$? 执行上一个命令的返回值
$(0~9) 输出几个参数的值
#!/bin/sh
DATE=`/bin/date +%F`
echo "today is $DATE"
echo '$# :' $#
echo '$* :' $*
echo '$? :' $?
echo '$$ :' $$
echo '$0 :' $0
执行结果:
today is 2018-07-27
$#:0
$*:
$?:0
$$:28805
$0:ex4.sh
例5–ex5.sh:
read命令从键盘读入数据,赋给变量。
#!/bin/sh
read f s t
echo "the first is $f"
echo "the second is $s"
echo "the third is $t"
执行时可以加-x参数 跟踪执行进度
例6–ex6.sh:
expr 只能对整数进行运算 + - * /
注意:
1、expr的运算比较用空格间隔开
2、*表示转义字符 乘法
3、保持先算惩处后算加法。如果需要优先运算则需要加命令替换符
4、 也可以对变量进行运算操作
测试命令:使用test命令可以对文件、字符串等进行测试,一般配合控制语句一起使用,不然单独的话看不到结果
if语句的语法格式:
if test -d $1 then … else…
test 可以用[]代替,如 test -d $1 等价于[ ‘空格’-d ‘空格’ $1]
#!/bin/sh
# if test $1 then ... else ... fi
if [ -d $1 ] test -d 测试是否为目录 $!为占位符
then
echo "this is a directory!"
else
echo "this is not a directory!"
fi
例7–ex7.sh
#!/bin/sh
# if test then ... elif test then ... else ... fi
if [ -d $1 ]
then
echo "is a directory!"
elif [ -f $1 ]
then
echo "is a file!"
else
echo "error!"
fi
例8–ex8.sh
#!/bin/sh
# -a -o
if [ $1 -eq $2 -a $1 = 1 ]
then
echo "param1 == param2 and param1 = 1"
elif [ $1 -ne $2 -o $1 = 2 ]
then
echo "param1 != param2 or param1 = 2"
else
echo "others"
fi
-a 为逻辑与 -o为逻辑或
例9–ex9.sh
#!/bin/sh
# for var in [params] do ... done
for var in 1 2 3 4 5 6 7 8 9 10
do
echo "number is $var"
done
例10–ex10.sh
#!/bin/sh
# select var in [params] do ... done
select var in "java" "c++" "php" "linux" "python" "ruby" "c#"
do
break
done
echo "you selected $var"
例11–ex11.sh
#!/bin/sh
read op
case $op in
a)
echo "you selected a";;
b)
echo "you selected b";;
c)
echo "you selected c";;
*)
echo "error"
esac
例12–ex12.sh
#!/bin/sh
#while test do ... done
num=1
sum=0
while [ $num -le 100 ]
do
sum=`expr $sum + $num`
num=`expr $num + 1`
done
#sleep 5
echo $sum
例13–ex13.sh
#!/bin/sh
i=0
while [ $i -le 100 ]
do
i=`expr $i + 1`
if [ $i -eq 5 -o $i -eq 10 ]
then continue;
else
echo "this number is $i"
fi
if [ $i -eq 15 ]
then break;
fi
done