# Check if a file exists if [ -f $file ] then echo "File exists" else echo "File does not exist" fi # Check if a command is successful if ls then echo "Command succeeded" else echo "Command failed" fi ``` 通过使用i
Now let’s perform a similar check,but with thetest [command: $ if [ -x a.out ]; then echo "File is executable"; else echo "File is not executable"; fi File is executable So, thetest‘sxflag proves if the file exists and is executable. ...
11. Bash File Testing #!/bin/bash file="./file" if [ -e $file ]; then echo "File exists" else echo "File does not exists" fi 1. 2. 3. 4. 5. 6. 7. Similarly for example we can use while loop to check if file does not exists. This script will sleep until file does exi...
但bash shell的if语句并不是这么做的。 bash shell的if语句会运行if后面的那个命令。如果该命令的退出状态码是0(该命令成功运行),位于then部分的命令就会被执行。如果该命令的退出状态码是其他值,then部分的命令就不会被执行,bash shell会继续执行脚本中的下一个命令。fi语句用来表示if-then语句到此结束。 #!/bi...
如果if语句后放入一个不能工作的命令,则状态码为非0,且bash shell会跳过then后面的语句。 [22:43:53 root@libin3 libin]# vim shell21 /bin/bash #this is result error if libin then echo "this is error" fi if date then echo "this is success" ...
# wget-url-check.shHTTP/1.1200OKGoogle.com is up 如果你想看多个网站的状态,使用下面的 shell 脚本: 代码语言:javascript 复制 # vi curl-url-check-1.sh #!/bin/bashforsiteinwww.google.com google.co.in www.xyzzz.comdoifwget--spider-S"$site"2>&1|grep-w"200\|301";then echo"$site is...
[root@openvpn test]# cat check_user.sh #!/bin/bash id $1 &> /dev/null if [ $? -eq 0 ];then echo "user $1 exists" else echo "user $1 not exists" fi [root@openvpn test]# chmod +x check_user.sh [root@openvpn test]# ./check_user.sh openvpn user openvpn exists [root...
首先,我们定义了四种颜色,并定义了三种日志输出函数,分别是error,info,warning,最后只调用了log::info函数和check_file函数。 #!/usr/bin/env bash#!author zskset -o errexitset -o pipefail# set -o xtrace# set output colorNC='\033[0m'RED='\033[31m'GREEN='\033[32m'YELLOW='\033[33m'BLUE=...
wc -l filename 输出 filename 的行数 其他 1、ps -ef | grep 命令详解: ps 命令的作用是显示进程信息的; | 符号,是个管道符号,表示ps 和 grep 命令同时执行; grep 命令是查找(Global Regular Expression Print),能使用正则表达式搜索文本,然后把匹配的行显示出来; ps命令有一些参数: -e : 显示所有进程...
[root@master ~]# xsync /opt/scp -bash: /usr/local/bin/xsync: /bin/bash^M: 坏的解释器: 没有那个文件或目录 [root@master ~]# sudo xsync /opt/scp === master === sending incremental file list sent 130 bytes received 18 bytes 296.00 bytes/sec total size is 49 speedup is 0.33 === ...