免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 920 | 回复: 0
打印 上一主题 下一主题

linux shell script 系统学习1 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-06 09:17 |只看该作者 |倒序浏览

Linux程式设计-11.Shell Script(bash)--(5)控制圈for
示了几个简单的Shell Script,相信您应该对Shell Script有点概念了。现在我们开始来仔细研究一些较高等的Shell Script写作。一些进一步的说明,例如"$"、">"、"、">>"、"1>"、"2>"符号的使用,会在稍後解释。  
--------------------------------------------------------------------------------
for name [ in word; ] do list ; done
控制圈。  
word是一序列的字,for会将word中的个别字展开,然後设定到name上面。list是一序列的工作。如果[in word;]省略掉,那麽name将会被设定为Script後面所加的参数。  
--------------------------------------------------------------------------------
例一:  
#!/bin/sh  
for i in a b c d e f ; do  
    echo $i  
done  
它将会显示出a到f。  
--------------------------------------------------------------------------------
例二: 另一种用法,A-Z
#!/bin/sh  
WORD="a b c d e f g h i j l m n o p q r s t u v w x y z"  
for i in $WORD ; do  
  echo $i  
done  
这个Script将会显示a到z。  
--------------------------------------------------------------------------------
例三 : 修改副档名
如果您有许多的.txt档想要改名成.doc档,您不需要一个一个来。  
#!/bin/sh  
FILES=`ls /txt/*.txt`  
for txt in $FILES ; do  
  doc=`echo $txt | sed "s/.txt/.doc/"`  
  mv $txt $doc  
done  
这样可以将*.txt档修改成*.doc档。  
--------------------------------------------------------------------------------
例四 : meow
#!/bin/sh  
# Filename : meow  
for i ; do  
    cat $i  
done  
当您输入"meow file1 file2 ..."时,其作用就跟"cat file1 file2 ..."一样。  
--------------------------------------------------------------------------------
例五 : listbin  
#!/bin/sh  
# Filename : listbin  
for i in /bin/* ; do  
    echo $i  
done  
当您输入"listbin"时,其作用就跟"ls /bin/*"一样。  
--------------------------------------------------------------------------------
例六 : /etc/rc.d/rc  
拿一个实际的例来说,Red Hat的/etc/rc.d/rc的启动程式中的一个片断。  
for i in /etc/rc.d/rc$runlevel.d/S*; do  
    # Check if the script is there.  
    [ ! -f $i ] && continue  
    # Check if the subsystem is already up.  
    subsys=${i#/etc/rc.d/rc$runlevel.d/S??}  
    [ -f /var/lock/subsys/$subsys ] || \  
    [ -f /var/lock/subsys/${subsys}.init ] && continue  
    # Bring the subsystem up.  
     $i start  
done  
这个例中,它找出/etc/rc.d/rcX.d/S*所有档案,检查它是否存在,然後一一执行。  
Linux程式设计-11.Shell Script(bash)--(6)流程控制case
case word in [ pattern [ | pattern ] ... ] list ;; ] ... esac
case/esac的标准用法大致如下:  
case $arg in  
    pattern | sample) # arg in pattern or sample  
    ;;  
    pattern1) # arg in pattern1  
    ;;  
    *) #default  
    ;;  
esac  
arg是您所引入的参数,如果arg内容符合pattern项目的话,那麽便会执行pattern以下的程式码,而该段程式码则以两个分号";;"做结尾。  
可以注意到"case"及"esac"是对称的,如果记不起来的话,把"case"颠倒过来即可。  
--------------------------------------------------------------------------------
例一 : paranoia
#!/bin/sh  
case $1 in  
        start | begin)  
          echo "start something"  
        ;;  
        stop | end)  
          echo "stop something"  
        ;;  
        *)  
          echo "Ignorant"  
        ;;  
esac  
执行
[foxman@foxman bash]# chmod 755 paranoia  
[foxman@foxman bash]# ./paranoia  
Ignorant  
[foxman@foxman bash]# ./paranoia start  
start something  
[foxman@foxman bash]# ./paranoia begin  
start something  
[foxman@foxman bash]# ./paranoia stop  
stop something  
[foxman@foxman bash]# ./paranoia end  
stop something  
--------------------------------------------------------------------------------
例二 : inetpanel
许多的daemon都会附上一个管理用的Shell Script,像BIND就附上ndc,Apache就附上apachectl。这些管理程式都是用shell script来写的,以下示一个管理inetd的shell script。  
#!/bin/sh  
case $1 in  
    start | begin | commence)  
       /usr/sbin/inetd  
    ;;  
    stop | end | destroy)  
       killall inetd  
    ;;  
    restart | again)  
       killall -HUP inetd  
    ;;  
    *)  
       echo "usage: inetpanel [start | begin | commence | stop | end | destory | restart | again]"  
    ;;  
esac  
--------------------------------------------------------------------------------
例三 : 判断系统
有时候,您所写的Script可能会跨越好几种平台,如Linux、FreeBSD、Solaris等等,而各平台之间,多多少少都有不同之处,有时候需要判断目前正在那一种平台上执行。此时,我们可以利用uname来找出系统资讯。  
#!/bin/sh  
SYSTEM=`uname -s`  
case $SYSTEM in  
    Linux)  
        echo "My system is Linux"  
        echo "Do Linux stuff here..."  
    ;;  
    FreeBSD)  
        echo "My system is FreeBSD"  
        echo "Do FreeBSD stuff here..."  
    ;;  
    *)  
        echo "Unknown system : $SYSTEM"  
        echo "I don't what to do..."  
    ;;  
esac  
  Linux程式设计-11.Shell Script(bash)--(7)流程控制select
  
select name [ in word; ] do list ; done
select顾名思义就是在word中选择一项。与for相同,如果[in word;]省略,将会使用Script後面所加的参数。  

#!/bin/sh  
WORD="a b c"  
select i in $WORD ; do  
  case $i in  
    a)  
      echo "I am A"  
    ;;  
    b)  
      echo "I am B"  
    ;;  
    c)  
      echo "I am C"  
    ;;  
    *)  
      break;  
    ;;  
  esac  
done  
执行结果
[foxman@foxman bash]# ./select_demo  
1) a  
2) b  
3) c  
#? 1  
I am A  
1) a  
2) b  
3) c  
#? 2  
I am B  
1) a  
2) b  
3) c  
#? 3  
I am C  
1) a  
2) b  
3) c  
#? 4  
Linux程式设计-11.Shell Script(bash)--(8)返回状态Exit
在继续下去之前,我们必须要切入另一个话题,即返回状态值 - Exit Status。因为if/while/until都迁涉到了使用Exit Status来控制程式流程的问题。  
--------------------------------------------------------------------------------
许多人都知道,在许多语言中(C/C++/Perl....),都有一个exit的函数,甚至连Bash自己都有个exit的内建命令。而exit後面所带的数字,便是返回状态值 - Exit Status。  
返回状态值可以使得程式与程式之间,利用Shell script来结合的可能性大增,利用小程式,透过Shell script,来完成很杂的工作。  
在shell中,返回值为零表示成功(True),非零值为失败(False)。  
--------------------------------------------------------------------------------
举例来说,以下这个两个小程式yes/no分别会返回0/1(成功/失败):  
/* yes.c */  
void main(void) { exit(0); }  
/* no.c */  
void main(void) { exit(1); }  
那麽以下这个"YES"的shell script便会显示"YES"。  
#!/bin/sh  
# YES  
if yes ; then  
    echo "YES"  
fi  
而"NO"不会显示任何东西。  
#!/bin/sh  
# NO  
if no ; then  
    echo "YES"  
fi  
--------------------------------------------------------------------------------
test express  
[ express ]  
在Shell script中,test express/[ express ]这个语法被大量地使用,它是个非常实用的指令。由於它的返回值即Exit Status,经常被运用在if/while/until的场合中。而在後面,我们也会大量运用到,在进入介绍if/while/until之前,有必要先解一下。  
其返回值为0(True)或1(False),要看表述(express)的结果为何。  
express格式  
-b file : 当档案存在并且属性是Block special(通常是/dev/xxx)时,返回True。  
-c file : 当档案存在并且属性是character special(通常是/dev/xxx)时,返回True。  
-d file : 当档案存在并且属性是目录时,返回True。  
-e file : 当档案存在时,返回True。  
-f file :  当档案存在并且是正常档案时,返回True。  
-g file :  当档案存在并且是set-group-id时,返回True。  
-k file :  当档案存在并且有"sticky" bit被设定时,返回True。  
-L file :  当档案存在并且是symbolic link时,返回True。  
-p file :  当档案存在并且是name pipe时,返回True。  
-r file :  当档案存在并且可读取时,返回True。  
-s file :  当档案存在并且档案大小大於零时,返回True。  
-S file : 当档案存在并且是socket时,返回True。  
-t fd : 当fd被开启为terminal时,返回True。  
-u file : 当档案存在并且set-user-id bit被设定时,返回True。  
-w file : 当档案存在并且可写入时,返回True。  
-x file : 当档案存在并且可执行时,返回True。  
-O file : 当档案存在并且是被执行的user id所拥有时,返回True。  
-G file : 当档案存在并且是被执行的group id所拥有时,返回True。  
file1 -nt file2 : 当file1比file2新时(根据修改时间),返回True。  
file1 -ot file2 : 当file1比file2旧时(根据修改时间),返回True。  
file1 -ef file2 : 当file1与file2有相同的device及inode number时,返回True。  
-z string : 当string的长度为零时,返回True。  
-n string : 当string的长度不为零时,返回True。  
string1 = string2 : string1与string2相等时,返回True。  
string1 != string2 : string1与string2不相等时,返回True。  
! express : express为False时,返回True。  
expr1 -a expr2 : expr1及expr2为True。  
expr1 -o expr2 : expr1或expr2其中之一为True。  
arg1 OP arg2 : OP是-eq[equal]、-ne[not-equal]、-lt[less-than]、-le[less-than-or-equal]、-gt[greater-than]、-ge[greater-than-or-equal]的其中之一。  
--------------------------------------------------------------------------------
在Bash中,当错误发生在致命信号时,bash会返回128+signal number做为返回值。如果找不到命令,将会返回127。如果命令找到了,但该命令是不可执行的,将返回126。除此以外,Bash本身会返回最後一个指令的返回值。若是执行中发生错误,将会返回一个非零的值。  
Fatal Signal : 128 + signo  
Can't not find command : 127  
Can't not execute : 126  
Shell script successfully executed : return the last command exit status  
Fatal during execution : return non-zero  
Linux程式设计-11.Shell Script(bash)--(9)流程控制if
if list then list [ elif list then list ] ... [ else list ] fi
几种可能的写法  
--------------------------------------------------------------------------------
第一种  
if list then  
  do something here  
fi  
当list表述返回值为True(0)时,将会执行"do something here"。  
例一 : 当我们要执行一个命令或程式之前,有时候需要检查该命令是否存在,然後才执行。  
if [ -x /sbin/quotaon ] ; then  
    echo "Turning on Quota for root filesystem"  
    /sbin/quotaon /  
fi  
例二 : 当我们将某个档案做为设定档时,可先检查是否存在,然後将该档案设定值载入。  
# Filename : /etc/ppp/settings  
PHONE=1-800-COLLECT  
#!/bin/sh  
# Filename : phonebill  
if [ -f /etc/ppp/settings ] ; then  
    source /etc/ppp/settings  
    echo $PHONE  
fi  
执行  
[foxman@foxman ppp]# ./phonebill  
1-800-COLLECT  
--------------------------------------------------------------------------------
第二种  
if list then  
  do something here  
else  
  do something else here  
fi  
例三 : Hostname  
#!/bin/sh  
if [ -f /etc/HOSTNAME ] ; then  
    HOSTNAME=`cat /etc/HOSTNAME`  
else  
    HOSTNAME=localhost  
fi  
--------------------------------------------------------------------------------
第三种  
if list then  
  do something here  
elif list then  
  do another thing here  
fi  
例四 : 如果某个设定档允许有好几个位置的话,例如crontab,可利用if then elif fi来找寻。  
#!/bin/sh  
if [ -f /etc/crontab ] ; then  
    CRONTAB="/etc/crontab"  
elif [ -f /var/spool/cron/crontabs/root ] ; then  
    CRONTAB="/var/spool/cron/crontabs/root"  
elif [ -f /var/cron/tabs/root ] ; then  
    CRONTAB="/var/cron/tabs/root"  
fi  
export CRONTAB  
--------------------------------------------------------------------------------
第四种  
if list then  
  do something here  
elif list then  
  do another thing here  
else  
  do something else here  
fi  
例五 : 我们可利用uname来判断目前系统,并分别做各系统状况不同的事。  
#!/bin/sh  
SYSTEM=`uname -s`  
if [ $SYSTEM = "Linux" ] ; then  
  echo "Linux"  
elif [ $SYSTEM = "FreeBSD" ] ; then  
  echo "FreeBSD"  
elif [ $SYSTEM = "Solaris" ] ; then  
  echo "Solaris"  
else  
  echo "What?"  
fi  

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/14952/showart_81475.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP