我在脚本下面运行:
#!/bin/bash
ps ax | grep -q [v]arnish
if [ $? -eq 0 ];then
echo varnish is running...
exit 0
else
echo "Critical : varnish is not running "
exit 2
fi
Run Code Online (Sandbox Code Playgroud)
输出就像 ::
[root@server ~]# sh -x check_varnish_pro.sh
+ ps ax
+ grep -q '[v]arnish'
+ '[' 0 -eq 0 ']'
+ echo varnish is running...
varnish is running...
+ exit 0
Run Code Online (Sandbox Code Playgroud)
当我在命令行中运行相同时,我的退出状态为 1:
[root@server ~]# ps ax | grep -q [v]arnish; echo $?
1
Run Code Online (Sandbox Code Playgroud)
这种情况就像服务器中没有安装清漆。此脚本在安装了 varnish 的服务器中运行良好。
为什么使用脚本和命令行运行时退出状态不同?如何改进这个脚本?
我有一个在服务器中运行的脚本,它将创建许多子进程(大约 800 个)。我想一口气把他们全部杀光。以下是ps
信息。
root 26363 0.0 0.0 119216 1464 ? Ss Mar02 0:00 SCREEN -S website_status
root 26365 0.0 0.0 108472 1844 pts/12 Ss Mar02 0:00 \_ /bin/bash
root 4910 0.0 0.0 161684 1956 pts/12 S Mar02 0:00 \_ su webmon
webmon 4939 0.0 0.0 108472 1924 pts/12 S+ Mar02 0:00 \_ bash
webmon 1094 3.4 0.0 107256 2432 pts/12 S 05:37 2:26 \_ sh /home/webmon/scripts/for_html/website/website_status.sh
webmon 5159 0.0 0.0 100956 1288 pts/12 S 05:37 0:00 \_ mysql …
Run Code Online (Sandbox Code Playgroud) 当 awk 接收“0”作为输入时,它在某些情况下的行为会有所不同。代码如下:
var=$1
echo ""; echo -n 'o/p of $1=$1 ==>'; echo $var | awk '$1=$1'
echo "";echo -n 'o/p of {$1=$1;print} ==>';echo $var | awk '{$1=$1;print}'
echo "";echo -n 'o/p of $1==$1 ==>';echo $var | awk '$1==$1'
echo "";echo -n 'o/p of {$1==$1;print} ==>';echo $var | awk '{$1==$1;print}'
Run Code Online (Sandbox Code Playgroud)
带有“0”(数字零)的输出:
[root@host ~]# sh /tmp/te.sh 0
o/p of $1=$1 ==>
o/p of {$1=$1;print} ==>0
o/p of $1==$1 ==>0
o/p of {$1==$1;print} ==>0
[root@GORJALA ~]#
Run Code Online (Sandbox Code Playgroud)
带有“1”(第一) …
我试图将下面的命令(从文件中选择随机行)分配给一个变量,但不起作用。
givinv@87-109:~$ head -$((${RANDOM} % `wc -l < file` + 1)) file | tail -1
cower
givinv@87-109:~$
Run Code Online (Sandbox Code Playgroud)
在我尝试将其分配给变量时遇到的错误下方。
givinv@87-109:~$ VARIA=`head -$((${RANDOM} % `wc -l < file` + 1)) file | tail -1`
bash: command substitution: line 1: unexpected EOF while looking for matching `)'
bash: command substitution: line 2: syntax error: unexpected end of file
bash: command substitution: line 1: syntax error near unexpected token `)'
bash: command substitution: line 1: ` + 1)) file | tail -1'
-l: command …
Run Code Online (Sandbox Code Playgroud) 我while
在脚本中运行这个循环来获取mysqldump
和压缩它,但exit
如果脚本table
不存在,我想要脚本。以下是我尝试过的。
while read TABLES; do
sudo mysqldump $DB $TABLES | gzip -f > $DB.$TABLES.sql.gz
if [ $? != 0 ]; then
echo "mysqldump Query executed with error !!"
exit 1
fi
done < file
Run Code Online (Sandbox Code Playgroud)
但这将给出 的退出状态gzip -f
,而不是mysqldump
。我知道mysqldump
如果我不在gzip
那里使用,我可以获得退出状态,但是在这种方法中有什么方法可以获得退出 ststusmysqldump
吗?
如何infinite_number_loop
在bash中运行a?,如下所示:
for i in infinite_number_statement
do date +%Y-%m-%d -d "$i day ago" 2>&1 > /dev/null
if [ $? -ne 0 ]
then echo $i
fi
done
Run Code Online (Sandbox Code Playgroud)如果我在本地计算机的终端中运行此循环,是否有任何问题?(我用for
循环尝试了一个大的随机数范围,并被挂起一次。)
shell-script ×4
bash ×3
exit-status ×2
process ×2
awk ×1
kill ×1
linux ×1
pipe ×1
ps ×1
variable ×1