[root@localhost shelltest]# ./ifdir.sh
Please input your direct: /home
you are right!
#!/bin/bash
read -p "Please input your direct: " -t 10 d
if [ -d "$d" ]
then
echo "you are right!"
else
echo
"It is not a direct"
fi
什么是输出重定向:https://blog.csdn.net/liucy007/article/details/90207830 grep 取反命令 grep -v
#!/bin/bash
apa_file=`ps aux | grep tomcat | grep -v grep`
if [ -n "$apa_file" ]
then
echo "$(date) apache is ok!" >> /opt/log/right_log/apache.log
else
echo "$(date) apache is error" >> /opt/log/error_log/apache.log
fi
ngi_file=`ps aux | grep nginx | grep -v grep`
if [ -n "$ngi_file" ]
then
echo "$(date) nginx is ok" >> /opt/log/right_log/nginxtest.log
else
systemctl start nginx &> /dev/null
echo "$(date) start nginx !!" >> /opt/log/error_log/nginxtest.log
fi
[root@localhost log]# cd error_log/
[root@localhost error_log]# ls
apache.log nginxtest.log
[root@localhost error_log]# vim apache.log
[root@localhost error_log]# vim nginxtest.log
[root@localhost error_log]# ps aux | grep nginx
root 7469 0.0 0.1 46344 1012 ? Ss 16:17 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 7470 0.0 0.1 46788 1972 ? S 16:17 0:00 nginx: worker process
root 7480 0.0 0.0 112708 972 pts/0 R+ 16:19 0:00 grep --color=auto nginx
[root@localhost error_log]#
[root@localhost error_log]# ps aux | grep nginx
root 7510 0.0 0.0 112708 976 pts/0 R+ 16:20 0:00 grep --color=auto nginx
[root@localhost error_log]# /bin/bas
base64 basename bash bashbug bashbug-64
[root@localhost error_log]# /bin/bas
base64 basename bash bashbug bashbug-64
[root@localhost error_log]# /bin/bash /opt/shelltest/ifapache.sh
[root@localhost error_log]# ps aux | grep nginx
root 7528 0.0 0.1 46344 1008 ? Ss 16:21 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 7529 0.0 0.1 46788 1968 ? S 16:21 0:00 nginx: worker process
root 7532 0.0 0.0 112708 972 pts/0 R+ 16:21 0:00 grep --color=auto nginx
[root@localhost error_log]#
定时执行脚本
https://blog.csdn.net/ycf921244819/article/details/80520217
多分支
[root@localhost shelltest]# vim iffile.sh
[root@localhost shelltest]# ./iffile.sh
please input a file nameiffull.sh
your are right!!!
[root@localhost shelltest]#
[root@localhost shelltest]# ./iffile.sh
please input a file nameyyyy
your input is a anther file
[root@localhost shelltest]# ./iffile.sh
please input a file name/home
your input is a direct!!!
[root@localhost shelltest]#
特别提醒:
在脚本里面read读取数据的时候,我们没法直接用删除键(Backpace)删除,这时候按ctrl+删除键就可以删除。 第二,写else语句的时候,没有then
[root@localhost shelltest]# ./iffile.sh
please input a file namesfdsafd^H^H^H^Hplease input content!!!