37.调整变量的属性
- 只读变量:使用readonly命令
readonly 变量名称
- readonly其他用法
- readonly或readonly -p:显示目前具有只读属性的变量列表
- readonly -f 函数名称:设定该函数式不可修改
- readonly -a 数组变量:设定-a后接的变量名是只读的数组
- 只读变量:使用declare
38.自定义环境变量
- 以一般用户登录时
- 以系统管理员的角色工作时,环境变量配置文件有三个
- /etc/profile
- /etc/bash.bashrc
- /etc/skel目录下的所有文件
39.Here Document
基本用法:
[[email protected] ~]# cat >>123.txt<< end > hello > my name is "jack" > welcome to BJ > end You have new mail in /var/spool/mail/root [[email protected] ~]# cat 123.txt hello my name is "jack" welcome to BJ
- Here Document也支持变量替换.在输入的内容中,如果有变量,bash在转向前,会先替换变量值
- 如何在变量中使用
[[email protected] ~]# Wow=‘Wow,great!‘ [[email protected] ~]# m1=$(cat <<HERE > line 1 is good. > They are jack,marry and john. > $WoW > HERE > ) [[email protected] ~]# echo $m1 line 1 is good. They are jack,marry and john.
- 如何关闭变量
[[email protected] ~]# m1=$(cat <<‘HERE‘ > line 1 is good. > They are jack,marry and john. > $WoW > HERE > ) [[email protected] ~]# echo $m1 line 1 is good. They are jack,marry and john. $WoW
- 做多行批注
40.如何测试变量是否存在
语法: ${待测变量-默认值}
解释:判断待测变量是否存在,如果变量存在,就传回变量的值,如果没有就会将默认值传给待测变量
- 判断变量是否存在
[[email protected] ~]# unset myname [[email protected] ~]# r=${myname-‘basher‘} [[email protected] ~]# echo $r basher
[[email protected] ~]# myname="Jack" [[email protected] ~]# r=${myname-‘basher‘} [[email protected] ~]# echo $r Jack
- 判断变量是否不存在或者为空
[[email protected] ~]# myname= [[email protected] ~]# r=${myname:-‘basher‘} [[email protected] ~]# echo $r basher [[email protected] ~]# unset myname [[email protected] ~]# r=${myname:-‘basher‘} [[email protected] ~]# echo $r basher
- 用if判断
- 测试变量是否"不存在"或其值为空:提示错误信息
语法:${待测变量:?提示信息}
解释:判断变量是否"不存在"或者为"空",如果条件成立,则显示:?后的提示信息。并立即停止执行script
拆解表达式:
范例:
时间: 2024-10-08 22:54:18