命令替换
方法 | 语法格式 |
方法一 | `command` |
方法二 | $(command) |
`` 和$()两者是等价的,但推荐初学者使用$(),易于掌握;缺点是极少数UNIX可能不支持
$(())主要用来进行整数运算,包括加减乘除,引用变量前面可以加$,也可以不加$
例子
- 获取系统所有用户并输出
- 根据系统时间计算今年或明年
- 根据系统时间获取今年还剩下多少星期,已经过了多少星期
- 判断 nginx 进程是否存在,若不存在则自动拉起该进程
1).获取系统所有用户并输出
#!/bin/bash index=1 for user in `cat /etc/passwd |cut -d ":" -f 1` do echo "This is $index user: $user" index=$(($index+1)) done
输出
This is 1 user: root This is 2 user: bin This is 3 user: daemon This is 4 user: adm This is 5 user: lp This is 6 user: sync This is 7 user: shutdown This is 8 user: halt This is 9 user: mail This is 10 user: operator This is 11 user: games This is 12 user: ftp This is 13 user: nobody This is 14 user: systemd-network This is 15 user: dbus This is 16 user: polkitd This is 17 user: sshd This is 18 user: postfix This is 19 user: chrony This is 20 user: nscd This is 21 user: tcpdump This is 22 user: ntp This is 23 user: www This is 24 user: redis This is 25 user: openvpn This is 26 user: emqx This is 27 user: epmd
2).根据系统时间计算今年或明年
#!/bin/bash echo "This is $(date +%Y) year"; echo "This is $(($(date +%Y)+1)) year";
输出
This is 2022 year This is 2023 year
2).根据系统时间获取今年还剩下多少星期,已经过了多少星期
# example.sh echo "This year have passed $(date +%j) days"; echo "This year have passed $(date +%V) weeks"; echo "There is $((365-$(date +%j))) days before new year"; echo "There is $(((365-$(date +%j))/7)) weeks before new year";
输出
This year have passed 363 days This year have passed 52 weeks There is 2 days before new year There is 0 weeks before new year
4).判断 nginx 进程是否存在,若不存在则自动拉起该进程
#!/bin/bash nginx_process_num=$(ps -ef|grep nginx|grep -v grep|wc -l) if [ $nginx_process_num -eq 0 ];then systemctl start nginx fi
有类型变量
declare
命令和typeset
命令两者等价declare
、typeset
命令都是用来定义变量类型的
declare命令参数表
参数 | 含义 |
-r | 将变量设为只读 |
-i | 将变量设为整数 |
-a | 将变量定义为数组 |
-f | 显示此脚本前定义过的所有函数及内容 |
-F | 仅显示此脚本前定义过的函数名 |
-x | 将变量声明为环境变量 |
取消声明的变量:declare +r;declare +i;declare +a;declare +f;declare +F;declare +x;
例子
- 声明变量为只读类型
- 声明变量类型为整型
- 在脚本中显示定义的函数和内容
- 在脚本中显示定义的函数
- 将变量声明为环境变量
- 声明变量为数组
1).声明变量为只读类型
# example.sh string="Hello world"; declare -r string echo "string = $string" # 再次赋值将无法修改 string="zxcvadsf" echo "string = $string"
输出
string = Hello world example7.sh: line 7: string: readonly variable string = Hello world
2).声明变量类型为整型
# example.sh num1=100 num2=10 num3=$num1+$num2 echo $num3 declare -i num3 num3=$num1+$num2 echo "num3 = $num3"
输出
num3 = 100+10 num3 = 110
3).在脚本中显示定义的函数和内容 && 在脚本中显示定义的函数
declare -f declare -F
5).将变量声明为环境变量
# example7.sh echo $num4 # bash > declare -x num4=122 # example7.sh echo $num4
输出
#第一行为空 122
6).声明变量为数组
# example.sh declare -a array array=("jones" "mike" "kobe" "jordan") echo "array = ${array[@]}" echo "array[0] = ${array[0]}" echo "array[1] = ${array[1]}" echo "array length = ${#array[@]}" for v in ${array[@]} do echo $v done
输出
array = jones mike kobe jordan array[0] = jones array[1] = mike array length = 4 jones mike kobe jordan
Bash 数学运算之 expr
语法格式
方法 | 语法 |
方法一 | expr $num1 operator $num2 |
方法二 | $(($num1 operator $num2 )) |
expr 操作符对照表
操作符 | 含义 |
num1 | num2 | num1不为空且非0 ,返回num1 ;否则返回num2 |
num1 & num2 | num1不为空且非0,返回num1 ;否则返回0 |
num1 < num2 | num1小于num2 ,返回1 ;否则返回0 |
num1 <= num2 | num1小于等于num2,返回1 ;否则返回0 |
num1 = num2 | num1等于num2 ,返回1 ;否则返回0 |
num1 != num2 | num1不等于num2,返回1 ;否则返回0 |
num1 > num2 | num1大于num2 ,返回1 ;否则返回0 |
num1 >= num2 | num1大于等于num2,返回1 ;否则返回0 |
使用 expr 命令时,表达式中的运算符左右必须包含空格,如果不包含空格,将会输出表达式本身:
例子
练习
提示用户输入一个正整数num,然后计算1+2+3+…+num的值;必须对num是否为正整数做判断,不符合应当允许再此输入。
思路
- 通过 expr 操作可判断是否是整数(只有整数才能使用expr)
- 再进行查看操作数是否大 0
# example.sh #!/bin/bash while true do read -p "pls input a positive number :" num expr $num + 1 &> /dev/null if [ $? -eq 0 ]; then if [ `expr $num \> 0` -eq 1 ]; then for ((i = 1; i <= $num; i++)) do sum=`expr $sum + $i` done echo "1+2+3...+$num = $sum" exit fi fi echo "error, input enlegal" continue done
输出
> sh example9.sh pls input a positive number :10 1+2+3...+10 = 55
Bash 数学运算之 bc
- bc 是 bash 内建的运算器,支持浮点数运算
- 内建变量 scale 可以设置,默认为 0
bc操作符对照表
操作符 | 含义 |
num1 + num2 | 求和 |
num1 - num2 | 求差 |
num1 * num2 | 求积 |
num1 / num2 | 求商 |
num1 % num2 | 求余 |
num1 ^ num2 | 指数运算 |
> bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. scale=4 23.444/5.2 4.5084 3.56+35.4 38.96