![](/img/trans.png)
[英]Binary operator expected error when running a bash script against a directory other than the one the script is currently located in
[英]Binary operator expected error while running a while loop in bash
翻譯:博士
檢查給定的 PID 是否正在運行,如果是,則終止該進程。
count=0
while [[ "$count" -le 3 && ps -p $pid > /dev/null ]];
do
kill -9 $pid
count=$(( $count + 1 )):
done
為此,我收到一個錯誤:
第 8 行:[: -p:預期的二元運算符
我知道有幾個類似的問題,我已經嘗試過他們的解決方案,但似乎不起作用。
正如@kvantour 提到的, while
循環在邏輯上是不正確的。 這是腳本。 請注意,如果它無法終止進程,它會通知您,以便您調查根本原因。 該腳本將 PID 作為其第一個參數(例如$./kill-pid.sh 1234
)請注意,這適用於 bash 版本。 4.1+:
#!/usr/bin/env bash
if ps -p $1 > /dev/null
then
output=$(kill -9 $1 2>&1)
if [ $? -ne 0 ]
then
echo "Process $1 cannot be killed. Reason:"
echo "$output"
# This line is added per OP request, to try to re-run the kill command if it failed for the first time.
# kill -9 $1
fi
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.