目录
一、基本语法
多命令执行逻辑
https://blog.csdn.net/u011630575/article/details/97613695
同时执行多条语句
command1 || {
statement1
statement2
statement3
}
if
if如果有else分支,则if分支里必须要执行命令,否则报错。
win_switch.sh: line 8: syntax error near unexpected token `elif'
win_switch.sh: line 8: `elif [ ${option} -eq 2 ];then'
for
https://blog.csdn.net/weixin_44175418/article/details/124444938
-
文件遍历:
for file in $(ls /opt/control-server/*console.log) do fileDate=${file##*/} fileDate=${fileDate%console*} fileDateSeconds=$(date -d "${fileDate}" +%s) echo "file:${file},fileDateSeconds:${fileDateSeconds}" # 10天之前的日志删除 if [ ${fileDateSeconds} -le $((${curDateSeconds} - 10*24*60*60)) ];then echo "发现过期(超过10天)日志文件${file},删除" sudo rm -f ${file} fi done
-
连续整数遍历
下面是批量开启防火墙端口的代码for port in {9901..9909} do result=$(firewall-cmd --query-port=$port/tcp --zone=public) if [[ "no" = $result ]];then firewall-cmd --permanent --add-port=$port/tcp fi result=$(firewall-cmd --query-port=$port/udp --zone=public) if [[ "no" = $result ]];then firewall-cmd --permanent --add-port=$port/udp fi done
包含子shell文件
http://static.kancloud.cn/dlover/linuxbashshell/2009401
. ./t1.fish #将t1.fish引入
eval命令
https://blog.51cto.com/u_10706198/1788573
exit与return
https://www.jb51.net/jiaoben/304275fob.htm
二、数值运算
- 变量自增
在Shell脚本中,你可以使用$((expression))或者let命令来进行自增操作。以下是两种方法的示例:
使用$((expression))方式(变量不需要加$符号):
#!/bin/bash
# 初始化变量
num=1
# 使用$((expression))进行自增
num=$((num + 1))
echo "自增后的值为: $num"
使用let命令:
#!/bin/bash
# 初始化变量
num=1
# 使用let进行自增
let num=num+1
echo "自增后的值为: $num"
两种方法都会使变量num的值自增1。
三、逻辑运算
- 与
if [ $a -eq 10 ] && [ $b -eq 20 ]; then echo "Both conditions are true" else echo "At least one condition is false" fi
- 或
if [ $a -lt 10 ] || [ $b -gt 20 ]; then echo "At least one condition is true" else echo "No condition is true" fi
0 条评论