• 赋值语句=前后不能有空格
  • 算数表达式使用expr

    // 1+2
    // 注意运算符前后必须要有空格
    expr 1 + 2
    expr 2 - 1
    expr 10 / 5
    expr 20 % 3
    // *是特殊字符,所以要转义
    expr 10 \* 3
    
  • 反引号用来执行被反引号括起来的命令

    result=`expr 6 + 3`
    echo $result
    
  • 单引号和双引号,与PHP一样,单引号中的变量会被当成字符串,双引号中的变量会被替换成相应的变量

    str=23
    // Str is 23
    echo "Str is $str"
    // Str is $str
    echo 'Str is $str'
    
  • 关于exit 1还是exit 0. 每条命令执行完后都有一个退出码(exit status),Linux中退出码为0表示命令执行成功,不为0表示命令执行失败。使用$?来获取前一条命令的执行状态

    rm unknownfile
    // 1
    echo $?
    ls
    // 0
    echo $?
    
  • 读取键盘输入

    echo 'Input your name:'
    read name
    echo "Hi $name"
    
  • if语法

    if condition
    then
        command1 if condition is true or if exit status
        of condition is 0 (zero)
        ...
        ...
    fi
    

    或者是

       if condition
       then
                   condition is zero (true - 0)
                   execute all commands up to else statement
    
       else
                   if condition is not true then
                   execute all commands up to fi
       fi
    

    或者是

       if condition
       then
                   condition is zero (true - 0)
                   execute all commands up to elif statement
       elif condition1 
       then
                   condition1 is zero (true - 0)
                   execute all commands up to elif statement  
       elif condition2
       then
                   condition2 is zero (true - 0)
                   execute all commands up to elif statement          
       else
                   None of the above condtion,condtion1,condtion2 are true (i.e. 
                   all of the above nonzero or false)
                   execute all commands up to fi
       fi
    
  • test命令

    数字比较

    Mathematical Operator in  Shell Script  Meaning Normal Arithmetical/ Mathematical Statements But in Shell
          For test statement with if command For [ expr ] statement with if command
    -eq is equal to 5 == 6 if test 5 -eq 6 if [ 5 -eq 6 ]
    -ne is not equal to 5 != 6 if test 5 -ne 6 if [ 5 -ne 6 ]
    -lt is less than 5 < 6 if test 5 -lt 6 if [ 5 -lt 6 ]
    -le is less than or equal to 5 <= 6 if test 5 -le 6 if [ 5 -le 6 ]
    -gt is greater than 5 > 6 if test 5 -gt 6 if [ 5 -gt 6 ]
    -ge is greater than or equal to 5 >= 6 if test 5 -ge 6 if [ 5 -ge 6 ]

    字符串比较

    Operator Meaning
    string1 = string2 string1 is equal to string2
    string1 != string2 string1 is NOT equal to string2
    string1 string1 is NOT NULL or not defined 
    -n string1 string1 is NOT NULL and does exist
    -z string1 string1 is NULL and does exist

    文件或者目录

    Test Meaning
    -s file    Non empty file
    -f file    Is File exist or normal file and not a directory 
    -d dir     Is Directory exist and not a file
    -w file   Is writeable file
    -r file    Is read-only file
    -x file    Is file is executable
  • for语法

        for { variable name } in { list }
        do
                 execute one for each item in the list until the list is
                 not finished (And repeat all statement between do and done)
        done
    

    for ((  i = 0 ;  i <= 5;  i++  ))
    do
      echo "Welcome $i times"
    done
    
  • while语法

       while [ condition ]
       do
             command1
             command2
             command3
             ..
             ....
        done
    

    例子

    i=0
    while [ $i -lt 10 ]
    do
        echo $i
        i=`expr $i + 1`
    done
    
  • case语法, 注意每个语句结束是两个;;

       case  $variable-name  in
        pattern1)   command
                        ...
                        ..
                        command;;
        pattern2)   command
                        ...
                        ..
                        command;;
        patternN)   command
                        ...
                        ..
                        command;;
        *)             command
                        ...
                        ..
                        command;;
       esac
    

niklausxiao
61 声望1 粉丝