AIX环境变量简介及使用(2)

本地变量在用户当前的shell生命周期之内有效,结束当前进程,启动另一个进行,则此变量将无效。设置本地变量的格式:
variable-name = value 或者 {valiable-name=value}
注意,“=”左右可以有空格,若值有空格,则使用“”包含。
下面是实际变量模式
----------------------------------------------------------------------------
variable-name=value 设置实际值到variable-name
variable-name+value 如果设置了variable-name,则重置其值
variable-name:?value 如果未设置variable-name,显示未定义用户错误信息
variable-name?value 如果未设置variable-name,显示系统错误信息
variable-name:=value 如果未设置variable-name,则取value值,并将其赋给变量
variable-name:-value 如果未设置variable-name,则取value值,但不给变量赋值
----------------------------------------------------------------------------

1.设置变量并显示其值
TODAY="Thuesday"
echo ${TODAY}
Thuesday

MYINFO="my name is $LOGNAME,my home is $HOME"
echo ${MYINFO}
my name is xxxx,my home is /usr/xxxx

2.重置变量:unset variable-name
echo ${MYINFO}
my name is xxxx,my home is /usr/xxxx
unset MYINFO
echo ${MYINFO}

3.显示本地所有shell变量:set

4.连接变量:echo ${variable-name}${variable-name}
echo ${LOGNAME}${HOME}
xxxx/usr/xxxx

5.检查变量是否被设置
命令格式:${variable:-value}
意思:如果设置了variable变量的值,则取之,否则使用value,但是不做赋值:variable=value。
设置变量值:
TODYA="Thuesday"
显示变量:
echo "today is ${TODAY}"
today is Thuesday
重置变量:
unset TODAY
检查变量是否有值:
echo "today is ${TODAY:-unknow}"
today is unknow
查看TODAY的变量值:echo "today is ${TODAY}"
today is
这里显示TODAY变量现在已经没有包含任何值了。

6.下面这个命令,如果变量设置了值,则取之,否则取新值,并给变量赋新值:
${variable:=value}
设置变量值:
SMART_PHONE="iPhone5"
查看变量值:
echo "a smart phone is ${SMART_PHONE}"
a smart phone is iPhone5
重置变量值:
unset SMART_PHONE
检查变量是否有值,若没值,则给其赋值,并显示新值:
echo "a smart phone is ${SMART_PHONE:=Meizu MX2}"
a smart phone is Meizu MX2
查看变量的新值:
echo ${SMART_PHONE}
Meizu MX2

7.如果要检查变量是否设置了值,如果有值,则显示值,否则显示一个系统错误信息:
echo "${MEIZU_MX2:?} is a better than MI2"
[or echo "${MEIZU_MX2?} is a better than MI2"]
ksh: MEIZU_MX2: 0403-040 Parameter null or not set.

8.如果变量有值,我们可以重新给其赋值:
SMART_PHONE="iPhone 4s"
echo ${SMART_PHONE}
iPhone 4s
将变量的值修改了:
echo "a smart phone is ${SMART_PHONE+MX2}"
a smart phone is MX2

9.设置只读变量
格式:
variable-name=value
readonly variable-name
设置变量值:ENAME="Scott"
查看变量值:
echo "employee 001122 is ${ENAME}"
employee 001122 is Scott
设置变量只读:readonly ENAME
修改变量值:
ENAME="Amy"
ksh: ENAME: This variable is read only.
这里报错,变量ENAME为只读。

查看已经设置的只读变量值:
readonly
ENAME=Scott
*当前环境下,只读变量只有刚刚设置的这个ENAME。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/20357.html