zoukankan      html  css  js  c++  java
  • linux bash变量作用域

    linux bash变量作用域

    一,思考一个问题,当在shell里执行某个程序时,shell是怎么找到这个程序的?

    shell会去$PATH环境变量定义的目录里去找这个命令。环境变量里一般包括/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin这么多目录,这些目录下又有上千个程序,从这么多目录里的,这么多程序里查找肯定是花费时间的。shell为了提高查找效率,使用了缓存机制,这个机制叫hash

    有了hash缓存后,shell先去hash里查找,如果找到了,就使用;如果没有找到,才去$PATH环境变量定义的目录里去找,找到后,把命令的路劲加程序名放入hash中。

    注意:如果是shell的buildin命令,不会放入hash。

    • hash命令用法:

      • 查看hash里都缓存了哪些程序:hash

        hits:代表此命令,被命中了几次。

        # hash
        hits    command
           1    /usr/bin/cat
           2    /usr/bin/ls
        
      • 详细查看hash里都缓存了哪些程序:hash -l

        # hash -l
        builtin hash -p /usr/bin/cat cat
        builtin hash -p /usr/bin/ls ls
        
      • 清除某个缓存:hash -d cat

        # hash -d cat
        # hash
        hits    command
           2    /usr/bin/ls
        
      • 清除所有缓存:hash -r

        # hash -r
        # hash
        hash: hash table empty
        
      • 为什么还要清除缓存?理由:当把命令移动到别的目录后,用原来的缓存就找不到命令了,shell会报出错误,所有要清除。

    二,bash变量的作用域

    • shell进程的子孙进程。

      在shell进程里又启动了别的shell进程,下面的例子是在bash里又启动了一个bash,让后在新启动的bash里,又启动了一个csh。

      # pstree
      systemd─┬
              ├─sshd───sshd───bash───pstree
      # bash
      # csh
      # pstree
      systemd─┬
              ├sshd───sshd───bash───bash───csh───pstree
      
    • bash变量种类:

      • 按变量的作用域范围分为:

      • 本地变量:作用域仅为当前shell进程

        验证本地变量的作用域:

        # firstName=jerry
        # echo $firstName
        jerry
        # csh
        # echo $firstName
        firstName: Undefined variable.
        # exit
        exit
        # echo $firstName
        jerry
        
        • 赋值:name=value

        • 引用:${name},$name

          • "":变量会被替换成其值
          • '':变量不会被替换成其值
        • 查看变量:set

        • 撤销变量:unset name。注意name前不要加$

          # firstname=tom
          # echo $firstname
          tom
          # unset firstname
          # echo $firstname
          
          
      • 环境变量:作用域为当前shell进程,及其子孙shell进程。

        验证环境变量的作用域:

        # fn=tom
        # echo $fn
        tom
        # export fn
        # csh
        # echo $fn
        tom
        # exit
        exit
        # echo $fn
        tom
        
        • 赋值:

          • export name=value

          • name=value

            export name

          • declare -x name=value

          • name=value

            declare -x name

        • 引用:同本地变量。

        • 撤销变量:unset name。注意name前不要加$

        • bash内嵌的环境变量:

          PATH,HISTORY,HISTSIZE,HISTFILESIZE,HISTCONTROL,SHELL,HOME,UID,PWD,OLDPWD

        • 查看环境变量命令:

          export,declare -x,printenv,env

      • 只读变量(常量):作用域是当前shell进程,且不可以撤销。随当前shell进程的终止而终止。

        • declare -r name
        • readonly name

        检证常量的作用域:

        # la=foo
        # declare -r la
        # echo $la
        foo
        # csh
        # $echo la
        echo: Undefined variable.
        # exit
        exit
        # echo $la
        foo
        # la=aa
        -bash: la: readonly variable
        [root@localhost ~]# unset la
        -bash: unset: la: cannot unset: readonly variable
        
        • 局部变量:函数里的变量,作用域仅为函数内。
      • 位置参数变量:shell脚本的参数

      • 特殊变量:

        • $?:上一个命令的执行结果。0:成功;1-255:失败。
        • ...
    • bash里,多个命令一起执行

      • 多个命令连续执行:command1;command2;command3...

        这些命令都会被执行。

      • 多个命令逻辑连续执行

        根据前面命令的执行结果(成功或者失败),决定是否执行后面的命令。

        • 前一个命令执行成功后,后面的命令才让执行:&&

          例子:先查看目录存不存在,存在了才进入此目录

          # pwd
          /root
          # ls /sdf && cd /tmp
          ls: cannot access /sdf: No such file or directory
          # pwd
          /root
          # ls /sdf || cd /tmp
          ls: cannot access /sdf: No such file or directory
          # pwd
          /tmp
          
        • 前一个命令执行失败后,后面的命令才让执行:||

          例子:先某个用户存不存在,不存在则创建此用户;存在了就不创建了。

          # pwd
          /root
          # ls /sdf || cd /tmp
          ls: cannot access /sdf: No such file or directory
          # pwd
          /tmp
          

    三,如何执行shell脚本文件

    1,用chmod赋予shell文件可执行权限。

    ./shell.sh

    2,把shell文件(这个文件不需要有执行权限)作为参数,传给bash程序。

    bash shell.sh

    • 练习1

      1,显示/etc目录下所有以p(不区分大小写)开头的文件或者目录本身。

      2,显示/var目录下的所有文件或目录,并将显示结果中的小写字母转换为大写后显示

      3,创建临时文件/tmp/myfile.XXXX

      #!/bin/bash
      ls -ld /etc/p*
      ls -d /var/* | tr 'a-z' 'A-Z'
      mktemp /tmp/myfile.XXXX
      

    四,bash配置文件

    1,启动shell进程的种类:

    • 交互方式启动的shell进程
      • 直接通过终端输入账号密码后登录后,启动的shell进程
      • 使用su命令:su - username,或者使用su-l username,登录后,启动的shell进程
    • 非交互方式启动的shell进程
      • 使用su命令:su username,登录后,启动的shell进程
      • 在gnome或者kde下打开终端后,启动的shell进程
      • 执行shell脚本文件时,启动的shell进程

    2,bash配置文件的种类

    • profile类

      • 对所有用户都有效,只有管理员才可以修改下面的配置文件

        • /etc/profile
        • /etc/profile.d/*.sh
      • 仅对当前用户有效

        ~/.bash_profile

      • 功用:

        • 用于定义环境变量
        • 运行命令或脚本
    • bashrc类

      • 对所有用户都有效,只有管理员才可以修改下面的配置文件

        /etc/bashrc

      • 仅对当前用户有效

        ~/.bashrc

      • 功用:

        • 定义本地变量
        • 定义命令别名

    3,bash配置文件的加载顺序。

    • 交互方式启动的shell进程

      /etc/profile--->/etc/profile.d/*.sh--->/.bash_profile--->/.bashrc--->/etc/bashrc

    • 非交互方式启动的shell进程

      ~/.bashrc--->/etc/bashrc--->/etc/profile.d/*.sh

    4,重新加载配置文件

    • 当修改配置文件后,是不会立即生效的
    • 使用source 配置名,让修改的配置立即生效

    五,用emacs编写shell脚本

    1,自动补全的快捷键:ESC TAB

    # c/c++ 学习互助QQ群:877684253 ![](https://img2018.cnblogs.com/blog/1414315/201811/1414315-20181106214320230-961379709.jpg) # 本人微信:xiaoshitou5854
  • 相关阅读:
    编译内核时出现drivers/mfd/mxchdmicore.c:36:24: fatal error: mach/clock.h: No such file or directory
    IE中iframe标签显示在DIV之上的问题解决方案
    Linux驱动学习1.hello world;
    Maven安装与配置(转)
    Jmeter阶梯式压测
    Jmeter的分布式测试
    adb connect命令连接多个Android设备
    Linux当中文件的显示命令
    软件测试流程
    测试时间不够,该怎么办?
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/12066585.html
Copyright © 2011-2022 走看看