所以我想根据主机设置不同的路径,但遗憾的是它不起作用。这是我的剧本:
if [$HOSTNAME == "foo"]; then
echo "success"
else
echo "failure"
fi
这就是:
-bash: [foo: command not found
failure
我确定$ HOSTNAME是foo,所以我不确定问题是什么。虽然我很吵。任何帮助,将不胜感激!谢谢!
答案 0 :(得分:48)
用于比较shell中字符串的POSIX和便携式方法是
if [ "$HOSTNAME" = foo ]; then
printf '%s\n' "on the right host"
else
printf '%s\n' "uh-oh, not on foo"
fi
案例陈述可能更灵活,但是:
case $HOSTNAME in
(foo) echo "Woohoo, we're on foo!";;
(bar) echo "Oops, bar? Are you kidding?";;
(*) echo "How did I get in the middle of nowhere?";;
esac
答案 1 :(得分:0)
你在开幕式之后错过了一个空格。使用双引号“不解析”变量内容也很好:
if [ "$HOSTNAME" = "foo" ]; then
...
答案 2 :(得分:0)
#!/bin/bash
if [[ $(hostname) == "home" ]] || [[ $(hostname) == "home2" ]]; then
echo "True"
fi