gqinvs 发表于 2018-8-29 09:37:54

shell学习之常用bash内置变量

  常用的Bash内置变量
  1 BASH_COMMAND当前执行的命令
  2 LINENO显示当前所在行号
  /bin/bash
  echo "this is test about ARGLINENO"
  echo "line now in :$LINENO"
  结果为
  # ./bash.sh
  this is test about ARG LINENO
  line now in :   4
  方便调试
  3 FUNCNAME在第几层函数
  #!/bin/bash
  #
  function test1(){
  echo "test1:FUNCNAME is${FUNCNAME}"
  echo "test1:FUNCNAME is${FUNCNAME}"
  echo "test1:FUNCNAME is${FUNCNAME}"
  test2
  }
  function test2(){
  echo "test2:FUNCNAME is${FUNCNAME}"
  echo "test2:FUNCNAME is${FUNCNAME}"
  echo "test2:FUNCNAME is${FUNCNAME}"
  }
  test1
  运行结果为
  liuliancao@liuliancao-K45VD:~/shell$ ./bash1.sh
  test1:FUNCNAME is test1
  #此时第一层func为本函数,所以是test1
  test1:FUNCNAME is main
  #第二层为二层函数,应该为main,称为主脚本,调用test1的函数
  test1:FUNCNAME is
  #再下层就没有了,下面的同理
  test2:FUNCNAME is test2
  test2:FUNCNAME is test1
  test2:FUNCNAME is main
  #这里可以方便调试
  4 HOSTNAME   HOSTTYPE    HOMEUIDGROUPS
  这些大家应该都比较熟悉,我们看一下我的主机中它们的值
  liuliancao@liuliancao-K45VD:~/shell$ echo$HOSTNAME;echo $HOSTTYPE;echo $HOME;echo $GROUPS
  liuliancao-K45VD
  x86_64
  /home/liuliancao
  1000
  liuliancao@liuliancao-K45VD:~/shell$ echo $UID
  1000
  5 OLDPWD上个工作目录PWD 当前工作目录
  liuliancao@liuliancao-K45VD:~/shell$ echo$PWD;echo $OLDPWD
  /home/liuliancao/shell
  /etc
  6 $$ SHELL本身的PID
  liuliancao@liuliancao-K45VD:~/shell$ echo $$
  22535
  liuliancao@liuliancao-K45VD:~/shell$ ps -e |grep bash
  22535 pts/6    00:00:00 bash
  7 RANDOM 随机数生成器
  #random.sh
  #!/bin/bash
  echo "this is a random nu from 0 to 30"
  scope=30
  num1="`expr $RANDOM % $scope + 0`"
  num2="`expr $RANDOM % $scope + 0`"
  echo "nu1: ${num1}"
  echo "nu2: ${num2}"
  运行结果为:
  liuliancao@liuliancao-K45VD:~/shell$./random.sh
  this is a random nu from 0 to 30
  nu1: 20
  nu2: 8
  liuliancao@liuliancao-K45VD:~/shell$./random.sh
  this is a random nu from 0 to 30
  nu1: 24
  nu2: 15
  #相当实用的
  8 REPLY 当read没有变量名时此时,内容保存在该变量中
  #reply.sh
  #!/bin/bash
  read -p "no arg test:"
  echo "\$REPLY is$REPLY"
  结果为:
  liuliancao@liuliancao-K45VD:~/shell$ ./reply.sh
  no arg test:hello ok
  $REPLY ishello ok
  #说不定什么时候用到这个变量
  9 SECONDS脚本运行的时间
  #seconds.sh
  #!/bin/bash
  sleep 2
  echo "script run $SECONDS already!"
  结果为
  liuliancao@liuliancao-K45VD:~/shell$./seconds.sh
  script run 2 already!
  #可以很方便地测试脚本运行性能呢
  10GLOBIGNORE对通配符忽略某些通配模式
  ~/shell文件夹下有如下的文件
  liuliancao@liuliancao-K45VD:~/shell$ ls -al
  total 24
  drwxrwxr-x2 liuliancao liuliancao 40965月20 19:40 .
  drwxr-xr-x 45 liuliancao liuliancao 40965月20 19:37 ..
  -rw-rw-r--1 liuliancao liuliancao    05月20 19:40 1.txt
  -rw-rw-r--1 liuliancao liuliancao    05月20 19:40 2.html
  -rw-rw-r--1 liuliancao liuliancao    05月20 19:40 3.c
  -rwxrwxr-x1 liuliancao liuliancao3245月20 19:02 bash1.sh
  -rwxrwxr-x1 liuliancao liuliancao1825月20 19:28 random.sh
  -rwxrwxr-x1 liuliancao liuliancao   715月20 19:36 reply.sh
  -rwxrwxr-x1 liuliancao liuliancao   685月20 19:37 seconds.sh
  -rw-rw-r--1 liuliancao liuliancao    05月20 19:40 .xmind
  如果我们修改下
  liuliancao@liuliancao-K45VD:~/shell$GLOBIGNORE=*.txt
  liuliancao@liuliancao-K45VD:~/shell$ ls *
  2.html3.cbash1.shrandom.shreply.sh seconds.sh.xmind
  liuliancao@liuliancao-K45VD:~/shell$ rm *.txt
  rm: cannot remove ‘*.txt’: No such file ordirectory
  #hah,成功隐藏了文件,不过只是对通配符有关,其实我们可以用来对制定文件夹内的一些满足某种模式的进行忽略,其他的进行自己想要的操作,上面的就可以删除除了.txt的文件,修改为rm*
  11 IFS (Internal Field Separator)空格分隔的内容
  #ifs.sh
  #!/bin/bash
  #save orign IFS,and set it to :
  OIFS=$IFS
  IFS=":"
  #next manage to read the val with separator :
  read -p "input something with separator :"h1 h2 h3
  echo "h1:${h1}"
  echo "h2:${h2}"
  echo "h3:${h3}"
  #return to orign
  IFS=$OIFS
  运行结果为
  liuliancao@liuliancao-K45VD:~/shell$ ./ifs.sh
  input something with separator :liuliancao :qixue : linux
  h1:liuliancao
  h2: qixue
  h3: linux
  12 PATH 执行文件查找路径
  通常命令执行前会去PATH变量按顺序查找是否有该命令,找到就执行
  第一次搜索完这个命令,系统就会把命令的路径保存在hash表中,方便以后实用,实用hash-r可以清空hash表
  关于PATH如果PATH开头有一个:是挺危险的,它会扩展为当前目录,此时目录下如果有一个ls命令,则可获得root的权限
  13 TMOUT 用于readselect 交互式shell,为其指定超时时间
  #tmout.sh
  #!/bin/bash
  TMOUT=3
  echo "you only have 3 seconds to input "
  read -p"hah,just a test:" test
  echo "you entered $test"
  结果为
  liuliancao@liuliancao-K45VD:~/shell$ ./tmout.sh
  you only have 3 seconds to input
  hah,just a test:you entered
  最后补充一下
  SHELLOPTS的一些
  对应一个命令shopt
  -p 表示print显示 -u 表示停止选项 -s 表示启用选项
  liuliancao@liuliancao-K45VD:~/shell$ shopt -p
  shopt -u autocd
  shopt -u cdable_vars#cd参数可以是变量
  shopt -u cdspell#纠正拼写错误
  shopt -u checkhash#执行命令先在hash表中查找
  shopt -u checkjobs#
  shopt -s checkwinsize#检查窗口大小,自动更新lines和column
  shopt -s cmdhist#多行命令保存为一行存在history
  shopt -u compat31
  shopt -u compat32
  shopt -u compat40
  shopt -u compat41
  shopt -u compat42
  shopt -s complete_fullquote
  shopt -u direxpand
  shopt -u dirspell
  shopt -u dotglob
  shopt -u execfail#交互式shell不会因为exec内置命令不能执行而退出
  shopt -s expand_aliases#别名被扩展
  shopt -u extdebug
  shopt -s extglob#打开扩展通配
  shopt -s extquote
  shopt -u failglob
  shopt -s force_fignore
  shopt -u globstar
  shopt -u globasciiranges
  shopt -u gnu_errfmt
  shopt -shistappend#shell退出,历史清单会添加到histfile文件,而不是覆盖
  shopt -u histreedit
  shopt -u histverify
  shopt -u hostcomplete
  shopt -u huponexit
  shopt -s interactive_comments#表示#开头的语句可以被忽略
  shopt -u lastpipe
  shopt -u lithist
  shopt -u login_shell
  shopt -u mailwarn
  shopt -u no_empty_cmd_completion
  shopt -u nocaseglob
  shopt -u nocasematch
  shopt -u nullglob
  shopt -s progcomp
  shopt -s promptvars
  shopt -u restricted_shell
  shopt -u shift_verbose
  shopt -ssourcepath#如果设置,source内置命令使用PATH的值来寻找参数
  shopt -u xpg_echo
  总结其实bash内置变量表面上看来挺枯燥,其实还是挺有意思,需要自己去体会哦。

页: [1]
查看完整版本: shell学习之常用bash内置变量