基于linux下的shell中的变量
1、shell中的变量
(1)环境级[[email protected] ~]# export a=1 #export作声明
[[email protected] ~]# echo $a
1
[[email protected] ~]# su - student
[[email protected] ~]$ echo $a #为空
(2)用户级
[[email protected] ~]# vim .bash_profile
export a=1
[[email protected] ~]# source .bash_profile #刷新
[[email protected] ~]# echo $a
1
[[email protected] ~]# su - student
[[email protected] ~]$ echo $a #为空
[[email protected] ~]# vim /etc/profile
export a=3
[[email protected] ~]# source /etc/profile
[[email protected] ~]# echo $a
3
[[email protected]esktop ~]# exit
[[email protected] ~]# echo $a #显示的是用户级的变量,这是因为用户级配置文件后被加载,所以其生效
2
2、
[[email protected] ~]# echo $PS1
[[email protected] ~]# PS1='westos>'
westos/u>PS1='westos\u>'
westosroot>PS1='westos \u>'
westos root>PS1='westos \w>' #w绝对路径
westos ~>cd /etc/sysconfig/
westos /etc/sysconfig>PS1='westos \W>' #W相对路径
westos sysconfig>cd /etc/sysconfig/
westos sysconfig>exit
3、变量的转义
\是单个的,""是批量的,
''为\的批量,称为强引用,可以引用所有字符,包括!、\、`、$这四个字符
4、变量值的传递
$n 脚本后跟的第n串字符
$# 脚本后所跟字符数的个数
$* 脚本后跟的所有字符串,其后字符默认为一串
[email protected] 脚本后跟的所有字符串,其后字符默认为三串
实验:利用脚本使用用户和密码文件建立用户,当文件不够时给出提醒
[[email protected] mnt]# vim password
[[email protected] mnt]# vim username
[[email protected] mnt]# vim user.sh
[ "$#" -ne "2" ]&&{
echo "Please give me userfile and passwordfile"
exit 1
} #当脚本执行是其后字符数不等于2时提醒
MAX_LINE=`wc -l $1|cut -d " " -f 1`
for LINE_NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n "${LINE_NUM}p" $1`
PASSWORD=`sed -n "${LINE_NUM}p" $2`
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
done
5、read实现变量传递
实验:交互式判断ip是否可以ping通
[[email protected] mnt]# vim read.sh
read -p "Please input ip: " (-s) ip #-p表示提示,-s表示用户输入时内容不可见
ping -c1 -w1 $ip &> /dev/null && echo $ip is up || echo $ip is down
实验:利用脚本建立用户,交互式输入用户文件和密码文件,当文件不存在时给出提醒
[[email protected] mnt]# vim user.sh
#!/bin/bash
read -p "Please input userfile: " userfile
[ -e "$userfile" ]||{
echo "This file is not exit!"
exit 1
}
read -p "Please input passfile: " passfile
[ -e "$passfile" ]||{
echo "This file is not exit!"
exit 1
}
MAX_LINE=`wc -l $userfile|cut -d " " -f 1`
for LINE_NUM in `seq 1 $MAX_LINE`
do
USERNAME=`sed -n "${LINE_NUM}p" $userfile`
PASSWORD=`sed -n "${LINE_NUM}p" $passfile`
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
done
6、系统中命令别名的设定
[[email protected] ~]# alias xie='vim' #临时设定别名
[[email protected] ~]# xie file
[[email protected] ~]# su - student
[[email protected] ~]$ xie
bash: xie: command not found...
[[email protected] ~]$ exit
logout
[[email protected] ~]# source .bashrc
[[email protected] ~]# xie file
[[email protected] ~]# su - student
[[email protected] ~]$ xie
bash: xie: command not found...
[[email protected] ~]$ exit
logout
[[email protected] ~]# source /etc/bashrc
[[email protected] ~]# xie file
[[email protected] ~]# su - student
[[email protected] ~]$ xie file
[[email protected] ~]# vim .bashrc #取消设定别名
[[email protected] ~]# vim /etc/bashrc
[[email protected] ~]# source .bashrc
[[email protected] ~]# source /etc/bashrc
[[email protected] ~]# xie file #删除文件中的设定行仍然成立,因为原有的设定不会被覆盖
[[email protected] ~]# unalias xie #撤销命令别名设定
[[email protected] ~]# xie file
bash: xie: command not found...
7、exit退出值
利用命令执行结果设定变量
Hostname=$(hostname)
Hostname=`hostname`
$?是命令在执行完成后产生的退出值,范围是[0-255]
$?=0时表示命令执行没有错误输出,这个值可以用exit命令执行,如exit66
实验:利用退出值判断ip是否通
[[email protected] mnt]# vim ip_check.sh
read -p "Please input ip: " ip
ping -c1 -w1 $ip &> /dev/null
B="$?" #返回值赋给B
echo exit="$B"
[ "$B" -eq "0" ]&&{ #退出值等于0时表示通
echo "$ip is up"
}||{
echo "$ip is down"
}
[[email protected] mnt]# sh ip_check.sh
Please input ip: 172.25.254.68
exit=1
172.25.254.68 is down
[[email protected] mnt]# sh ip_check.sh
Please input ip: 172.25.254.25
exit=0
172.25.254.25 is up
8、脚本中的函数
变量:利用一串字符表示一个值
函数:利用一串字符表示一个动作
简化脚本内容,提高可读性
可以循环调用
实验:循环建立、删除用户,exit退出
#!/bin/bash
ACTION_ADD(){
[ "$1" = add ]&&{
read -p "Please input your user name: " NAME
read -p "Please input your user passwd: " -s PASSWD
useradd $NAME
echo $PASSWD | passwd --stdin $NAME
}
}
ACTION_DEL(){
[ "$1" = del ]&&{
read -p "Please input your user name: " NAME
userdel -r $NAME
}
}
USER_CTL()
{ read -p "Please input action: " ACTION
[ "$ACTION" = exit ]&&{
echo bye!
exit 0
}
ACTION_ADD $ACTION
ACTION_DEL $ACTION
USER_CTL
}
USER_CTL
测试:
[[email protected] mnt]# sh user_ctrl.sh
Please input action: add
Please input your user name: haha
Please input action: exit
bye!
[[email protected] mnt]# id haha
uid=1011(haha) gid=1011(haha) groups=1011(haha)
[[email protected] mnt]# sh user_ctrl.sh
Please input action: del
Please input your user name: haha
Please input action: exit
bye!
[[email protected] mnt]# id haha
id: haha: no such user