Linux中的shell语句变量编程


Linux中的shell语句变量编程
 
shell变量
      1:两类变量:临时变量和永久变量
           临时变量是shell程序内部定义的,适用范围仅限于程序内部,对其他程序不可见。包括:用户自定义变量,位置变量。永久变量是环境变量,其值不随shell脚本的执行结束而消失。
      2:用户自定义变量要以字母或下划线开头,由字母,数字,下划线组成。在使用变量时,要在变量前面加"$"符号。
      3:位置变量和特殊变量
                   shell解释执行用户命令时,将执行命令的第一个部分作为命令名,其他部分作为参数。由出现在命令行上的位置确定的参数称为位置参数。
          例如:
                  ls  -l  file1 file2  file3
                $0这个程序的文件名 ls -l
                $n 这程序的第n个参数值,n=1-9
       4:特殊变量
               $*  这个程序的所有的参数
               $#   这个程序的参数的个数
               $$ 这个程序的PID
               $!  执行上一个后台命令的PID
                $?  执行上一个命令的返回值
       5:shell命令
            read命令:从键盘读入数据,付给变量。
           如:read  NAME
            expr命令:对整数型变量进行算数运算
           如:expr  3 + 5  之间要有空格否则以字符的形式表示出来
                  expr  $var1 / $var2   同上
                  expr  $var1 \* 10  乘法要用到转义字符"\"
            复杂的运算:expr  `expr 5 + 7` + 3   可以用命令替换符
       6:变量测试语句:
          test      测试条件
           1>字符串测试:
                      test  str1=str2   测试字符串是否相等
                      test  str1 != str2 测试字符串是否不相等
                      test   str1           测试字符串str1是否不为空
                      test    -n  str1      测试字符串是否不为空
                      test    -z   str1     测试字符串是否为空
            2>整数测试:
                      test   int1   -eq   int2  测试整数是否相等
                      test   int1   -ge    int2  测试int1是否>=int2
                      test   int1   -gt     int2   测试int1是否> int2
                      test   int1   -le     int2    测试int1是否 <= int2
                      test   int1   -lt     int2    测试int1是否< int2
                      test   int1    -ne   int2   测试int1和int2是否不相等
             3>文件测试:
                      test  -d  file  指定的问件是否为目录
                      test   -f file    指定的文件是否为常规的文件
                      test   -x file   指定的文件是否可执行
                      test   -r  file   指定的文件是否可读
                      test   -w  file   指定的文件是否可写
                      test   -a  file   指定的文件是否存在
                      test   -s  file    指定的文件大小是否非0
变量测试语句一般不单独使用,一般作为if语句的测试条件。
例如:
          if      test   -d   $1  then
          fi
变量测试语句可用[] 进行简化,如
test -d $1  等价于  [ -d $1 ] (注意括号两边的空格)
 
     7:流程控制语句
            多个条件的联合:
          -a   逻辑与,当且仅当两个条件都成立时,结果为真
         -o    逻辑或,两个条件只要有一个条件成立,结果为真。
       例如: elif [ -c $file_name -o -b $file_name ]   
                     then                                                     (注意测试语句内的空格)
      
        一个实际的例子:
                   #/bin/sh
                   if  [ $#  -ne 2 ]; then
                          echo  "Not enough parameters"
                          exit 0    #  0表示程序正常的退出
                   fi
                   if  [ $1 -eq $2 ]; then
                          echo "$1 equals $2"            #注意双引号和单引号的区别
                   elif  [ $1 -lt  $2 ]; then
                           echo  "$1 littler than $2"
                   elif  [ $1 -gt  $2 ]; then
                            echo "$1 greater than $2"
                    fi
        for ....done语句
                 例子:(剔除某一个在线的用户)
                 #!/bin/sh
                #the script to kill logined user
                    username="$1"
                    /bin/ps  aux |  /bin/grep $username | /bin/awk '{ print $2 }'  > /tmp/tmp.pid
                    killid = `cat /tmp/tmp.pid`
                    for PID in $killid
                    do
                            /bin/kill  -9  $PID 2> /dev/null
                    done

相关内容

    暂无相关文章