分类:
2010-07-15 15:10:03
[svnroot@localhost test]$ vi test06-ifthen.sh
#!/bin/bash
#This program is used to study if then
#created on 0713
echo "Press y to continue"
read yn
if ["$yn" = "y"];then
echo "script is running.."
else
echo "STOP!"
fi
错误了
[svnroot@localhost test]$ sh test06-ifthen.sh
Press y to continue
y
test06-ifthen.sh: line 6: [y=y]: command not found
奇怪
原因是:
if ["$yn" = "y"]
改成
if ["$yn" == "y"]
--还是错,注意[后与]前的空格
if [ "$yn" == "y" ]; then
*******************************************************
[svnroot@localhost test]$ vi test08-ifthen.sh
#!/bin/bash
#set parameters in the if then
#created on 0714
echo "parameter = $1"
if [ "$1"=="hello" ]; then--错误
if [ "$1" == "hello" ]; then
echo "Hello!"
elif [ "$1"=="" ]; then--错误
elif [ "$1" == "" ]; then
echo "You must input parameters"
else
echo "The only accept parameter is hello"
fi
注:if空格[空格。。。空格];then
*******************************************************
[svnroot@localhost test]$ vi port.sh
#!/bin/bash
#program:Using th study the [if...then...fi] program
#created on 0714
#1.print the program's work in your screen
echo "New,the services of your Linux system will be detect!"
echo "The www,ftp,ssh,and sendmail+pop3 will be detect!"
echo ""
#2.www
www='netstat -an|grep LISTEN|grep :80'
www=`netstat -an|grep LISTEN|grep :80`--正确写法,不是单引号,是ESC下面的那个键
echo "www = $www"
if [ "$www" != "" ];then
echo "www is running"
else
echo "www is not running"
fi
#3.ftp
ftp=`netstat -an|grep LISTEN|grep :21`
echo "ftp = $ftp"
if [ "$ftp" != "" ];then
echo "ftp is running"
else
echo "ftp is not running"
fi
*******************************************************
[svnroot@localhost test]$ vi test09-case.sh
#!/bin/bash
#program :using case mode
#created on 0715
#1\print this program
echo "This program will print you rselection"
case $1 in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "usage {one|two|three}"
exit 1
*******************************************************
[svnroot@localhost test]$ vi test10-case.sh
#!/bin/bash
#program:using case mode
#created on 0715
#1.print this program
echo "Press your select one,two,three"
read number
case $number in
one)
echo "your choice is one"
;;
two)
echo "your choice is two"
;;
three)
echo "your choice is three"
;;
*)
echo "your choice is wrong"
exit 1
esac
注:少了;;会报错
*******************************************************
[svnroot@localhost test]$ vi test11-loop.sh
#!/bin/bash
#using for and loop
#created on 0715
declare -i s
echo "init s=$s"
for ((i=1;i<=100;i=i+1))
do
s=s+i
done
echo "The count is ==>$s"
[svnroot@localhost test]$ sh test11-loop.sh
init s=
The count is ==>5050
*******************************************************
[svnroot@localhost test]$ vi test12-loop.sh
#!/bin/bash
#using while and loop
#created on 0715
declare -i i
declare -i s
while [ "$i" != "101" ]
do
s=s+i
i=i+1
done
echo "The cound is ==>$s"
*******************************************************
[svnroot@localhost test]$ vi test12-loop.sh
#!/bin/bash
#using until and loop
#created on 0715
declare -i i
declare -i s
until [ "$i" == "101" ]
do
s=s+i
i=i+1
done
echo "The cound is ==>$s"
*******************************************************
[svnroot@localhost test]$ vi test14-for.sh
#!/bin/bash
#using for...do...done
#created on 0715
LIST="Tomy Jony Mary Geoge"--》注:=号两边不能有空格,否则报错
for i in $LIST
do
echo $i
done
*******************************************************
[svnroot@localhost test]$ vi test15-for.sh
#!/bin/bash
#using for and loop to read the accound of this server
#created on 0715
account=`cut -d ":" -f1 /etc/passwd|sort`
echo "The following is your linux server's account"
for i in $account
do
echo $i
done
*******************************************************
[svnroot@localhost test]$ vi test16-loop.sh
#!/bin/bash
#using until
#created on 0715
echo "Press Y/y to stop"
until [ "$yn" = "Y" ]||[ "$yn" = "y" ]
do
read yn
done
echo "stop here"
--学习脚本,又让偶感觉到没啥不可编的乐趣,继续努力ing