Shell脚本中变量的基本概念和使用方法介绍
作者:.Kaser.
这篇文章主要为大家详细介绍了Shell脚本中变量的基本概念和使用方法,文中通过大量示例演示了变量在Shell编程中的灵活运用,并强调了变量命名规范等注意事项,希望对大家有所帮助

变量:用于一个固定的字符串去表示不固定的内容,便于修改。
1.自定义变量
用户自定义变量是最常用的变量类型;
其特点是变量名和变量值都是有用户自由定义的。
1.1 变量调用及注意事项
[root@shell-test ~]# name=kaser [root@shell-test ~]# echo $name kaser
变量名不能以数字开头
[root@shell-test ~]# 2aaa=kkkkkk bash: 2aaa=kkkkkk: command not found...
等号左右两侧不能有空格
[root@shell-test ~]# ccc = 123 bash: ccc: command not found...
变量值若有空格,必须使用引号包含
[root@shell-test ~]# bbb=ka ka bash: ka: command not found... [root@shell-test ~]# echo $bbb [root@shell-test ~]# bbb="ka ka" [root@shell-test ~]# echo $bbb ka ka
1.2 重复定义变量
[root@shell-test ~]# cc=123
[root@shell-test ~]# echo $cc
123
[root@shell-test ~]# cc="$cc"456
[root@shell-test ~]# echo $cc
123456
[root@shell-test ~]# cc=${cc}789
[root@shell-test ~]# echo $cc
123456789
1.3 变量查看set及变量删除unset
# set [root@shell-test ~]# set | grep cc cc=123456789 ccc='kaser 666' # unset [root@shell-test ~]# unset 变量名
1.4 变量注意事项
- 变量名和等号之间不能有空格。
- 变量名命名规则如下:
- 命名只能使用英文字母,数字和下划线,首个字符不能以数字开头。
- 中间不能有空格,可以使用下划线(_)。
- 不能使用标点符号。
- 不能使用bash里的关键字(可用help命令查看保留关键字)。
- 起变量名,请使用五个字母以上。
1.5 示例:编写“测试主机是否存活的脚本”?
1.5.1 不使用变量
[root@shell-test ~]# vim live_host-1.sh ping -c1 172.25.254.44 &> /dev/null && echo "172.25.254.44 UpUpUp~" || echo "172.25.254.44 DownDownDown~" [root@shell-test ~]# chmod +x live_host.sh [root@shell-test ~]# ./live_host.sh 172.25.254.44 UpUpUp~
1.5.2 使用变量
[root@shell-test ~]# vim live_host-2.sh #!/bin/bash ip=172.25.254.44 ping -c1 $ip &> /dev/null && echo "$ip UpUpUp~" || echo "%ip DownDownDown~" [root@shell-test ~]# chmod +x live_host-2.sh [root@shell-test ~]# ./live_host-2.sh 172.25.254.44 UpUpUp~
1.5.3 变量+交互式
# 交互定义变量 read 从键盘读入变量值 read 变量名
[root@shell-test ~]# vim live_host-3.sh #!/bin/bash read -p "请输入测试主机IP地址:" ip ping -c1 $ip &> /dev/null && echo "$ip UpUpUp~" || echo "$ip DownDownDown~" [root@shell-test ~]# chmod +x live_host-3.sh [root@shell-test ~]# ./live_host-3.sh 请输入测试主机IP地址:172.25.254.44 172.25.254.44 UpUpUp~
2.整数运算
2.1expr
+加,-减,*乘,/除,%取余
[root@shell-test ~]# expr 3 + 6 9 [root@shell-test ~]# num1=3 [root@shell-test ~]# num2=6 [root@shell-test ~]# expr $num1 + $num2 9 [root@shell-test ~]# expr $num1 - $num2 -3 [root@shell-test ~]# expr $num1 \* $num2 18 [root@shell-test ~]# expr $num1 / $num2 0 [root@shell-test ~]# expr $num1 % $num2 3
2.2$(())
[root@shell-test ~]# num1=2 [root@shell-test ~]# num2=4 [root@shell-test ~]# echo $((num1+num2)) 6 [root@shell-test ~]# echo $(($num1+$num2)) 6
2.3$[]
[root@shell-test ~]# echo $[3+2] 5 [root@shell-test ~]# echo $[3*2] 6 [root@shell-test ~]# echo $[3/2] 1 [root@shell-test ~]# echo $[3%2] 1
2.4let
[root@shell-test ~]# let a++;echo $a 1 [root@shell-test ~]# let a++;echo $a 2 [root@shell-test ~]# let a++;echo $a 3 [root@shell-test ~]# let a++;echo $a 4 [root@shell-test ~]# let a++;echo $a 5 [root@shell-test ~]# let a++;echo $a 6 [root@shell-test ~]# let a++;echo $a 7 [root@shell-test ~]# let a++;echo $a 8
3.小数运算
bc:交互运算器
[root@shell-test ~]# yum install -y bc [root@shell-test ~]# echo "2+3" | bc 5 [root@shell-test ~]# echo "2*3" | bc 6 [root@shell-test ~]# echo "2%3" | bc 2 [root@shell-test ~]# echo "2/3" | bc 0 [root@shell-test ~]# echo "scale=3;10/3" | bc 3.333 [root@shell-test ~]# echo "scale=8;10/3" | bc 3.33333333
4.环境变量
自定义变量,只能再当前shell生效。其他shell是不生效的。说白了,你的变量只能你用。那如果有些变量,需要所有的用户都使用用。怎么办呢?
定义环境变量
1.直接申明
[root@shell-test ~]# ppp=333 [root@shell-test ~]# echo $ppp 333 [root@shell-test ~]# echo $$ 2139 [root@shell-test ~]# bash [root@shell-test ~]# echo $ppp [root@shell-test ~]# echo $$ 2163 [root@shell-test ~]# export ppp=3 [root@shell-test ~]# echo $ppp 3 [root@shell-test ~]# echo $$ 2139 [root@shell-test ~]# bash [root@shell-test ~]# echo $$ 2186 [root@shell-test ~]# echo $ppp 3
2.转换环境变量
将自定义变量转换到环境变量
~/.bash_profile:当前用户/etc/profile:所有用户
# 直接申明,但是重新连接到ssh则会发现我们申明的环境变量消失了 [root@shell-test ~]# export kkk=123123123 [root@shell-test ~]# echo $kkk 123123123 [root@shell-test ~]# bash [root@shell-test ~]# echo $kkk 123123123 [root@shell-test ~]# bash [root@shell-test ~]# echo $kkk 123123123 [root@shell-test ~]# ssh 172.25.254.44 [root@shell-test ~]# echo $kkk
1.~/.bash_profile
[root@shell-test ~]# echo "export xx=0000" >> .bash_profile [root@shell-test ~]# source .bash_profile [root@shell-test ~]# echo $xx 0000 [root@shell-test ~]# bash [root@shell-test ~]# [root@shell-test ~]# echo $xx 0000 [root@shell-test ~]# useradd kaser [root@shell-test ~]# su - kaser [kaser@shell-test ~]$ echo $xx [kaser@shell-test ~]$
2./etc/profile
[root@shell-test ~]# echo "export ggg=444" >> /etc/profile [root@shell-test ~]# source /etc/profile [root@shell-test ~]# echo $ggg 444 [root@shell-test ~]# bash [root@shell-test ~]# echo $ggg 444 [root@shell-test ~]# su - kaser [kaser@shell-test ~]$ echo $ggg 444
5.预定义变量
5.1 与参数相关的变量
$0 # 当前脚本名称 $1 $2 $3 ... $x # 位置参数 $# # 参数总数 $* $@ # 所有位置参数
5.2 与执行状态相关的变量
$? # 上个命令退出的状态码 $$ # 当前Shell进程ID(PID) $! # 最后一个后台运行进程的进程ID(PID)
5.3 与环境及输入相关的变量
$- # 当前Shell的选项标志 $_ # 上一个命令的最后一个参数 $IFS # 内部字段分隔符
5.4 示例
[root@shell-test ~]# vim test-2.sh #!/bin/bash echo "脚本名称: $0" echo "第一个参数: $1" echo "第二个参数: $2" echo "参数总数: $#" echo "所有参数(*): $*" echo "所有参数(@): $@" # 模拟一个命令并检查返回值 ls -l /mnt echo "上一个命令的退出状态: $?" ls -l /non_existent_directory echo "上一个命令的退出状态: $?" echo "当前进程ID: $$" # 后台运行示例 sleep 100 & echo "后台进程ID: $!" echo "上一个命令的最后一个参数: $_" # 注意:在这里 $_ 将是 "$!" 的值,因为那是上一个命令(echo)的最后一个参数 [root@shell-test ~]# chmod +x test-2.sh [root@shell-test ~]# ./test-2.sh 3 5 脚本名称: ./test-2.sh 第一个参数: 3 第二个参数: 5 参数总数: 2 所有参数(*): 3 5 所有参数(@): 3 5 total 0 drwxr-xr-x. 2 root root 6 Nov 4 10:34 hgfs 上一个命令的退出状态: 0 ls: cannot access '/non_existent_directory': No such file or directory 上一个命令的退出状态: 2 当前进程ID: 4099 后台进程ID: 4102 上一个命令的最后一个参数: 后台进程ID: 4102
5.5 测试
5.5.1 编写自动创建用户并设置初始密码的简易脚本
[root@shell-test ~]# vim new_user_passwd.sh #!/bin/bash read -p "新建用户名(name)=" name read -sp "新建密码(passwd)=" passwd echo useradd $name echo "$passwd" | passwd --stdin $name > /dev/null echo "$name已经被创建,密码为$passwd,为账号安全,请勿外传!" [root@shell-test ~]# chmod +x new_user_passwd.sh [root@shell-test ~]# ./new_user_passwd.sh 新建用户名(name)=kkk 新建密码(passwd)= kkk已经被创建,密码为123,为账号安全,请勿外传!
5.5.2 编写自动配置本地软件源的简易脚本
[root@shell-test ~]# vim auto_mount_yum.sh #!/bin/bash echo "这是一个自动配置本地软件仓库的脚本(开机自动挂载)!" echo "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" echo echo mkdir /local echo "已创建/local,将/dev/sr0挂载至此。" mount /dev/sr0 /local mount /dev/sr0 /local >> /etc/rc.d/rc.local chmod +x /etc/rc.d/rc.local echo "已完成开机自动挂载!" echo cat <<EOF > /etc/yum.repos.d/local.repo [AppStream] name=AppStream baseurl=file:///local/AppStream [BaseOS] name=BaseOS baseurl=file:///local/BaseOS EOF yum clean all && yum makecache echo "元数据已缓存至本地,开始使用吧!"
以上就是Shell脚本中变量的基本概念和使用方法介绍的详细内容,更多关于Shell脚本变量使用的资料请关注脚本之家其它相关文章!
