4
我有一個url數組。我想打開它們,如果它們打開時沒有任何錯誤,則顯示狀態爲正在運行,否則未運行。如何通過從當前輸出中刪除所有其他消息來實現下面提到的所需輸出。使用curl和grep的shell腳本
#!/bin/ksh
urlArray=('http://url1:port1' 'http://url2:port2' 'http://url3:port3')
for url in "${urlArray[@]}"
do
result=`curl $url | head -1`
if (echo $result | grep '<?xml' >/dev/null 2>&1); then
echo Running
else
echo Not Running
fi
done
當前腳本的輸出是
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 12980 0 12980 0 0 711k 0 --:--:-- --:--:-- --:--:-- 0
Running
curl: (6) Couldn't resolve host 'url2:port2'
Not Running
curl: (6) Couldn't resolve host 'url3:port3'
Not Running
希望的輸出:
Running
Not Running
Not Running