1.shell中的变量
变量的定义:变量即在程序运行过程中它的值是允许改变的量
变量是用一串固定的字符来标示不固定的值的一种方法
变量是一种使用方便的占位符,用于引用计算机内存地址,该地址可以存储Script运行时可更改的程序信息
在shell中变量是不能永久保存在系统中的,必须在文件中声明
实验(变脸的简单使用):
<1>在系统中写入变量
[[email protected] ~]# a=1
[[email protected] ~]# echo $a
[[email protected] ~]# echo {$a}b
<2>当在脚本中写入变量时,必须使用命令export将其放在系统共享处
[[email protected] ~]# export a=1
[[email protected] ~]# vim /mnt/test.sh
[[email protected] ~]# chmod +x /mnt/test.sh
[[email protected] ~]# /mnt/test.sh
[[email protected] ~]# cat /mnt/test.sh
<3>在系统中写入共享的变量,退出这个shell界面后,再次登陆后,会发现无法使用之前设置的变量
[[email protected] mnt]# export a=1
[[email protected] mnt]# echo $a
[[email protected] mnt]# logout
[[email protected] Desktop]$ ssh [email protected]
[[email protected] ~]# echo $a
<4>在~/.bash_profile中写入变量,发现解决了退出界面后变无法使用的问题,但是发现切换用户后便无法使用变量,在文件中写入变量(用户级变量)
[[email protected] ~]# echo $a
[[email protected] ~]# vim ~/.bash_profile ##也可以写成[[email protected] ~]# vim ~/.bashrc
[[email protected] ~]# source ~/.bash_profile ##使其生效
[[email protected] ~]# echo $a ##成功输出
[[email protected] ~]# logout
[[email protected] Desktop]$ ssh [email protected]
[[email protected] ~]# echo $a ##成功输出
[[email protected] ~]# su - student
[[email protected] ~]$ echo $a ##无法输出
[[email protected] ~]$ logout
~/.bash_profile文件更改的内容:
<5>在/etc/profile中写入变量,使其变量成为系统级变量(即是切换用户后依旧可以成功使用)
[[email protected] ~]# vim ~/.bash_profile ##先将用户级别的变量注释掉
[[email protected] ~]# vim /etc/profile
[[email protected] ~]# source /etc/profile
[[email protected] ~]# echo $a
[[email protected] ~]# logout
[[email protected] Desktop]$ ssh [email protected]
[[email protected] ~]# echo $a
[[email protected] ~]# su - student
[[email protected] ~]$ echo $a
[[email protected] ~]$ logout
将~/.bash_profile中的内容注释:
/etc/profile文件更改的内容:
<6>建议不要直接更改/etc/profile中的内容,最好是在/etc/profile.d中新建一个脚本并在里面写入变量(会发现两者的效果是相同的)
[[email protected] ~]# vim /etc/profile
[[email protected] ~]# cd /etc/profile.d
[[email protected] profile.d]# ls
[[email protected] profile.d]# vim westos.sh
[[email protected] profile.d]# cat westos.sh
[[email protected] profile.d]# source /etc/profile.d/westos.sh
[[email protected] profile.d]# echo $a
[[email protected] profile.d]# logout
[[email protected] Desktop]$ ssh [email protected]
[[email protected] ~]# echo $a
[[email protected] ~]# su - student
[[email protected] ~]$ echo $a
[[email protected] ~]$ logout
[[email protected] ~]# cd /etc/profile.d
[[email protected] profile.d]# vim westos.sh
[[email protected] profile.d]# cat westos.sh
将/etc/profile中的内容注释:
2.shell变量名称的规范
变量名称中通常包含大小写字母,数字,下划线(不是必须)
变量名称格式
WESTOS_LINUX
Westos_Linux
westoS_Linux
3.shell字符的转译及变量的声明
\ 转译单个字符
"" 弱引用,批量转译""中出现的字符
'' 强引用,批量转译''中出现的字符
''与"" 两者的区别在于,""不能转译"\","`","!","$"
${} 变量声明
例如:
A=1
echo $Ab
echo ${A}b
实验:
<1>查看“#”的用法
[[email protected] mnt]# echo # ##无法输出“#”,因为“#”在文件内是注释的作用
[[email protected] mnt]# echo #hello ##无法输出“#hello”,因为“#”在文件内是注释的作用
[[email protected] mnt]# echo \#hello ##可以成功输出,因为“#”被转译字符转译了
[[email protected] mnt]# echo \#hello# ##可以输出全部“#”,因为有转译字符,而且第一个字符与第二个字符之间没有空格,说明两个字符是同一个字符
[[email protected] mnt]# echo \hello # ##两个字符间有空格,则第二个“#”将无法被识别
[[email protected] mnt]# echo \#hello # ##两个字符间有空格,则第二个“#”将无法被识别
[[email protected] mnt]# echo \#hello \# ##当两个字符都使用转译字符时,发现都可以被成功识别
<2>说明双引号(“”)是弱引用
[[email protected] mnt]# echo `hostname` ##成功输出命令
[[email protected] mnt]# echo '#`hostname`#' ##无法成功输出命令
[[email protected] mnt]# echo #`hostname`# ##无法成功输出命令
[[email protected] mnt]# echo "#`hostname`#" ##成功输出命令
<3>"“不能转译”","`","!","$"
[[email protected] mnt]# echo "!"
[[email protected] mnt]# echo "$5"
[[email protected] mnt]# echo "`"
[[email protected] mnt]# echo "\"
4.利用命令执行结果设定变量
Hostname=$(hostname)
Hostname=`hostname`
$?
$?是命令在执行完成之后产生的退出值,范围是[0-255]
当$0=0时标示命令执行没有错误输出,这个值可以用exit命令执行,例如exit 66
实验一:
-1-变量中添加命令并输出
<1>使用export命令的第一种格式
[[email protected] ~]# export a=`hostname`
[[email protected] ~]# vim /mnt/test.sh
[[email protected] ~]# chmod +x /mnt/test.sh
[[email protected] ~]# /mnt/test.sh
[[email protected] ~]# cat /mnt/test.sh
<2>使用export命令的第二种格式
[[email protected] ~]# export a=$(hostname)
[[email protected] ~]# /mnt/test.sh
-2-有时候只能用“\”而不能用“$()”
<1>在脚本中使用“``”输出没命令,发现可以成功输出
[[email protected] ~]# yum install perl -y
[[email protected] ~]# which perl ##查找perl的所在位置
[[email protected] ~]# cd /mnt
[[email protected] mnt]# vim test1
[[email protected] mnt]# cat test1
[[email protected] mnt]# chmod +x test1
[[email protected] mnt]# /mnt/test1
在/mnt/test1中写的内容:
#!/usr/bin/perl
print `date`
<2>在脚本中使用"$()"输出没命令,发现可以无法成功输出
[[email protected] mnt]# vim test1
[[email protected] mnt]# cat test1
[[email protected] mnt]# /mnt/test1
在/mnt/test1中写的内容:
#!/usr/bin/perl
print $(date)
注意:
反向单引号中的内容是先被引用的,是通用的,而$()是不通用的
实验二:
-1-当输入一串系统无法识别的命令时,使用echo $?会出现说明错误的数字(1-155)
[[email protected] mnt]# dfhdsohgfoshdg
[[email protected] mnt]# echo $?
-2-当输入一串系统无法识别的命令时,使用echo $?会出现说明正确的数字0(默认的)
[[email protected] mnt]# ls
[[email protected] mnt]# echo $?
-3-设置当输入一串系统无法识别的命令时,使用echo $?会出现说明指定的数字
[[email protected] mnt]# vim haha.sh
[[email protected] mnt]# sh haha.sh
[[email protected] mnt]# echo $?
[[email protected] mnt]# cat haha.sh
5.linux系统中命令别名的设定
alias xie=‘vim’
vim ~/.bashrc
alias xie=‘vim’
vim /etc/bashrc
allias xie=‘vim’
unalias xie
<1>只在当前shell界面设置ls的别名
[[email protected] mnt]# ls
[[email protected] mnt]# alias bai='ls' ##给ls起别名为bai
[[email protected] mnt]# bai
[[email protected] mnt]# logout
[[email protected] Desktop]$ ssh [email protected]
[[email protected] ~]# cd /mnt
[[email protected] mnt]# bai ##发现退出后便无法使用
[[email protected] mnt]# ls
<2>设置用户级别ls的别名
[[email protected] mnt]# vim ~/.bashrc
[[email protected] mnt]# source ~/.bashrc
[[email protected] mnt]# ls
[[email protected] mnt]# bai
[[email protected] mnt]# logout
[[email protected] Desktop]$ ssh [email protected]
[[email protected] ~]# cd /mnt
[[email protected] mnt]# bai
[[email protected] mnt]# ls
[[email protected] mnt]# su - student
[[email protected] ~]$ cd /mnt
[[email protected] mnt]$ bai ##发现切换用户后便无法使用
[[email protected] mnt]$ ls
[[email protected] mnt]$ logout
在~/.bashrc文件中写入的内容:
<3>设置系统级别ls的别名
[[email protected] mnt]# vim ~/.bashrc
[[email protected] mnt]# vim /etc/bashrc
[[email protected] mnt]# source /etc/bashrc
[[email protected] mnt]# bai
[[email protected] mnt]# logout
[[email protected] Desktop]$ ssh [email protected]
[[email protected] ~]# cd /mnt
[[email protected] mnt]# bai
[[email protected] mnt]# su - student
[[email protected] ~]$ cd /mnt
[[email protected] mnt]$ bai
[[email protected] mnt]$ logout
[[email protected] mnt]# vim /etc/bashrc
将~/.bashrc文件中写入的内容进行注释:
在/etc/bashrc文件中写入的内容:
实验做完后将/etc/bashrc文件中写入的内容进行注释: