Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74712
  • 博文数量: 12
  • 博客积分: 1425
  • 博客等级: 上尉
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-11 09:15
文章分类

全部博文(12)

文章存档

2011年(1)

2010年(3)

2008年(8)

我的朋友

分类:

2010-07-15 15:10:03

  上次努力学脚本都是两年前的事啦。。。两年半前换工作成功,没有了继续学习的动力。。。现在,又面临合同到期处于是否要再换份工作的纠结中,因此又有了学习的动力。貌似这也不是偶个人的原因,招聘条件上写着要这要那,招进去后总会发现没这没那--对别人不要太苛刻地说。
  这两天看了鸟哥的shell script那章,将例子贴上来记录一下:
[svnroot@localhost test]$ vi test01-hello.sh
#!/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

阅读(1717) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~