鸟哥shell script学习笔记
时间:2010-07-15 来源:echo_shu
#!/bin/bash
#show "hello world"in the screen
#create on 20100712
hello=hello\ \!\ How\ are\ you\ \?
echo $hello
[svnroot@localhost test]$ sh test01-hello.sh
hello ! How are you ? ******************************************************* [svnroot@localhost test]$ vi test02-2var.sh
#!/bin/bash
#call two variation
#create on 20100712
name="V.Bird"
myname1="My name is $name"
myname2='My name is $name'
echo $name
echo $myname1
echo $myname2
[svnroot@localhost test]$ sh test02-2var.sh
V.Bird
My name is V.Bird
My name is $name ******************************************************* [svnroot@localhost test]$ vi test03-declare.sh
#!/bin/bash
#this program is used to "declare" variables
#created on 20100712
number1=2*3+5*13-32+25
declare -i number2=2*3+5*13-32+25
echo "number1=$number1"
echo "number2=$number2"
执行结果:
[svnroot@localhost test]$ sh test03-declare.sh
number1=2*3+5*13-32+25
number2=64 ******************************************************* [root@localhost test]# vi test04-read.sh
#!/bin/bash
#This program is used to "read" variables
#created on 20100713
echo "Please keyin your name,and press Enter to start"
read name
echo "name = $name"
[root@localhost test]# sh test04-read.sh
Please keyin your name,and press Enter to start
echo
name = echo *******************************************************
[root@localhost test]# vi test05-0123
#!/bin/bash
#This program will define what is the parameters
#created on 20100713
echo "This script's name =>$0"
echo "parameters $1 $2 $3"
[root@localhost test]# sh test05-0123 pa1 pa2 pa3
This script's name =>test05-0123
parameters pa1 pa2 pa3 *******************************************************
[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