我正在編寫一個bash腳本,該腳本登錄到遠程節點並返回正在該節點上運行的服務。打印出不需要的太多參數錯誤
#!/bin/bash
declare -a SERVICES=('redis-server' 'kube-controller-manager' 'kubelet' 'postgres' 'mongod' 'elasticsearch');
for svc in "${SERVICES[@]}"
do
RESULT=`ssh 172.29.219.109 "ps -ef | grep -v grep | grep $svc"`
if [ -z ${RESULT} ]
then
echo "Is Empty" > /dev/null
else
echo "$svc is running on this node"
fi
done
現在的ssh 172.29.219.109 "ps -ef | grep -v grep | grep $svc"
節點上的輸出是::
postgres 2102 1 0 Jan29 ? 00:24:27 /opt/PostgresPlus/pgbouncer/bin/pgbouncer -d /opt/PostgresPlus/pgbouncer/share/pgbouncer.ini
postgres 2394 1 0 Jan29 ? 00:20:10 /opt/PostgresPlus/9.4AS/bin/edb-postgres -D /opt/PostgresPlus/9.4AS/data
postgres 2431 2394 0 Jan29 ? 00:00:01 postgres: logger process
postgres 2434 2394 0 Jan29 ? 00:07:15 postgres: checkpointer process
postgres 2435 2394 0 Jan29 ? 00:01:10 postgres: writer process
postgres 2436 2394 0 Jan29 ? 00:03:27 postgres: wal writer process
postgres 2437 2394 0 Jan29 ? 00:20:03 postgres: autovacuum launcher process
postgres 2438 2394 0 Jan29 ? 00:37:00 postgres: stats collector process
postgres 2494 1 0 Jan29 ? 00:08:12 /opt/PostgresPlus/9.4AS/bin/pgagent -l 1 -s /var/log/ppas-agent-9.4.log hostaddr=localhost port=5432 dbname=postgres user=postgres
postgres 2495 2394 0 Jan29 ? 00:11:25 postgres: postgres postgres 127.0.0.1[59246] idle
當我運行該腳本,我得到我想要的結果,但是我得到這似乎涉及到不想要的消息我在其中存儲我的結果的變量。
# ./map_services_to_nodes.sh
./map_services_to_nodes.sh: line 12: [: too many arguments
postgres is found on this node
的算法中,我使用即時通訊是::
- 搜尋我的陣列中定義的所有服務。
- 將結果存儲在變量中。
- 如果變量爲空,則表示服務未運行。
- 如果它不爲空,服務正在運行。
報價.......... –
http://shellcheck.net/本來會在沒有人類參與的情況下抓住你的。 –
(在另一點上,你最好使用'pgrep'而不是'ps | grep';爲此目的而構建,已經知道如何避免匹配本身) –