设为首页 收藏本站
查看: 1046|回复: 0

shell学习之常用bash内置变量

[复制链接]

尚未签到

发表于 2018-8-29 09:37:54 | 显示全部楼层 |阅读模式
  常用的Bash内置变量
  1 BASH_COMMAND当前执行的命令
  2 LINENO显示当前所在行号
  /bin/bash
  echo "this is test about ARGLINENO"
  echo "line now in :$LINENO"
  结果为
  [root@www shell]# ./bash.sh
  this is test about ARG LINENO
  line now in :   4
  方便调试
  3 FUNCNAME在第几层函数
  #!/bin/bash
  #
  function test1(){
  echo "test1:FUNCNAME[0] is${FUNCNAME[0]}"
  echo "test1:FUNCNAME[1] is${FUNCNAME[1]}"
  echo "test1:FUNCNAME[2] is${FUNCNAME[2]}"
  test2
  }
  function test2(){
  echo "test2:FUNCNAME[0] is${FUNCNAME[0]}"
  echo "test2:FUNCNAME[1] is${FUNCNAME[1]}"
  echo "test2:FUNCNAME[2] is${FUNCNAME[2]}"
  }
  test1
  运行结果为
  liuliancao@liuliancao-K45VD:~/shell$ ./bash1.sh
  test1:FUNCNAME[0] is test1
  #此时第一层func为本函数,所以是test1
  test1:FUNCNAME[1] is main
  #第二层为二层函数,应该为main,称为主脚本,调用test1的函数
  test1:FUNCNAME[2] is
  #再下层就没有了,下面的同理
  test2:FUNCNAME[0] is test2
  test2:FUNCNAME[1] is test1
  test2:FUNCNAME[2] is main
  #这里可以方便调试
  4 HOSTNAME   HOSTTYPE    HOME  UID  GROUPS
  这些大家应该都比较熟悉,我们看一下我的主机中它们的值
  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 is  hello ok
  #说不定什么时候用到这个变量
  9 SECONDS脚本运行的时间
  #seconds.sh
  #!/bin/bash
  sleep 2
  echo "script run $SECONDS already!"
  结果为
  liuliancao@liuliancao-K45VD:~/shell$./seconds.sh
  script run 2 already!
  #可以很方便地测试脚本运行性能呢
  10  GLOBIGNORE对通配符忽略某些通配模式
  ~/shell文件夹下有如下的文件
  liuliancao@liuliancao-K45VD:~/shell$ ls -al
  total 24
  drwxrwxr-x  2 liuliancao liuliancao 4096  5月20 19:40 .
  drwxr-xr-x 45 liuliancao liuliancao 4096  5月20 19:37 ..
  -rw-rw-r--  1 liuliancao liuliancao    0  5月20 19:40 1.txt
  -rw-rw-r--  1 liuliancao liuliancao    0  5月20 19:40 2.html
  -rw-rw-r--  1 liuliancao liuliancao    0  5月20 19:40 3.c
  -rwxrwxr-x  1 liuliancao liuliancao  324  5月20 19:02 bash1.sh
  -rwxrwxr-x  1 liuliancao liuliancao  182  5月20 19:28 random.sh
  -rwxrwxr-x  1 liuliancao liuliancao   71  5月20 19:36 reply.sh
  -rwxrwxr-x  1 liuliancao liuliancao   68  5月20 19:37 seconds.sh
  -rw-rw-r--  1 liuliancao liuliancao    0  5月20 19:40 .xmind
  如果我们修改下
  liuliancao@liuliancao-K45VD:~/shell$GLOBIGNORE=*.txt
  liuliancao@liuliancao-K45VD:~/shell$ ls *
  2.html  3.c  bash1.sh  random.sh  reply.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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-558012-1-1.html 上篇帖子: shell 脚本分析Nginx 日志 下篇帖子: Shell随机重命名所有当前目录一级子目录
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表