![ping:选项需要一个参数 - 'c' 和 'usage: ping'](https://linux22.com/image/203301/ping%EF%BC%9A%E9%80%89%E9%A1%B9%E9%9C%80%E8%A6%81%E4%B8%80%E4%B8%AA%E5%8F%82%E6%95%B0%20-%20'c'%20%E5%92%8C%20'usage%3A%20ping'.png)
尝试运行下面的代码,以显示 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
和 'usage ping'替换
“usage 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
答案1
您没有正确调用脚本。查看输出:
+ 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
)或其他可写文件。