Bash 警告 - 预期的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29178135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash warning - argument expected
提问by user986959
The bash script bellow takes a MAC address as a variable and finds the IP address corresponding to this MAC address.
下面的 bash 脚本将 MAC 地址作为变量,并找到该 MAC 地址对应的 IP 地址。
#!/bin/bash
read address
OUTPUT="$(nmap -sn -n /IP_ADDRESS_RANGE/ | grep -B 2 "$address" |
grep -e ^.*[0-9].[0-9].[0-9].[0-9] | awk '{print substr(find.sh: 7: [: =: argument expected
/IP_ADDRESS/
**:**:**:**:*:* **:**:**:**:**:** 0806 42: arp reply 192.168.1.1 is-at **:**:**:**:*:*
,22)}')"
if [ ${OUTPUT} = ""]; then
echo "not found"
else
echo "${OUTPUT}"
arpspoof -i wlan1 -t ${OUTPUT} /GATEWAY/
fi
Although it works fine and as expected I get a warning when I execute it and I want to find out how to fix it. The output I get after I enter the MAC address:
虽然它工作正常并且按预期执行,但我在执行它时收到警告,我想找出如何修复它。输入MAC地址后得到的输出:
if [ ${OUTPUT} = "" ]
^--- Required
回答by that other guy
There are multiple errors. shellcheckfinds the most obvious ones:
有多个错误。shellcheck找到最明显的:
First, you are missing a space here:
首先,您在这里缺少一个空格:
if [ "${OUTPUT}" = "" ]
^-- Here -^
Second, you're missing required quoting:
其次,您缺少必需的引用:
[ -z "${OUTPUT}" ]
This should be sufficient to get rid of the error you're seeing.
这应该足以消除您看到的错误。
Here are the other problems:
以下是其他问题:
Your regex tries to match an ipv4 address, but instead matches 8 character strings where every other character is a digit.
The regex is unquoted, so it could also accidentally match files in the directory and break
grep
You run the script with
sh yourscript
. This only works forsh
scripts. To runbash
scripts, usebash yourscript
.
您的正则表达式尝试匹配 ipv4 地址,但匹配 8 个字符串,其中每个其他字符都是数字。
正则表达式未加引号,因此它也可能不小心匹配目录中的文件并中断
grep
您使用
sh yourscript
. 这仅适用于sh
脚本。要运行bash
脚本,请使用bash yourscript
.
回答by Droopy4096
why not use:
为什么不使用:
[ "z${OUTPUT}" = "z" ]
instead? people have also use an ugly trick like this:
反而?人们还使用了这样一个丑陋的技巧:
##代码##to catch empty strings
捕捉空字符串