xiaui520 发表于 2018-8-29 09:20:56

Advanced Bash-Shell Guide(Version 10) 学习笔记二

  变量替换
  $variable 是 ${variable}的简写
  39 hello="A B C D"
  40 echo $hello # A B C D
  41 echo "$hello" # A B C D
  引号保留变量里面的空白
  1 echo "$uninitialized" # (blank line)
  2 let "uninitialized += 5" # Add 5 to it.
  3 echo "$uninitialized"
  未初始化的变量是null,但是在算数表达式中等于0
  命令替换
  17 a=`ls -l`   # Assigns result of 'ls -l' command to 'a'
  18 echo $a
  或
  a=$(ls -l)
  echo $a
  字符串+整数等于整数,字符串相当于0
  但是在除以一个null变量的时候,系统报错。
  变量类型
  本地变量
  在代码块可见
  环境变量
  影响shell和用户接口的变量
  位置参数
  取最后的参数
  1 args=$# # Number of args passed.
  2 lastarg=${!args}
  或
  lastarg=${!#}
  [...]和``.``.``.``
  使用``.``.``.``比起[]可以阻止在脚本中常见的逻辑错误
  例如:&&, ||,操作
  在进行算数运算的时候,``.``.``.``会将八进制和十六进制进行自动计算而[]
  则不可以
  5 decimal=15
  6 octal=017 # = 15 (decimal)
  7 hex=0x0f # = 15 (decimal)
  8
  9 if [ "$decimal" -eq "$octal" ]
  10 then
  11 echo "$decimal equals $octal"
  12 else
  13 echo "$decimal is not equal to $octal"    # 15 is not equal to 017
  14 fi    # Doesn't evaluate within [ single brackets ]!
  17 if [[ "$decimal" -eq "$octal" ]]
  18 then
  19 echo "$decimal equals $octal"    # 15 equals 017
  20 else
  21 echo "$decimal is not equal to $octal"
  22 fi # Evaluates within ` double brackets `!
  ((...))算数测试
  如果表达式计算是0,则它的退出状态码是1或false
  如果表达式计算是非0,则它的退出状态码是0或true
  它的退出状态和[...]相反
  echo ${aa##*/}    获取变量aa的文件名称 以/位分隔符
  ${filename##*.} != "gz"
  filename##*.    获取filename的扩展名
  Infinite Monkeys
  AppMakr
  字符串操作
  获取字符串长度
  ${#string}
  expr length $string
  expr "$string" : '.*'
  获取从字符串开头匹配的字符串的长度
  expr match "$string" '$substring'
  expr "$string" : '$substring'
  $substring is a regular expression
  获取子串在字符串中开始的位置
  expr index $string $substring
  取出子串
  ${string:position}
  ${string:position:length}

页: [1]
查看完整版本: Advanced Bash-Shell Guide(Version 10) 学习笔记二