【发布时间】:2011-12-19 19:26:46
【问题描述】:
我正在编写一个用于映射网络驱动器的脚本。但是,我们只想在机器具有特定 IP 地址的情况下尝试映射驱动器。下面列出的是我们正在尝试使用的代码 sn-p。
#!/bin/sh
IP="dig $HOSTNAME +short"
if [ $IP == *10.130.10.* ]; then
mount drive commands here
fi
if [ $IP == *10.130.11.* ]; then
mount drive commands here
fi
我无法让 IP 检查工作。有没有更好的方法来检查变量是否包含字符串,在这种情况下是 IP 地址的一部分?
this 发布中列出的信息没有帮助,因为它不起作用。
【问题讨论】:
-
我不认为
[可以测试复杂的子字符串。它只能做字符串相等,但只有一个=。 -
@KerrekSB:确实不能。在 bash 中,您可以使用
[[ $VAR == *pattern* ]](注意双括号)。或者您可以在任何与 POSIX 兼容的 shell(包括 bash)中使用case。 -
@derobert:哦,太好了,我不知道
[[——谢谢!
标签: macos bash shell scripting ip-address