Shell 脚本常用参考
echo cat read sed awk
cat
cat > /path/${TEMP}/account.conf<<EOF
export ${API_ID_HEADER}="${API_ID_INPUT}"
export ${API_KEY_HEADER}="${API_KEY_INPUT}"
EOF
# 单引号转译内容
# cat > /path/${TEMP}/account.conf<<'EOF'
sed
sed -i \
-e '/Port 22/a\Port 22222' \
-e '/^PasswordAuthentication.*yes$/s/yes/no/g' \
-e '/^PermitRootLogin.*no$/s/no/yes/g' \
-e '/^#PermitRoot.*yes$/s/#PermitRoot/PermitRoot/g' \
-e '/#ClientAliveInterval/s/#ClientAliveInterval 0/ClientAliveInterval 30/g' \
-e '/#MaxSessions/s/#MaxSessions 10/MaxSessions 100/g' \
/etc/ssh/sshd_config
awk
从文件获取 分隔符 = 如果第一列含有 DOMAIN 打印第二列
DOMAIN=$(cat /conf/account.conf | awk -F= '{if($1~"DOMAIN")print $2}')
read
read -p "请输入:" INPUT
case
case "$INPUT" in
1)
echo "1"
;;
2)
echo "2"
;;
*)
echo "其他"
exit 0
esac
if 判断
字符串对比判断
if [ "$INPUT" == "3" ]; then
echo "变量等于 3"
fi
判断变量是否存在
if [ ! -n "${INPUT}" ]; then
echo "变量不存在"
fi
判断目录是否存在
if [ -d /data/test ]; then
echo "目录存在"
fi
判断文件是否存在
if [ -f /data/test ]; then
echo "目录存在"
fi
服务器软件判断
if type docker-compose >/dev/null 2>&1; then
echo "已安装 docker-compose"
else
echo "未安装 docker-compose"
fi
判断是否是root
if [[ $EUID -ne 0 ]]; then
echo "非 root 用户"
exit 1
fi
判断 OpenVZ
if [[ -d "/proc/vz" ]]; then
echo -e "这是 OpenVZ,非 KVM"
exit 1
fi
数值比较判断
if [ $TIMES -eq 3 ]; then
echo "Times 大于 3"
fi
运算符
算术运算符
#!/bin/sh
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
val=`expr $b / $a`
echo "b / a : $val"
val=`expr $b % $a`
echo "b % a : $val"
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
关系运算符
#!/bin/sh
a=10
b=20
if [ $a -eq $b ]
then
echo "$a -eq $b : a is equal to b"
else
echo "$a -eq $b: a is not equal to b"
fi
if [ $a -ne $b ]
then
echo "$a -ne $b: a is not equal to b"
else
echo "$a -ne $b : a is equal to b"
fi
if [ $a -gt $b ]
then
echo "$a -gt $b: a is greater than b"
else
echo "$a -gt $b: a is not greater than b"
fi
if [ $a -lt $b ]
then
echo "$a -lt $b: a is less than b"
else
echo "$a -lt $b: a is not less than b"
fi
if [ $a -ge $b ]
then
echo "$a -ge $b: a is greater or equal to b"
else
echo "$a -ge $b: a is not greater or equal to b"
fi
if [ $a -le $b ]
then
echo "$a -le $b: a is less or equal to b"
else
echo "$a -le $b: a is not less or equal to b"
fi
关系运算符列表
布尔运算符
#!/bin/sh
a=10
b=20
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ $a -lt 100 -a $b -gt 15 ]
then
echo "$a -lt 100 -a $b -gt 15 : returns true"
else
echo "$a -lt 100 -a $b -gt 15 : returns false"
fi
if [ $a -lt 100 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
if [ $a -lt 5 -o $b -gt 100 ]
then
echo "$a -lt 100 -o $b -gt 100 : returns true"
else
echo "$a -lt 100 -o $b -gt 100 : returns false"
fi
字符串运算符
#!/bin/sh
a="abc"
b="efg"
if [ $a = $b ]
then
echo "$a = $b : a is equal to b"
else
echo "$a = $b: a is not equal to b"
fi
if [ $a != $b ]
then
echo "$a != $b : a is not equal to b"
else
echo "$a != $b: a is equal to b"
fi
if [ -z $a ]
then
echo "-z $a : string length is zero"
else
echo "-z $a : string length is not zero"
fi
if [ -n $a ]
then
echo "-n $a : string length is not zero"
else
echo "-n $a : string length is zero"
fi
if [ $a ]
then
echo "$a : string is not empty"
else
echo "$a : string is empty"
fi
文件测试运算符列表
#!/bin/sh
file="/var/www/tutorialspoint/unix/test.sh"
if [ -r $file ]
then
echo "File has read access"
else
echo "File does not have read access"
fi
if [ -w $file ]
then
echo "File has write permission"
else
echo "File does not have write permission"
fi
if [ -x $file ]
then
echo "File has execute permission"
else
echo "File does not have execute permission"
fi
if [ -f $file ]
then
echo "File is an ordinary file"
else
echo "This is sepcial file"
fi
if [ -d $file ]
then
echo "File is a directory"
else
echo "This is not a directory"
fi
if [ -s $file ]
then
echo "File size is not zero"
else
echo "File size is zero"
fi
if [ -e $file ]
then
echo "File exists"
else
echo "File does not exist"
fi
参数
循环
标准按次循环
for (( i=1;i<=3;i++ )); do
xxx
done
for 文件循环 套 while 比对循环
for LOG in $(ls -1 ${LOG_PATH}/*.log)
do
SIZE=`ls -l $LOG | awk '{print $5}'`
while [ $SIZE -gt $MAX_SIZE ]
do
sed -i '1,100000d' $LOG
SIZE=`ls -l $LOG | awk '{print $5}'`
done
done
评论区