1.变量的定义
变量用来存放各种数据,脚本语言在定义变量时通常不需要指明类型,直接赋值就可以,shell变量也遵循这个规则。
2.在shell脚本中变量的种类
在shell中变量分为环境级变量,用户级变量,系统级变量。环境级变量只在当前shell中生效,shell关闭变量丢失,用户级变量卸载用户的骨文件中,只针对当前用户生效,系统级变量被写在系统的配置文件/etc/profile
或者/etc/frofile.d/
,对于所有用户都生效。
3.在shell脚本中如何定义变量
1).export a=1 /环境级
[root@westos mnt]# vim a.sh
[root@westos mnt]# sh a.sh
[root@westos mnt]# lzy=1
[root@westos mnt]# sh a.sh
[root@westos mnt]# export lzy=1
[root@westos mnt]# sh a.sh
vim ~/.bash_profile /用户级
[root@westos mnt]# vim ~/.bash_profile
[root@westos mnt]# tail -n 1 ~/.bash_profile
[root@westos mnt]# source ~/.bash_profile
[root@westos mnt]# echo $b
[root@westos mnt]# su - student
[student@westos ~]$ echo $b
vim /etc/profile /系统级
[root@westos ~]# vim /etc/profile
[root@westos ~]# tail -n 1 /etc/profile
[root@westos ~]# source /etc/profile
[root@westos ~]# echo $c
[root@westos ~]# su - student
[student@westos ~]$ echo $c
4.字符的转译和变量的声明
\ 转译但个字符
"" 弱引用,批量转译""中出现的字符
'' 强引用,批量转译''中出现的字符
/""与''的区别载于:""不能转译"\","`","!","$"
$() 变量声明
e.g.
[root@westos ~]# A=1
[root@westos ~]# echo $Ab
[root@westos ~]# echo $\Ab
[root@westos ~]# echo $\A b
[root@westos ~]# echo $A \b
[root@westos ~]# echo '#####`hostname`#####' /转译`符号和hostname一起输出
[root@westos ~]# echo "#####`hostname`#####" /弱引用,执行`hostname`里的命令
e.g.执行create_user.sh
后可以建立用户user1,user2,user3
[root@westos mnt]# vim create_user.sh
[root@westos mnt]# cat userfile
[root@westos mnt]# sh create_user.sh userfile
e.g.执行create_users.sh
后可以建立用户user1,user2,user3
以及他们对应的登录密码是passfile
里的对应行
[root@westos mnt]# cat userfile passfile
[root@westos mnt]# sh create_users.sh userfile passfile
[root@westos mnt]# su - student
[student@westos ~]$ su - user1
Password:
[user1@westos ~]$
5.变量值传递
$1 /脚本后的第一串字符
$2 /脚本后的第二串字符
$3 /脚本后的第三串字符
$# /脚本后的字符串的个数
$* /脚本后的所有字符串,模式为"1 2 3"
$@ /脚本后的所有字符串,模式为"1""2""3"
e.g.
[root@westos mnt]# vim westos.sh
[root@westos mnt]# chmod +x westos.sh
[root@westos mnt]# sh westos.sh linux
[root@westos mnt]# sh westos.sh linux lin
[root@westos mnt]# sh westos.sh linux lin li
6.用read
实现变量传递
read WESTOS
read -s WESTOS
read -p "input:" WESTOS
e.g.执行usercel.sh
文件后可以实现对用户的建立与删除
[root@westos mnt]# sh userctl.sh -c
Please input username: 123
Please input password:
[root@westos mnt]# su - student
[student@westos ~]$ su - 123
Password:
[123@westos ~]$ exit
[student@westos ~]$ exit
[root@westos mnt]# sh userctl.sh -d
Please input username: 123
[root@westos mnt]# sh userctl.sh -d
Please input username: westos
7.命令别名的设定
unalias xie /撤销命令别名设定
1).alias xie='vim' /在当前shell下的定别名,退出后自动消失
[root@westos mnt]# alias xie='vim'
[root@westos mnt]# xie xiefile
[root@westos mnt]# cat xiefile
[root@westos mnt]# unalias xie
[root@westos mnt]# xie file
2).vim ~/.bashrc /用户级设定别名
alias xie='vim'
[root@westos mnt]# vim ~/.bashrc
[root@westos mnt]# source ~/.bashrc
[root@westos mnt]# tail -n 1 ~/.bashrc
[root@westos mnt]# xie rootfile
[root@westos mnt]# cat rootfile
[root@westos mnt]# su - student
[student@westos ~]$ xie studentfile
3).vim /etc/bashrc /系统级设定别名
alisas xie='vim'
[root@westos mnt]# vim /etc/bashrc
[root@westos mnt]# source /etc/bashrc
[root@westos mnt]# tail -n 1 /etc/bashrc
[root@westos mnt]# vim haha
[root@westos mnt]# cat haha
[root@westos mnt]# su - student
[student@westos ~]$ xie studentfile
[student@westos ~]$ cat studentfile
4).撤销设定
删除文件中的别名设定后执行unalias
[root@westos mnt]# vim /etc/bashrc
[root@westos mnt]# unalias xie
[root@westos mnt]# xie checkfile
8.利用命令执行结果设定变量
Hostname=$(hostname)
Hostname=`hostname`
$?
$?是命令在执行完成之后产生的退出值
范围是[1-255]
当$0=0时表示命令执行没有错误输出,这个值可以用exit命令执行
9.脚本函数
e.g.编写函数使得执行usercreate.sh
时可以建立用户,若用户存在不可以建立,若不存在输入建立的用户名和用户密码
[root@westos mnt]# vim usercreate.sh
[root@westos mnt]# sh usercreate.sh
Please input username: student
Please input username: liu
[root@westos mnt]# su - liu
[liu@westos ~]$
e.g.检查教室中1-20台主机中,哪些主机的网络是通通畅的并输出这些主机
[root@westos mnt]# vim check_host.sh
[root@westos mnt]# sh check_host.sh