eg1、根据用户的不同选择,程序显示不同的结果。Y就显示“OK, continue " N显示"Oh , interrupt!"
如果不是Y/y/N/n之内的其他字符,就显示“I don't know what your choice is."
#!/bin/bash
#Program:
# This program shows the user's choice
#History:
#2011/12/15 Jason First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
read -p "Please input your choice:" yn
if [ $yn = Y ] || [ $yn = y ]; then
echo "OK , continue "
exit 0
elif [ $yn = N ] || [ $yn = n ] ; then
echo "Oh , interrupt ."
else
echo " I don't know what your choice is."
fi
eg2、判断$1是否为hello ,如果是的话就显示"Hello , hou are you. "; 如果没有加任何参数,就提示用户必须使用参数 ; 如果参数不是hello ,提示只能输入hello
#!/bin/bash
#Program:
# Check $1 is equal to "hello"
#History:
#2011/12/15 Jason First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
if [ "$1" == "hello" ] ; then
echo "Hello , how are you?"
exit 0
elif [ "$1" == "" ] ; then
echo "You must input a parameter!"
exit 0
else
echo "You just can input hello!"
exit 0
fi
#!/bin/bash
#Program:
# Check $1 is equal to "hello"
#History:
#2011/12/15 Jason First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
if [ "$1" == "hello" ] ; then
echo "Hello , how are you?"
exit 0
elif [ "$1" == "" ] ; then
echo "You must input a parameter!"
exit 0
else
echo "You just can input hello!"
exit 0
fi
#!/bin/bash
#Program:
# Check $1 is equal to "hello"
#History:
#2011/12/15 Jason First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
if [ "$1" == "hello" ] ; then
echo "Hello , how are you?"
exit 0
elif [ "$1" == "" ] ; then
echo "You must input a parameter!"
exit 0
else
echo "You just can input hello!"
exit 0
fi
eg3、检查服务器上,www/ssh/ftp/mail服务是否开启
#!/bin/bash
#Program:
# This program test "www" server "ssh" server "ftp" server and "mail" server already open.
#History:
#2011/12/15 Jason Frist release
PATH=/bin:/sbin:/usr/bin:/usr/sbin/:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
echo "Now , I will detect Linux server's services!"
echo "The www , ftp , ssh and mail will be detected!"
testing=$(netstat -tuln | grep ':80 ')
if [ -n "$testing" ] ; then
echo "Your www service is open."
else
echo "Your www service is closed."
fi
testing=$(netstat -tuln | grep ':22 ')
if [ -n "$testing" ] ; then
echo "Your ssh service is open."
else
echo "Your ssh service is closed."
fi
testing=$(netstat -tuln | grep ':21 ')
if [ -n "$testing" ] ; then
echo "Your ftp service is open."
else
echo "Your ftp service is closed."
fi
testing=$(netstat -tuln | grep ':25 ')
if [ -n "$testing" ] ; then
echo "Your mail service is open."
else
echo "Your mail service is closed."
fi