bash 比较两个变量的 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14853695/
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
shell script comparing two variables
提问by suboss87
I have created a script for updating some ipaddress in iptables. Hereby I'm describing the issues which I'm facing with that.
我创建了一个脚本来更新 iptables 中的一些 ipaddress。在此,我正在描述我面临的问题。
Issues:
问题:
- Comparison not happening between two variables within a script
- At the end of script i need to execute a command ie; service restart/stop etc and output of the commands should be visible while executing the script.
- 脚本中的两个变量之间不会发生比较
- 在脚本结束时,我需要执行一个命令,即;在执行脚本时,服务重启/停止等和命令的输出应该是可见的。
1) Here am comparing two variable strings:
1) 这是比较两个变量字符串:
BASE=172.31.0.0
CMD=172.31.1.0
if [[ "$CMD" == "$BASE" ]]; then
echo "ip are same"
else
echo "not matched"
fi
but there is no response/output while executing the above script. Here its not comparison is not happening..Kindly suggest a best solution to resolve this issue.
但是在执行上述脚本时没有响应/输出。这里没有进行比较。请提出解决此问题的最佳解决方案。
2) after executing the script I need to restart the iptables:
2)执行脚本后,我需要重新启动iptables:
BASE=172.31.0.0
CMD=172.31.1.0
if [[ "$CMD" == "$BASE" ]]; then
echo "ip are same"
else
echo "not matched"
fi
service iptables restart
iptables -nvL
A script should display the output of the last two lines (commands). Kindly suggest me the best solution and how to do this in a best way.
脚本应显示最后两行(命令)的输出。请向我建议最佳解决方案以及如何以最佳方式执行此操作。
采纳答案by darque
That's very odd. This should work, so if it's not working you forgot to mention something important.
这很奇怪。这应该有效,所以如果它不起作用,你就忘记提及一些重要的事情。
How is this script being executed? Do you simply type ./script or is it executed by some service (like cron)?
这个脚本是如何执行的?您是简单地输入 ./script 还是由某些服务(如 cron)执行?
Here are some of suggestions to debug:
以下是一些调试建议:
Sanity check: see if bash works (perhaps your login shell isn't bash, so you didn't notice). Run this at the terminal:
/bin/bash -c 'echo hello world'It prints hello world, right? How about this:
/bin/bash -c 'BASE=172.31.0.0; CMD=172.31.1.0; if [[ "$CMD" == "$BASE" ]]; then echo "ip are same"; else echo "not matched"; fi'If any of the above doesn't work, you have a problem with your bash installation.
Instead of executing your script with ./script.sh, run it like this:
/bin/bash script.shNothing? Run this:
file script.shIf it ends with something like "with CRLF line terminators", then cdarke nailed it: the file was created on Windows with an improper tool. Recreate it on Linux or use dos2unix. But anyway, I doubt it because with a CRLF-ending file I get this printed:
bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directoryInstead of nothing at all.
Put those this line on the beginning of the file:
set -x(below #!/bin/bash, if you have it). This ensures a debugging trace will be printed, showing each command as it is executed.
If still there is nothing shown.. put this at your script (below set -xif you put it):
touch /tmp/hi-this-is-strangeThen check if there is a /tmp/hi-this-is-strangefile after you run the script.
健全性检查:查看 bash 是否有效(也许您的登录 shell 不是 bash,所以您没有注意到)。在终端运行这个:
/bin/bash -c 'echo hello world'它打印你好世界,对吧?这个怎么样:
/bin/bash -c 'BASE=172.31.0.0; CMD=172.31.1.0; if [[ "$CMD" == "$BASE" ]]; then echo "ip are same"; else echo "not matched"; fi'如果上述任何一项不起作用,则您的 bash 安装有问题。
不要使用 ./script.sh 执行脚本,而是像这样运行它:
/bin/bash script.sh没有?运行这个:
file script.sh如果它以“带有 CRLF 行终止符”之类的结尾,那么 cdarke 就会指出:该文件是在 Windows 上使用不正确的工具创建的。在 Linux 上重新创建它或使用 dos2unix。但无论如何,我对此表示怀疑,因为使用 CRLF 结尾的文件,我打印了以下内容:
bash: ./script.sh: /bin/bash^M: bad interpreter: No such file or directory而不是什么都没有。
将这一行放在文件的开头:
set -x(在#!/bin/bash下方,如果有的话)。这确保将打印调试跟踪,显示执行时的每个命令。
如果仍然没有显示任何内容......把它放在你的脚本中(如果你把它放在set -x下面):
touch /tmp/hi-this-is-strange然后在运行脚本后检查是否有/tmp/hi-this-is-strange文件。

