| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > SHELL

Bourne Shell及shell编程

时间:2006-06-28 22:35:53  来源:Linux联盟收集  作者:Linux联盟收集
r />                $ echo Hello ${UNAME:-there} 6w6Linux联盟
                Hello there 6w6Linux联盟
                $ echo $UNAME   #变量值并未发生变化 6w6Linux联盟
  6w6Linux联盟
                $ UNAME=hbwork 6w6Linux联盟
                $ echo Hello ${UNAME:-there} 6w6Linux联盟
                Hello hbwork 6w6Linux联盟
                $ 6w6Linux联盟
        .另一种情况:改变变量的值,格式如下: 6w6Linux联盟
            ${variable:=value} 6w6Linux联盟
  6w6Linux联盟
            例: 6w6Linux联盟
                $ echo Hello $UNAME 6w6Linux联盟
                Hello 6w6Linux联盟
                $ echo Hello ${UNAME:=there} 6w6Linux联盟
                Hello there 6w6Linux联盟
                $ echo $UNAME   #变量值并未发生变化 6w6Linux联盟
                there 6w6Linux联盟
                $ 6w6Linux联盟
        .变量替换中使用命令替换 6w6Linux联盟
                $USERDIR=${$MYDIR:-`pwd`} 6w6Linux联盟
  6w6Linux联盟
        .在变量已赋值时进行替换  ${variable:+value} 6w6Linux联盟
        .带有错误检查的有条件变量替换 6w6Linux联盟
          ${variable:?value} 6w6Linux联盟
          例: 6w6Linux联盟
          $ UNAME= 6w6Linux联盟
          $ echo ${UNAME:?"UNAME has not been set"} 6w6Linux联盟
          UNAME: UNAME has not been set 6w6Linux联盟
          $ echo ${UNAME:?} 6w6Linux联盟
          UNAME: parameter null or not set 6w6Linux联盟
  6w6Linux联盟
  (2)位置变量(Shell参数) 6w6Linux联盟
  在shell script中位置参数可用$1..$9表示,$0表示内容通常为当前执行程序的文件名。 6w6Linux联盟
  .防止变量值被替换 readonly variable 6w6Linux联盟
  .使用export命令输出变量,使得变量对子shell可用,当shell执行一下程序时,shell 6w6Linux联盟
   将为其设置一个新的环境让其执行,这称之了subshell.  在Bourne Shell中变量通常 6w6Linux联盟
   被认为是本地变量,也就是说在对其赋值之外的shell环境之外是不认识此变量的。使 6w6Linux联盟
   用export对subshell可用。 6w6Linux联盟
  6w6Linux联盟
   例:对变量PS1的export操作,shell的提示符将发生变化。 6w6Linux联盟
   $ PS1=`hostname`$ 6w6Linux联盟
        peony$sh 6w6Linux联盟
        $ echo $PS1 6w6Linux联盟
        $  <-输出结果 6w6Linux联盟
        $ exit 6w6Linux联盟
        peony$export PS1 6w6Linux联盟
        peony$sh 6w6Linux联盟
        peony$ echo $PS1 6w6Linux联盟
        peony$  <-输出结果 6w6Linux联盟
        peony$ 6w6Linux联盟
  6w6Linux联盟
  6w6Linux联盟
3.Shell Script编程 6w6Linux联盟
目的:使用UNIX所提供的最常用工具来完成所需复杂任务的强大功能。 6w6Linux联盟
  6w6Linux联盟
(1)最简单的Shell 编程 6w6Linux联盟
   $ls -R / |grep myname |more 6w6Linux联盟
  6w6Linux联盟
   每天数据的备份: 6w6Linux联盟
   $ cd /usr/yourname; ls * |cpio -o > /dev/rmt/0h 6w6Linux联盟
  6w6Linux联盟
   书写程序的目的是一次编程,多次使用(执行)! 6w6Linux联盟
  6w6Linux联盟
   $ cat > backup.sh 6w6Linux联盟
   cd /home/hbwork 6w6Linux联盟
   ls * | cpio -o > /dev/rmt/0h 6w6Linux联盟
   ^D 6w6Linux联盟
  6w6Linux联盟
   执行: 6w6Linux联盟
   $ sh backup.sh 6w6Linux联盟
  6w6Linux联盟
   或: 6w6Linux联盟
   $ chmod +x backup.sh 6w6Linux联盟
   $ ./backup.sh 6w6Linux联盟
  6w6Linux联盟
   技巧:在shell script中加入必要的注释,以便以后阅读及维护。 6w6Linux联盟
  6w6Linux联盟
(2)shell是一个(编程)语言 6w6Linux联盟
  同传统的编程语言一样,shell提供了很多特性,这些特性可以使你的shell script 6w6Linux联盟
  编程更为有用,如:数据变量、参数传递、判断、流程控制、数据输入和输出,子 6w6Linux联盟
  程序及以中断处理等。 6w6Linux联盟
  6w6Linux联盟
  . 在shell编程中使用数据变量可以将程序变量更为通用,如在上面backup.sh中: 6w6Linux联盟
    cd $WORKDIR 6w6Linux联盟
    ls * | cpio -o > /dev/rmt/0h 6w6Linux联盟
  6w6Linux联盟
  . Shell编程中的注释以#开头 6w6Linux联盟
  . 对shell变量进行数字运算,使用expr命令 6w6Linux联盟
        expr integer operator integer 6w6Linux联盟
        其中operator为+ - * / %, 但对*的使用要用转义符,如: 6w6Linux联盟
        $expr 4 * 5 6w6Linux联盟
        20 6w6Linux联盟
        $int=`expr 5 + 7` 6w6Linux联盟
        $echo $int 6w6Linux联盟
        12 6w6Linux联盟
  6w6Linux联盟
(3)Shell编程的参数传递, 可通过命令行参数以及交互式输入变量(read) 6w6Linux联盟
  6w6Linux联盟
     restoreall.sh 对backup.sh程序的备份磁带进行恢复 6w6Linux联盟
        $cat > restoreall.sh 6w6Linux联盟
        cd $WORKDIR 6w6Linux联盟
        cpio -i < /dev/rmt/0h 6w6Linux联盟
        ^D 6w6Linux联盟
     restore1.sh:只能恢复一个文件 6w6Linux联盟
        #restore1 --program to restore a single file 6w6Linux联盟
        cd $WORKDIR 6w6Linux联盟
        cpio -i $i < /dev/rmt/0h 6w6Linux联盟
  6w6Linux联盟
        $restore1 file1 6w6Linux联盟
  6w6Linux联盟
        恢复多个文件restoreany : 6w6Linux联盟
        #restoreany --program to restore a single file 6w6Linux联盟
        cd $WORKDIR 6w6Linux联盟
        cpio -i $* < /dev/rmt/0h 6w6Linux联盟
  6w6Linux联盟
        $ restoreany file1 file2 file3 6w6Linux联盟
  6w6Linux联盟
  6w6Linux联盟
(4)条件判断 6w6Linux联盟
   . if-then语句,格式如下: 6w6Linux联盟
        if command_1 6w6Linux联盟
        then 6w6Linux联盟
                command_2 6w6Linux联盟
                command_3 6w6Linux联盟
        fi 6w6Linux联盟
        command_4 6w6Linux联盟
  6w6Linux联盟
   在if-then语句中使用了命令返回码$?,即当command_1执行成功时才执行command_2和 6w6Linux联盟
   command_3,而command_4总是执行. 6w6Linux联盟
  6w6Linux联盟
   示例程序unload: 在备份成功时删除原始文件,带有错误检查 6w6Linux联盟
  6w6Linux联盟
        cd $1 6w6Linux联盟
        #备份时未考虑不成功的情况! 6w6Linux联盟
        ls -a | cpio -o > /dev/rmt/0h 6w6Linux联盟
        rm -rf * 6w6Linux联盟
  6w6Linux联盟
        改进如下: 6w6Linux联盟
  6w6Linux联盟
        #当使用了管道命令时,管理命令的返回代码为最后一个命令的返回代码 6w6Linux联盟
        if ls -a | cpio -o > /dev/rmt/0h 6w6Linux联盟
        then 6w6Linux联盟
                rm -rf * 6w6Linux联盟
        fi 6w6Linux联盟
  6w6Linux联盟
   . if-then-else语句 6w6Linux联盟
        if command_1 6w6Linux联盟
        then 6w6Linux联盟
                command_2 6w6Linux联盟
        else 6w6Linux联盟
                command_3 6w6Linux联盟
        fi 6w6Linux联盟
  6w6Linux联盟
        技巧: 由于shell对命令中的多余的空格不作任何处理,一个好的程序员会用这一特 6w6Linux联盟
6w6Linux联盟
              对自己的程序采用统一的缩进格式,以增强自己程序的可读性. 6w6Linux联盟
  6w6Linux联盟
    . 使用test命令进行进行条件测试 6w6Linux联盟
      格式: test conditions 6w6Linux联盟
  6w6Linux联盟
      test在以下四种情况下使用: a. 字符比较 b.两个整数值的比较 6w6Linux联盟
                c. 文件操作,如文件是否存在及文件的状态等 6w6Linux联盟
                d.
 2/12   |‹ ‹‹ 1 2 3 4 5 6 ›› ›|

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
    无相关信息
栏目更新
栏目热门