我的shell脚本(二)-命令替换

文章目录

命令替换

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

例子1:

  • 获取系统用户所有用户并输出
[root@localhost ~]# cat /etc/passwd   //打印出用户
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
haha:x:666:1000::/home/haha:/sbin/nologin
xixi:x:1000:1001::/home/xixi:/bin/bash
admin1:x:1001:1002::/home/admin1:/bin/bash

使用cut 分隔 -d 指定分隔符 -f 指定第几段

[root@localhost ~]# cut /etc/passwd -d ":" -f 1
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix
haha
xixi
admin1
[root@localhost ~]# 

脚本部署:(在脚本里面遍历它)

#!/bin/bash
#
index=1  # 定义一个变量,第一个输出的

for user in `cut /etc/passwd -d ":" -f 1` # 在打印出来的用户中循环
do 
	echo "This is $index user:$user" #打印出第几个,
	index=$(($index+1)) #变量+1

done 

总结:$() 表示命令替换
	$(()) 2个括号表示算数计算
	
[root@localhost ~]# num1=13
[root@localhost ~]# num2=14
[root@localhost ~]# echo $(($num1+$num2))
27
[root@localhost ~]# 

例子2:

  • 根据系统时间就散今年或明年

    [root@localhost ~]# date 
    Sun Nov 17 14:08:32 CST 2019
    [root@localhost ~]# date +%Y
    2019
    [root@localhost ~]# 
    
    

计算明年:

[root@localhost ~]# echo $"This is $(date +%Y) year"
This is 2019 year
[root@localhost ~]# echo $"This is $(($(date +%Y)+1)) year"
This is 2020 year
[root@localhost ~]# 

例子3:根据系统时间获取今年还剩下多少星期,已近过了多少星期

思路分析:通过date +%j 我们可以看到已近过了多少天,那么/7 就可以得到多少天,还剩多少天,就可以用365 去减已近过的天数

[root@localhost ~]# echo "This year has passed $(date +%j) days"     //打印已近过了多少天
This year has passed 321 days
[root@localhost ~]# echo "This year has passed  $(($(date +%j)/7)) weeks"
This year has passed 45 weeks
[root@localhost ~]# 

[root@localhost ~]# echo "This has befor new year $((365- $(date +%j))) days"   //打印还剩多少天
This has befor new year 44 days
[root@localhost ~]#

脚本部署:

#!/bin/bash
#
echo "This year has passed $(date +%j) days"
echo "This year har passed $(($(date +%j)/7)) weaks"
echo "This has befor new year $((365- $(date +%j))) days"

echo "This has befor new year $(((365- $(date +%j))/7)) weeks"

例子4:

  • 断定nginx进场是否存在,若不存在则自动拉起该进程

这里,我是变异安装的nginx的,所以,我的nginx启动方式如下:

服务控制方式,
nginx     //直接启动服务
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}
  nginx -s stop  //停止nginx

首先,我们查看下nginx的进程: 查看进程我们使用ps 命令

[root@localhost ~]# ps -ef | grep nginx
root       9490      1  0 15:00 ?        00:00:00 nginx: master process nginx
nginx      9491   9490  0 15:00 ?        00:00:00 nginx: worker process
root       9508   1415  0 15:02 pts/1    00:00:00 grep --color=auto nginx

总结:master 、worker是2个进程,grep是自己的,我们可以直接过滤掉grep  ,-v 取反。

[root@localhost ~]# ps -ef | grep nginx |grep -v grep
root       9490      1  0 15:00 ?        00:00:00 nginx: master process nginx
nginx      9491   9490  0 15:00 ?        00:00:00 nginx: worker process

脚本:

思路,通过我们过滤,发现进程只有2行了,

[root@localhost ~]# ps -ef | grep nginx |grep -v grep | wc -l
2
[root@localhost ~]# 

如果,过滤、统计后为0,说明master已近挂掉了,so 这种方法脚本很常用,

#!/bin/bash
#
nginx_processor_num=$(ps -ef|grep nginx |grep -v grep|wc -l)
if [ $nginx_processor_num -eq 0 ];then
	nginx
fi

验证下:

[root@localhost ~]# nginx -s stop
[root@localhost ~]# 
[root@localhost ~]# ps -ef | grep nginx |grep -v grep 
[root@localhost ~]# sh example_2.sh 
[root@localhost ~]# ps -ef | grep nginx |grep -v grep 
root       9555      1  0 15:20 ?        00:00:00 nginx: master process nginx
nginx      9556   9555  0 15:20 ?        00:00:00 nginx: worker process

总结:

`` 和$() 两者是等价的,但推荐初学者用$(),易于掌握;缺点是极少数UNIX可能不支持;$(())算数运算,$(())主要用于整数运算,包括加减乘除,引用变量前可以不加$:
$(((180+20)/100))

num1=13;num2=14
((num++));
((num--))
$(($num1+$num2*2))

发布了60 篇原创文章 · 获赞 3 · 访问量 2075

猜你喜欢

转载自blog.csdn.net/weixin_42313749/article/details/103109221