shell脚本第三部分
if判断语句
运用if,fi实例
vi sh06-2.sh
#!/bin/sh
read -p "Please input (y/Y n/N) :" yn
if [ "$yn" = "y" -o "$yn" = "Y" ];then
echo "ok go on please"
exit 0
fi
if [ "$yn" = "n" -o "$yn" = "N" ];then
echo "on byebye!"
exit 0
fi
echo "Sorry ,i don't know you choice! $yn"
sh sh06-2.sh
运用if,elif,else,fi判断实例
vi sh06-3.sh
#!/bin/sh
read -p "Please input y/Y n/N :" yn
if [ "$yn" = "y" -o "$yn" = "Y" ]; then
echo "go on go on"
elif [ "$yn" = "n" -o "$yn" = "N" ]; then
echo "oh,byebye!"
else
echo "I don't know your choice $yn"
fi
sh sh06-3.sh
使用if和$1实例
vi sh07.sh
#!/bin/sh
if [ "$1" == "one" ]; then
echo "this is the first paramter!"
elif [ "$1" == "" ]; then
echo "you should input a paramter,just like >$0 someword"
else
echo "only one is allowed to input"
fi
sh sh07.sh one
使用if和netstat -tlun 查看系统已经启动的服务实例
vi sh08.sh
#!/bin/sh
echo "this script will to find which service have started!"
#to find www service
testing=`netstat -tlun | grep ":80"`
if [ -n "$testing" ]; then
echo "WWW server has started!"
fi
#to find vsftpd service
testing=`netstat -tlun | grep ":21"`
if [ "$testing" != "" ]; then
echo "vsftpd has started!"
fi
#to find ssh service
testing=`netstat -tlun |grep ":22"`
if [ -n "$testing" ]; then
echo "SSH server has started"
fi
#to find mail service
testing=`netstat -tlun | grep ":25"`
if [ "$testing" != "" ]; then
echo "MALL server has started"
fi
sh sh08.sh
注意:该脚本中``是引用,意思是首先要执行该引用中的命令然后返回给testing
netstat -tlun 查看所有端口占用运行情况
case判断语句
使用case和$1实例
vi sh09.sh
#!/bin/sh
case $1 in
"one")
echo " the paramter is one"
;;
"")
echo "you should input something,like > $0 someword"
;;
*)
echo " you only can input one"
esac
sh sh09.sh one
注意:这里的$1为脚本的默认变量,$0为脚本的名称