Bash
ping: 選項需要一個參數 – ‘c’ 和 ‘usage: ping’
嘗試執行以下程式碼,以顯示 IP 地址、接收和發送的數據包數。
#!/bin/bash interface=$1 ip=$2 packet=$3 if [[ $1 == "-h" ]] then echo "Perform ping command and get an automated result" echo "Perform like ./ping_lan.sh Interface IP Packet " echo "Example: ./ping_lan.sh eno1 192.168.7.4 10" exit fi ping -I $interface -q $ip -c $3 >> ping_summary.txt transmit=$(grep -o '[0-9]\+ packets transmitted' ping_summary.txt | grep -o '[0-9]\+') receive=$(grep -o ', [0-9]\+ received' ping_summary.txt | grep -o '[0-9]\+') packet_loss=$(grep -o ', [0-9]\+% packet loss' ping_summary.txt | grep -o '[0-9]\+') cat ping_summary.txt >> summary.txt rm ping_summary.txt if [[ $transmit -ne $packet ]] then echo -n "FAIL: transmit package is: " echo -n $transmit echo -n " expected: " echo $packet exit 1 fi if [[ $receive -ne $packet ]] then echo -n "FAIL: receive package is: " echo -n $receive echo -n " expected: " echo $packet exit 1 fi if [[ $packet_loss -ne 0 ]] then echo -n "FAIL: transmit package is: " echo -n $packet_loss echo -n "expected: " echo "0" exit 1 fi exit 0
我收到
argument requires an argument
. 我嘗試替換-c
為--c
“使用 ping”“使用 ping”是什麼意思,為什麼會顯示?
- 這個錯誤是什麼意思?
- 為什麼我會收到此錯誤?
- 我應該如何避免這個錯誤?
使用後
bash -x script_name
,我得到的輸出是:+ interface= + ip= + packet= + [[ '' == \-\h ]] + ping -I -q -c 5 Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface] [-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] [-W timeout] [hop1 ...] destination Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface] [-l preload] [-m mark] [-M pmtudisc_option] [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] [-W timeout] destination ++ grep -o '[0-9]\+ packets transmitted' ping_summary.txt ++ grep -o '[0-9]\+' + transmit= ++ grep -o ', [0-9]\+ received' ping_summary.txt ++ grep -o '[0-9]\+' + receive= ++ grep -o ', [0-9]\+% packet loss' ping_summary.txt ++ grep -o '[0-9]\+' + packet_loss= + cat ping_summary.txt do_ping_lan.sh: line 22: summary.txt: Permission denied + rm ping_summary.txt + [[ '' -ne '' ]] + [[ '' -ne '' ]] + [[ '' -ne 0 ]] + exit 0
我用來呼叫腳本的命令是:
sudo ./script_name interface ip packet sudo ./do_ping_lan.sh wlan0 192.168.0.1 5
您沒有正確呼叫腳本。看看輸出:
+ ping -I -q -c 5 Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
您可以清楚地看到任何參數都沒有值,因此
ping
會給您一個使用消息。稍後的,
+ cat ping_summary.txt do_ping_lan.sh: line 22: summary.txt: Permission denied
您不能
summary.txt
在目前目錄中寫入。您需要寫入臨時文件(請參閱 StackExchange 上與 相關的答案mktemp
)或其他可寫文件。