简单的 BASH 如果其他

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5525406/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:44:04  来源:igfitidea点击:

easy BASH If Else

bash

提问by jmituzas

Ok I cant get this to work for some reason

好的,由于某种原因,我无法使其正常工作

 if /etc/mysql/my.cfn exist
    then goto end;
else bash install.sh;

end exit;;

Thanks in advance, Joe

提前致谢,乔

回答by SiegeX

Check for non-existence and run install.shif true.

检查不存在,install.sh如果为真则运行。

[[ ! -e /etc/mysql/my.cfn ]] && bash install.sh

回答by Gordon Davisson

Here's a relatively literal translation:

这是一个比较直白的翻译:

if [ -e /etc/mysql/my.cfn ]; then
    exit # Note: bash does not have a goto command
else
    bash install.sh
fi

Or, eliminate the irrelevant then condition, and invert the test:

或者,消除不相关的 then 条件,并反转测试:

if [ ! -e /etc/mysql/my.cfn ]; then
    bash install.sh
fi

回答by glenn Hymanman

The :command in bash with no arguments is a no-op, so you can use that in an if-body if you need to do nothing.

:bash 中没有参数的命令是无操作的,所以如果你什么都不用做,你可以在 if-body 中使用它。

if something; then
    :
else
    do something else
fi

Of course, you'd normally want to write that as:

当然,您通常希望将其写为:

if ! something; then
    do something else
fi

or

或者

something || do something else

回答by lecodesportif

Your four lines in one line of Bash:

一行 Bash 中的四行:

[[ -e /etc/mysql/my.cfn ]] && exit || bash install.sh

Did you mean my.cnf?

你是说我的吗?cnf?

回答by 0xC0000022L

(One) correct syntax is:

(一)正确的语法是:

if [[ expression ]]; then
  command
else
  command
fi

The traditional style would be:

传统风格是:

if test -f filename; then
  command
fi