Shell用于批量,重复处理的环境。
第一行使用#!,指定解释器。
''和""的区别 ''里面的变量保持原有的值 ""将变量值替换
$[1+1]实现数值换算 + - * / **
列表有多种方式表示:
1. 直观的列表 user1 user2 user3
2. 替换的方式 $(seq 1 100) seq 1 100
{N…M}
3. 调用文件
$* 横序
$@顺序
$#统计个数
$?返回结果 0表示正确,2表示错误。
&& and 代表做完一个命令后执行另外一条命令,有一个错误则为1
|| or 代表其中一条执行为true,都为0
1.单个判断条件If/then
systemctl is-active psacct > /dev/null 2>&1
If [ $? -ne 0 ] ;then
sytemctl start psacct
fi
2.两个判断条件If/then/else
Systemctl is-active psacct > /dev/null 2>&1
If [ $? -ne 0 ];then
systemctl start psacct
else
systemctl stop psacct
fi
3.多个判断条件if/then/elif/then/else
systemctl is-active mariadb > /dev/null 2>&1
MARIADB_ACTIVE=$?
systemctl is -active postgresql > /dev/null 2>&1
POSTGRESQL_ACTIVE=$?
If [ “MARIADB_ACTIVE” -eq 0 ];then
mysq
elif[ “POSTGRESQL_ACTIVE” -eq 0 ];then
psql
else
sqlite3
fi
Case 如同elif
case “$1”in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
status
;;
*)
请珍惜劳动成果,支持原创,欢迎点赞或者关注收藏,你每一次的点赞和收藏都是作者的动力,内容如有问题请私信随时联系作者,谢谢!