shell programming 经验总结

shell programming 经验总结

1. 脚本的分行


经过试验,可以将多行的shell脚本写成一行,一次执行多个命令之间用";"分隔,控制结构可以参考man bash,这样就可以用system函数来执行一个长脚本了。
如果将一行的脚本写成多行(通常的做法,可读性较好),原来控制结构的";"可以用换行符代替,原来的";"可保留可不保留。但";;"不可用一个换行符加一个";"代替.

下面是2个例子,检查某进程是否在运行:
例1:
#!/bin/sh
#filename: sysmon.sh
#usage" sysmon.sh appname1 appname2 ...
for app in "$@" ;
do
  if [ `ps -ef | grep "${app}" | grep -v "grep" | grep -v "sysmon.sh" |wc -l` == 0 ];
  then
          echo "/"${app}/" not running";
  else
          echo "/"${app}/" running";
  fi
done
#-------------------------------------
#上面的代码中各行末尾的";"可以去掉

例2:(不需要wc)
#!/bin/sh
#filename: sysmon3.sh
#usage: sysmon3.sh appname1 appname2 ...
for app in "$@";
do
  if ps -ef | grep "${app}" | grep -v "grep" | grep -v "sysmon3.sh" ; then
          echo "/"${app}/" running";
  else
          echo "/"${app}/" not running";
  fi
done
#---------------------------------------

2. && 和 ";" 的区别:

    ";"只是分隔符, 在";'前后的命令之间没有关联, "&&" 和 "||"的前后命令之间有关联, 下面是man bash里面的描述:

Lists
       A list is a sequence of one or more pipelines separated by one  of  the
       operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or
       <newline>.

       Of these list operators, && and || have equal precedence, followed by ;
       and &, which have equal precedence.

       A  sequence  of  one or more newlines may appear in a list instead of a
       semicolon to delimit commands.

       If a command is terminated by the control operator &,  the  shell  exe-
       cutes  the command in the background in a subshell.  The shell does not
       wait for the command to finish, and the return status is  0.   Commands
       separated  by  a  ; are executed sequentially; the shell waits for each
       command to terminate in turn.  The return status is the exit status  of
       the last command executed.

       The  control operators && and || denote AND lists and OR lists, respec-
       tively.  An AND list has the form

              command1 && command2

       command2 is executed if, and only if, command1 returns an  exit  status
       of zero.

       An OR list has the form

              command1 || command2

       command2  is  executed  if and only if command1 returns a non-zero exit
       status.  The return status of AND and OR lists is the  exit  status  of
       the last command executed in the list.

3. while 循环

while list do list done
当list为True时,该圈会不停地执行。 
例一 : 无限回圈写法 
#!/bin/sh 

while : ; do 
  echo "do something forever here" 
  sleep 5 
done 

例二 : 强迫把pppd杀掉。 
#!/bin/sh 

while [ -f /var/run/ppp0.pid ] ; do 
    killall pppd 
done 


--------------------------------------------------------------------------------

until list do list done
当list为False(non-zero)时,该圈会不停地执行。 
例一 : 等待pppd上线。 
#!/bin/sh 
until [ -f /var/run/ppp0.pid ] ; do 
    sleep 1 
done 

(http://www.fanqiang.com)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值