shell脚本快速入门系列之------变量

一、shell变量的作用,类型

1、变量的作用

为灵活管理Linux系统提供特定参数,有两层意思
变量名:使用固定的名称,由系统预设或用户定义
变量值:能够根据用户设置,系统环境的变化而变化

2、变量的类型

自定义变量:由用户自己定义,修改和使用

环境变量:由系统维护,用于设置工作环境

位置变量:通过命令行给脚本程序传递参数

预定义变量:bash中内置的一类变量,不能直接修改

二、自定义变量

1、定义一个新的变量与查看变量的值

变量名以字母或下划线开头,区分大小写,建议全大写
变量名=变量值
代表将右边的值赋予左边
查看变量的值:echo $变量名
例如:

[root@localhost opt]# shangzhen=111
[root@localhost opt]# shengjie=222
[root@localhost opt]# echo $shangzhen $shengjie
111 222



2、赋值时使用引号与从键盘输入内容为变量赋值

赋值时使用引号

双引号:允许通过$符号引用其他变量值

单引号:禁止引用其他变量值,$视为普通字符

反撇号:命令替换,提取命令执行后的输出结果

[root@localhost opt]# echo "$product"
python
[root@localhost opt]# echo "${product}3.3"	'双引号:允许通过$符号引用其他变量值'
python3.3
[root@localhost opt]# echo ‘$product’
‘python’
[root@localhost opt]# echo '$product'	'单引号禁止引用其他变量值,$视为普通字符'
$product
[root@localhost opt]# ps aux | wc -l
151
[root@localhost opt]# num=`ps aux | wc -l`	'反撇号中识别命令,对命令结果进行解析'
[root@localhost opt]# echo $num
152
[root@localhost opt]# abc=$(ps aux | wc -l)
[root@localhost opt]# echo $abc
152

从键盘输入内容为变量赋值

read [-p "提示信息"] 变量名
[root@localhost opt]# vim demo.sh 
#!/bin/bash
#This is my first shell-script
read -p "请输入一个整数:" score
echo "你的成绩为:$score"
~ 
[root@localhost opt]# ./demo.sh 
请输入一个整数:80
你的成绩为:80

3、设置变量的作用范围

格式一:export 变量名...
格式二:export 变量名=变量值...
两种格式可以混合使用
[root@localhost opt]# num=`ps aux | wc -l`
[root@localhost opt]# echo $num
156
[root@localhost opt]# bash	'设置变量作用范围'
[root@localhost opt]# echo $num
		'显示不出来'
[root@localhost opt]# exit	'退出变量范围'
exit
[root@localhost opt]# echo $num
156
[root@localhost opt]# export num	'使用export设置变量'
[root@localhost opt]# bash		'变量范围'
[root@localhost opt]# echo $num		'生效'
156
[root@localhost opt]# 

4、整数变量的运算和常用运算符

  • 整数变量的运算
expr 变量1 运算符 变量2 [运算符 变量3]...
  • 常用运算符
加法运算:+
减法运算:-
乘法运算:\*
除法运算:/
求模(取余)运算:%
[root@localhost opt]# expr 3+2	'错误格式,不加空格'
3+2
[root@localhost opt]# expr 3 + 2	'正确格式,加法运算'
5
[root@localhost opt]# expr 3 - 2	'正确格式,减法运算'
1	
[root@localhost opt]# expr 3 * 2	'错误格式,乘法运算'
expr: syntax error
[root@localhost opt]# expr 3 \* 2	'正确格式,乘法运算'
6
[root@localhost opt]# expr 3 / 2	'正确格式,除法运算'
1
[root@localhost opt]# expr 3 % 2	'正确格式,取余运算'
1
[root@localhost opt]# expr 32 % 21
11
[root@localhost opt]# expr 32 % 5
2
[root@localhost opt]# sum=`expr 3+3`	'错误格式,求和变量运算'
[root@localhost opt]# echo $sum
3+3
[root@localhost opt]# sum=`expr 3 + 3`	'正确格式,求和变量运算'
[root@localhost opt]# echo $sum
6

三、特殊的shell变量

1:环境变量和常见的环境变量

  • 环境变量

由系统提前创建,用来设置用户的工作环境
配置文件:/etc/profile,~/.bash_profile

  • 常见的环境变量

PWD,PATH
USER,SHELL,HOME

2:位置变量

表示为$n,n为1-9之间的数字
[root@localhost opt]# vim demo.sh 
#!/bin/bash
#This is my first shell-script
sum=0
sum=`expr $1 + $2`
echo "总和为:$sum"

[root@localhost opt]# ./demo.sh 20 30
总和为:50
[root@localhost opt]# vim demo.sh 
#!/bin/bash
#This is my first shell-script
sum=0
sum=`expr $1 + $2`
echo "总和为:$sum"
echo "执行的脚本是$0"

[root@localhost opt]# ./demo.sh 22 33
总和为:55
执行的脚本是./demo.sh
[root@localhost opt]# !vim
#!/bin/bash
#This is my first shell-script
sum=0
sum=`expr $1 + $2`
echo "总和为:$sum"
echo "执行的脚本是:$0"
echo "执行的脚本个数是:$#"		'$#:命令行中位置变量的个数'
~     
[root@localhost opt]# ./demo.sh 13 30
总和为:43
执行的脚本是:./demo.sh
执行的脚本个数是:2
[root#!/bin/bash
#This is my first shell-script
sum=0
sum=`expr $1 + $2`
echo "总和为:$sum"
echo "执行的脚本是:$0"
echo "执行的脚本个数是:$#"
echo "详细内容时:$*"

@localhost opt]# !vim
#!/bin/bash
#This is my first shell-script
sum=0
sum=`expr $1 + $2`
echo "总和为:$sum"
echo "执行的脚本是:$0"
echo "执行的脚本个数是:$#"
echo "详细内容是:$*"

[root@localhost opt]# ./demo.sh 13 30 
总和为:43
执行的脚本是:./demo.sh
执行的脚本个数是:2
详细内容是:13 30
[root@localhost opt]# ./demo.sh 13 30 22 22 22
总和为:43
执行的脚本是:./demo.sh
执行的脚本个数是:5
详细内容是:13 30 22 22 22

[root@localhost opt]# !vim
#!/bin/bash
#This is my first shell-script
sum=0
sum=`expr $1 + $2`
echo "是否执行成功:$?"
echo "总和为:$sum"		
echo "执行的脚本是:$0"
echo "执行的脚本个数是:$#"
echo "详细内容是:$*"

[root@localhost opt]# ./demo.sh 13 30 
是否执行成功:0	'0:表示执行成功,非0值:表示执行失败'
总和为:43
执行的脚本是:./demo.sh
执行的脚本个数是:2
详细内容是:13 30

[root@localhost opt]# !vim
#!/bin/bash
#This is my first shell-script
sum=0
sum=`expr $1 / $2`	'改变成除法'
echo "是否执行成功:$?"	'此变量要放在第一行,输出上一行命令执行后返回的状态'
echo "总和为:$sum"
echo "执行的脚本是:$0"
echo "执行的脚本个数是:$#"
echo "详细内容是:$*"

[root@localhost opt]# ./demo.sh 10 0
expr: division by zero	
是否执行成功:2	'显示失败'
总和为:
执行的脚本是:./demo.sh
执行的脚本个数是:2
详细内容是:10 0

2、预定义变量

$#:命令行中位置变量的个数

$*:所有位置变量的内容

$?:上一条命令执行后返回的状态,当返回状态值为0时表示正常,非0值表示执行异常或出错

$0:当前执行的进程/程序名

分析脚本

[root@localhost opt]# vim ddd.sh
#!/bin/bash
TARFILE=beifen-`date +%s`.tgz  '设置变量名称,+%s表示从1970至今经过的秒数,所以文件名不会出现重复的情况,就不会有被覆盖的风险'
tar zcvf $TARFILE $* &> /dev/null	'tar工具压缩输出到 /dev/null'
echo "已执行$0个脚本"	'当前执行的进程名'
echo "共完成$#个对象的备份"	'位置变量的个数'
echo "具体内容包括:$*"	'所有位置变量的内容'
~    
[root@localhost opt]# ./ddd.sh /etc/passwd /etc/shadow
已执行./ddd.sh个脚本
共完成2个对象的备份
具体内容包括:/etc/passwd /etc/shadow
'/dev/null:黑洞,数据到这里就消失了,无法找回'
[root@localhost opt]# date
Tue Nov 26 00:17:31 CST 2019
[root@localhost opt]# date +%s		'从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数(时间戳)'
1574698476
[root@localhost opt]# date +%F		'显示当前日期'
2019-11-26
[root@localhost opt]# date +%Y%m%d	'显示年月日'
20191126
[root@localhost opt]# date "+现在的时间是:%Y-%m-%d %H-%M-%S"
现在的时间是:2019-11-26 00-22-10
[root@localhost opt]# date "+三年前的时间是:%Y-%m-%d %H-%M-%S" -d "-3 year"
三年前的时间是:2016-11-26 00-23-31
[root@localhost opt]# date "+三个月后的时间是:%Y-%m-%d %H-%M-%S" -d "+3 month"
三个月后的时间是:2020-02-26 00-24-16
[root@localhost opt]# date "+十天后的时间是:%Y-%m-%d %H-%M-%S" -d "+10 day"
十天后的时间是:2019-12-06 00-24-48

[root@localhost opt]# 
%Y表示年
%m表示月
%d表示日
%H表示小时
%M表示分钟
%S表示秒
%s表示从 1970  1  1  00:00:00 UTC 到目前为止的秒数,相当于time函数
%w表示一周中的第几天。

猜你喜欢

转载自blog.csdn.net/weixin_47219935/article/details/107378834