使用环境变量

什么是变量?

        变量就是一些字符串被赋予了一个特定的值,该值是动态的,可以被修改,变量的定义会影响了程序运行的方式。

        所有用户的进程在内存中都有一个PEB(process environment block),在其中定义了环境变量。当一个新的进程被创建时,默认情况下是将环境变量从父进程的PEB那里拷贝到自己的PEB中。

变量可以分为两种:

      普通变量/本地变量:

        普通变量的值作用范围只限于当前的shell中,任何由当前shell所衍生的新shell或进程都不能继承该变量,如我们常用的:

      # ORACLE_SID=rac

      # echo $ORACLE_SID

     rac

     看到当前shell的变量定义生效,然后在当前shell下新生成一个shell:

#bash

#echo $ORACLE_SID

#exit

#echo $ORACLE_SID

rac

#echo "echo \$ORACLE_SID" > test.sh

#sh test.sh

-->输出为空行


      环境变量:

        用户的环境变量的值能继承给任何由当前shell所衍生的新shell或者进程,可以使用export命令将普通变量导出到环境变量:

       如上例:

#export ORACLE_SID

#echo $ORACLE_SID

rac

#bash

#echo $ORACLE_SID

rac

# sh test.sh
rac
        也可以使用export ORACLE_SID=rac直接进行定义或者更改环境变量


        不同SHELL类型,定义变量的方式不同,上述方式用于我们常用的bash和ksh等

        而对于 c-shell/tc-shel则使用set命令定义普通变量,setenv定义环境变量,如下:

# csh

# ORACLE_SID=test

ORACLE_SID=test: Command not found.

# set ORACLE_SID=test

#echo $ORACLE_SID

test

#csh

#echo $ORACLE_SID

ORACLE_SID: Undefined variable.


# setenv ORACLE_SID test2

# echo $ORACLE_SID

test2

# csh

# echo $ORACLE_SID

test2

如何查看变量?

         通常我们可以使用set、env、export(只作用于Bash和Ksh等个别Shell类型)

        set:查看用户的所有变量,包括普通变量和环境变量

        env:查看用户的环境变量,bash或者ksh中用于定义程序的环境变量

        export (-p):查看导出到环境变量的普通变量,并显示导出的命令


如何调用变量?

         所有的变量,不能直接调用变量名进行操作,原因是shell会认为它是一个字符串,比如:

# NUM=123

# printf "%-10s\n" NUM

NUM

          变量的调用需要在前面加上特殊字符"$",如果变量名后面直接跟字符的话需要用{},为了不与其他变量混淆,如:

# printf "%-10s\n" $NUM

123

# printf "%-10s\n" $NUM9

-->输出为空,shell认为NUM9是一个变量,但是没有赋值

# printf "%-10s\n" ${NUM}9

1239 


如何删除变量?

#echo $NUM

123

#unset NUM

#echo $NUM

系统环境变量

         环境变量有的是系统的SHELL自带的,不同的SHELL自带的环境变量各有不同;也可以通过用户自己定义;有的也可以通过程序继承。

        常见的重要环境变量有:

DISPLAY             指定一个显示设备,所有的图形程序都将把自己显示到这个设备上

HOME                指定当前用户的主目录,默认值为“cd”命令切换的目录

LANG                指定区域语言环境,该值会被变量LC_ALL所覆盖

SHELL                指定当前shell使用的类型

LD_LIBRARY_PATH        查找共享库(动态链接库)时除了默认路径之外的其他路径。(该路径在默认路径之前查找)

PATH                指定命令的搜索引路径。

PWD                指定当前工作目录

SHLVL                指定shell实例的启动个数,这个参数可以确定当前exit命令会不会退出连接会话

TERM                 指定显示类型

TZ                 指定时区

LOGNAME            指定当前登录用户名

HISTSIZE            指定历史命令条数

变量PS

        重点介绍一下变量PS1、PS2、PS3、PS4

        PS1: 主要提示符,进入shell界面所看到的提示符。bash默认提示符为\s-\v\$,ksh默认root用户为#,其他用户为$.

         bash下可以使用转移符自己定义PS1,主要有:

   

 \a : an ASCII bell character (07)

    \d : the date in "Weekday Month Date" format (e.g., "Tue May 26")

    \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific                       time representation. The braces are required

    \e : an ASCII escape character (033)

    \h : the hostname up to the first '.'

    \H : the hostname

    \j : the number of jobs currently managed by the shell

    \l : the basename of the shell’s terminal device name

    \n : newline

    \r : carriage return

    \s : the name of the shell, the basename of $0 (the portion following the final slash)

    \t : the current time in 24-hour HH:MM:SS format

    \T : the current time in 12-hour HH:MM:SS format

    \@ : the current time in 12-hour am/pm format

    \A : the current time in 24-hour HH:MM format

    \u : the username of the current user

    \v : the version of bash (e.g., 2.00)

    \V : the release of bash, version + patch level (e.g., 2.00.0)

    \w : the current working directory, with $HOME abbreviated with a tilde

    \W : the basename of the current working directory, with $HOME abbreviated with a tilde

    \! : the history number of this command

    \# : the command number of this command

    \$ : if the effective UID is 0, a #, otherwise a $

    \nnn : the character corresponding to the octal number nnn

    \\ : a backslash

    \[ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt

    \] : end a sequence of non-printing characters 

        如:

[root@dgtest ~]# PS1="[\t \u@\h:\w]"

[15:42:51 root@dgtest:~]pwd

/root


        PS2: 当在命令行上输入反斜杠"\"时,告诉shell命令没用输完,下一行继续,此时出现的提示符便是PS2,默认是"> "

[root@dgtest ~]for i in 1 2 3

> do

> echo $i

> done

1

2

3

        设置PS2="==>"

[root@dgtest ~]for i in 1 2 3

==> do

==> echo $i

==> done

1

2

3


        PS3: 主要应用于ksh的select命令提示符,默认提示符为"#? "

#echo $PS3

-->bash中没有定义PS3

# ksh

# echo $PS3

#?

创建一个select脚本

# cat >>test.sh<<EOF
> select i in one two three exit
> do
>     case \$i in
>         one) echo \"number none!\";;
>         two) echo \"number two!\";;
>         three)  echo \"number three!\";;
>         exit) exit;;
>     esac
> done
> EOF

执行脚本

# sh test.sh

1) one

2) two

3) three

4) exit

#? 1

"number none!"

#? 3

"number three!"

#? 4

        给PS3赋予一个自己定义的值如:

# export PS3="Enter one number(1-4):"

# sh test.sh

1) one

2) two

3) three

4) exit

Enter one number(1-4):1

"number none!"

Enter one number(1-4):2

"number two!"

Enter one number(1-4):4


          PS4: 表示使用"set +x"或者"sh -x"跟踪DEBUG输出时的提示符,默认提示符为"+ "如:

# sh -x test.sh

+ select i in one two three exit

1) one

2) two

3) three

4) exit

#? 1

+ case $i in

+ echo '"number' 'none!"'

"number none!"

        给PS4赋予一个自己定义的值如:

# export PS4="Debug:)"

# sh -x test.sh

Debug:)select i in one two three exit

1) one

2) two

3) three

4) exit

#? 3

Debug:)case $i in

Debug:)echo '"number' 'three!"'

"number three!"

#? 4

Debug:)case $i in

Debug:)exit

#


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值