(一)、 语法
命令替换
(二)、例子
例子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
例子2
根据系统时间计算今年或明年
#!/bash/bin
#
echo "This year is $(date +%Y)"
echo "Next year is $(($(date +%Y) + 1))"
例子3
根据系统时间获取今年还剩下多少星期,已经过了多少星期?
#!/bash/bin
#
echo "This year have passed $(date +%j) days"
echo "This year have passed $(($(date +%j) / 7)) weeks"
echo "Thers is $((365 - $(date +%j))) days before new year"
echo "There is $(((365 - $(date +%j)) / 7)) weeks before new year"
例子4
判定nginx进程是否存在,若不存在则自动拉起该进程。
#!/bash/bin
#
nginx_process_num=$(ps -ef | grep nginx | grep -v grep | wc -l)
if [[ $nginx_process_num -eq 0 ]];then
systemctl start nginx
fi
注意⚠️
,也可以不加
() 用于命令替换。