环境配置可分为以下:变量类型及作用范围、变量命名(定义)、配置文件理解、撤销变量、查看(定义)变量、shell登录类型及配置文件作用顺序

    1、变量类型及作用范围

       环境变量:作用范围当前shell进程及其子进程

       本地变量:作用范围当前shell进程

       局部变量:作用范围仅为当前shell进程中某代码片断(通常为函数上下文)

       位置变量:$1, $2

       特殊变量:

               $? 上一个命令执行状态返回值

               $# 参数的个数

               $* 参数列表

               $@ 参数列表

               $0 命令本身、脚本本身

     2、变量命名(定义)

       变量命名遵循以下法则:

       --只能包含字母、数字和下划线,并且不能以数字开头,

       --不应该跟系统中已有的环境变量重名

       --见名知意

       变量赋值:变量名=值

     如:var_name=value 等号两边不能有空格

   image

         注:变量和引号的关系:

       单引号'':强引用,变量替换不会发生

       双引号"":使特殊符号生效

       反引号``:引用命令

    image

 

     3、配置文件理解

       按范围划分 :

全局配置:

   /etc/profile, /etc/profile.d/*.sh

   /etc/bashrc

个人配置:

   ~/.bash_profile

   ~/.bashrc

按功能类别划分:

profile类:为交互式登录的shell提供配置

/etc/profile, /etc/profile.d/*.sh

      ~/.bash_profile

bashrc类:为非交互式登录shell提供配置

/etc/bashrc

~/.bashrc

      4、撤销变量

         unset name

       image

 

       注:执行状态返回代码(0-255):0表示正确;1-255:错误执行 1,2,127 系统预留,有特殊意义

      5、查看(定义)变量

        查看本地变量:set

image

        定义环境变量:export name=value或declare -x name=value

        示例:export PATH=$PATH:/usr/local/apsch/bin

 

        查看环境变量:env, printenv, export

image

       6、shell登录类型及配置文件作用顺序

shell登录类型作用次序:

   交互式登录:/etc/profile --> /etc/profile.d/*.sh --> ~/.bash_profile --> ~/.bashrc --> /etc/bashrc

直接通过终端进行的登录;

通过su -l Username命令实现的用户切换;

    非交互式登录:~/.bashrc --> /etc/bashrc --> /etc/profile.d/*.sh

切换普通用户

su Username;

练习:


        1、让普通用户能使用/tmp/cat去查看/etc/shadow文件;

image

image

image
        2、创建目录/test/data,让某组内普通用户对其有写权限,且创建的所有文件的属组为目录所属的组;此外,每个用户仅能删除自己的文件;

image

image

image

image