泷羽sec-星河飞雪-shell-5
免责声明
学习视频来自 B 站up主泷羽sec,如涉及侵权马上删除文章。
笔记的只是方便各位师傅学习知识,以下代码、网站只涉及学习内容,其他的都与本人无关,切莫逾越法律红线,否则后果自负。
泷羽sec官网:https://longyusec.com/
泷羽sec B站地址:https://space.bilibili.com/350329294
泷羽sec帮会:https://wiki.freebuf.com/front/societyFront?invitation_code=5a2005d9&society_id=239&source_data=2
文章目录
- 流程控制
- `if`
- `if else`
- `if elif else`
- 循环
- for
流程控制
if
if condition
then
command1
command2
...
commandN
fi
写成一行的话是这样,适用于终端命令行
if [ $(ps -ef | grep -c "bash") -gt 1 ]; then echo "true"; fi
-
$(...)
:这是命令替换的语法,它会执行括号内的命令,并将输出替换到当前位置。因此,$(ps -ef | grep -c "ssh")
会执行ps -ef
和grep -c "ssh"
的组合命令,并将输出(即包含"ssh"的进程数)替换到if
语句的条件判断部分 -
if [...]; then ...; fi
:这是if
语句的语法,用于根据条件执行不同的命令。如果条件([...]
内的表达式)为真,则执行then
后面的命令;否则,不执行
末尾的 fi 就是 if 倒过来拼写
if else
if condition
then
command1
command2
...
commandN
else
command
fi
if elif else
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
需要注意的是,在if else
的[……]
判断中,大于用-gt
,小于用-lt
。
但如果使用((……))
做判断的话,大于小于则可以直接用<
>
循环
for
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
写成一行是这样
for var in item1 item2 ... itemN; do command1; command2… done;
当变量值在列表里,for 循环即执行一次所有命令,使用变量名获取列表中的当前取值。
命令可为任何有效的 shell 命令和语句。in 列表可以包含替换、字符串和文件名。
in列表是可选的,如果不用它,for循环使用命令行的位置参数。
for i in 1 2 3 4 5
do
echo the var is $i
done
输出结果是
the var is 1
the var is 2
the var is 3
the var is 4
the var is 5
is $i
done
输出结果是
```shell
the var is 1
the var is 2
the var is 3
the var is 4
the var is 5