Linux下的shell中的变量

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 

【12】

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

【13】

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

【14】

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

【15】

[root@westos ~]# echo '#####`hostname`#####'	/转译`符号和hostname一起输出
[root@westos ~]# echo "#####`hostname`#####"	/弱引用,执行`hostname`里的命令

【16】

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 

【17】【18】

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 ~]$ 

【19】【20】

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

【21】【22】

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

【23】【24】

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

【25】

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

【26】

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 

【27】

4).撤销设定
删除文件中的别名设定后执行unalias
[root@westos mnt]# vim /etc/bashrc 
[root@westos mnt]# unalias xie
[root@westos mnt]# xie checkfile

【28】

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 ~]$ 

【29】【30】
e.g.检查教室中1-20台主机中,哪些主机的网络是通通畅的并输出这些主机

[root@westos mnt]# vim check_host.sh
[root@westos mnt]# sh check_host.sh 

【31】【32】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值