Shell 编程(一):Shell 变量的高级用法(二)

简介: Shell 编程(一):Shell 变量的高级用法
+关注继续查看

命令替换

方法语法格式
方法一`command`
方法二$(command)

`` 和$()两者是等价的,但推荐初学者使用$(),易于掌握;缺点是极少数UNIX可能不支持

$(())主要用来进行整数运算,包括加减乘除,引用变量前面可以加$,也可以不加$

例子

  1. 获取系统所有用户并输出
  2. 根据系统时间计算今年或明年
  3. 根据系统时间获取今年还剩下多少星期,已经过了多少星期
  4. 判断 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 命令两者等价
  • declaretypeset 命令都是用来定义变量类型的

declare命令参数表

参数含义
-r将变量设为只读
-i将变量设为整数
-a将变量定义为数组
-f显示此脚本前定义过的所有函数及内容
-F仅显示此脚本前定义过的函数名
-x将变量声明为环境变量

取消声明的变量:declare +r;declare +i;declare +a;declare +f;declare +F;declare +x;

例子

  1. 声明变量为只读类型
  2. 声明变量类型为整型
  3. 在脚本中显示定义的函数和内容
  4. 在脚本中显示定义的函数
  5. 将变量声明为环境变量
  6. 声明变量为数组

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 | num2num1不为空且非0 ,返回num1 ;否则返回num2
num1 & num2num1不为空且非0,返回num1 ;否则返回0
num1 < num2num1小于num2 ,返回1 ;否则返回0
num1 <= num2num1小于等于num2,返回1 ;否则返回0
num1 = num2num1等于num2 ,返回1 ;否则返回0
num1 != num2num1不等于num2,返回1 ;否则返回0
num1 > num2num1大于num2 ,返回1 ;否则返回0
num1 >= num2num1大于等于num2,返回1 ;否则返回0

使用 expr 命令时,表达式中的运算符左右必须包含空格,如果不包含空格,将会输出表达式本身:

例子

练习

提示用户输入一个正整数num,然后计算1+2+3+…+num的值;必须对num是否为正整数做判断,不符合应当允许再此输入。

思路

  1. 通过 expr 操作可判断是否是整数(只有整数才能使用expr)
  2. 再进行查看操作数是否大 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

目录
相关文章
|
2月前
|
Shell
Shell编程:流程控制与高级应用的深入解析
Shell 流程控制 使用Shell编程时,流程控制是非常重要的,它允许你根据条件执行不同的命令或者控制程序的执行流程。Shell支持一些基本的流程控制结构,包括条件语句和循环语句。 1、条件语句 if语句 if [ 条件 ]; then # 如果条件为真执行的命令 elif [ 其他条件 ]; then # 如果其他条件为真执行的命令 else # 如果所有条件都不为真执行的命令 fi 示例: #!/bin/bash read -p "请输入一个数字: " num if [ $num -eq 0 ]; then echo "输入的数字是零" elif [ $n
28 1
|
2月前
|
监控 Shell Linux
Linux Shell高级用法:优化和自动化你的工作流程
Linux Shell是一个非常强大的工具,可以用于自动化任务、处理文本和数据、进行系统管理等。在这篇文章中,我们将介绍一些Linux Shell的高级用法,帮助你更高效地利用Shell完成各种任务。
67 0
|
2月前
|
Shell Linux
Linux Shell 进阶:探索高级命令和脚本编程技巧
Linux Shell不仅仅是一个命令解释器,它还提供了许多强大的高级命令和脚本编程技巧,能够帮助用户更高效地管理系统和处理数据。在这篇文章中,我们将深入探讨Linux Shell的高级功能。
39 0
|
4月前
|
运维 监控 应用服务中间件
【运维知识高级篇】34道Shell编程练习题及答案(从基础到实战:基础+计算+判断+循环+控制与数组+实战进阶)(二)
【运维知识高级篇】34道Shell编程练习题及答案(从基础到实战:基础+计算+判断+循环+控制与数组+实战进阶)(二)
170 0
|
4月前
|
运维 Shell Linux
【运维知识高级篇】34道Shell编程练习题及答案(从基础到实战:基础+计算+判断+循环+控制与数组+实战进阶)(一)
【运维知识高级篇】34道Shell编程练习题及答案(从基础到实战:基础+计算+判断+循环+控制与数组+实战进阶)
158 0
|
4月前
|
运维 Shell 索引
【运维知识高级篇】超详细的Shell编程讲解5(普通数组+关联数组+抓阄项目)
【运维知识高级篇】超详细的Shell编程讲解5(普通数组+关联数组+抓阄项目)
51 0
|
4月前
|
运维 Shell
【运维知识高级篇】超详细的Shell编程讲解4(for循环+并发问题+while循环+流程控制语句+函数传参+函数变量+函数返回值+反向破解MD5)(二)
【运维知识高级篇】超详细的Shell编程讲解4(for循环+并发问题+while循环+流程控制语句+函数传参+函数变量+函数返回值+反向破解MD5)(二)
54 0
|
4月前
|
运维 Shell 数据安全/隐私保护
【运维知识高级篇】超详细的Shell编程讲解4(for循环+并发问题+while循环+流程控制语句+函数传参+函数变量+函数返回值+反向破解MD5)(一)
【运维知识高级篇】超详细的Shell编程讲解4(for循环+并发问题+while循环+流程控制语句+函数传参+函数变量+函数返回值+反向破解MD5)
87 0
|
4月前
|
运维 Shell 应用服务中间件
【运维知识高级篇】超详细的Shell编程讲解3(if判断+Shell菜单+case流程判断+批量创建删除用户+猜数字小游戏)
【运维知识高级篇】超详细的Shell编程讲解3(if判断+Shell菜单+case流程判断+批量创建删除用户+猜数字小游戏)
82 1
|
4月前
|
运维 Shell Perl
【运维知识高级篇】超详细的Shell编程讲解2(变量切片+统计变量长度+字串删除+字串替换+七种方法进行数值运算+整数比较+多整数比较+文件判断+字符串比对+正则比对+配合三剑客的高阶用法)(二)
【运维知识高级篇】超详细的Shell编程讲解2(变量切片+统计变量长度+字串删除+字串替换+七种方法进行数值运算+整数比较+多整数比较+文件判断+字符串比对+正则比对+配合三剑客的高阶用法)(二)
77 0
相关产品
云迁移中心
推荐文章
更多