文章目录
1. shell 脚本语言
首行指定解释器:#!/bin/bash
特殊字符的含义
shell 控制流
if 循环
if condition1
then
command1
elif condition2
then
command2
else
commandN
fi
附:
1、如果else分支
没有语句执行,就不要写这个else
2、末尾的fi
就是if
倒过来拼写,后面还会遇到类似的
写成一行(适用于终端命令提示符):if [ $(ps -ef | grep -c "ssh") -gt 1 ]; then echo "true"; fi
例子:
#!/bin/bash
a=1
b=2
if [[ $1==a && $2==b ]]
then
echo 'This a test shell script.'
else
echo 'Please give some paras.'
fi
输出:
This a test shell script.
for 循环
for var in item1 item2 ... itemN
do
command1
command2
...
commandN
done
写成一行:for var in item1 item2 ... itemN; do command1; command2… done;
注:in列表是可选的,如果不用它,for循环使用命令行的位置参数,比如: