如果条件在 shell 脚本中不起作用,则很简单
simple if condition not working in shell script
我有以下代码,其中我的 if
条件在简单的 shell 脚本中不起作用。
#!/bin/sh
run() {
cd /tmp/in/current; java -Dlog4j.configurationFile=/tmp/in/logging/log4j2_Importer.xml -Djava.security.egd=file:///dev/urandom -classpath /tmp/in/betl-runner/lib/*:/tmp/in/lib/* baag.betl.runner.Application --config /tmp/in/config/import.dev.properties.TODO --workflow import --inputDir ""
}
dev_path="/tmp/in"
mode=
if [ "$mode" = "$dev_path" ]; then
run "$mode"
fi
在 if
条件下,如果我只是执行 if [ "$mode" = "" ];
然后代码以某种方式运行,但我不知道为什么上述 if
条件失败。
在您的脚本中,您将 mode
的值设置为 </code>,这是脚本的第一个参数。尝试这样称呼它:</p>
<pre><code>$ myscript /tmp/in
其中 myscript
是您的 shellscript 文件的名称。
你应该 运行 你的代码通过传递一个参数给它(看看你代码中的语句 mode=
),因为 OP 在评论中确认没有传递任何参数所以代码的 if 条件失败了。
运行喜欢(这里只是举个例子):
./script.sh "/tmp/in"
其中 script.sh
是您的 shell 脚本代码。
由于 OP 在评论中提到如果 OP 在这种情况下不想传递参数,我会说:
Argument passing means you are allowing people/code(whoever
running/calling code to pass their own values), so if you don't want
that then do something like mode="Your_new_value" in your code in
place of mode="". But IMHO please go through complete
need/requirement of your code/logic then only take some decision but
this is IMHO.
我有以下代码,其中我的 if
条件在简单的 shell 脚本中不起作用。
#!/bin/sh
run() {
cd /tmp/in/current; java -Dlog4j.configurationFile=/tmp/in/logging/log4j2_Importer.xml -Djava.security.egd=file:///dev/urandom -classpath /tmp/in/betl-runner/lib/*:/tmp/in/lib/* baag.betl.runner.Application --config /tmp/in/config/import.dev.properties.TODO --workflow import --inputDir ""
}
dev_path="/tmp/in"
mode=
if [ "$mode" = "$dev_path" ]; then
run "$mode"
fi
在 if
条件下,如果我只是执行 if [ "$mode" = "" ];
然后代码以某种方式运行,但我不知道为什么上述 if
条件失败。
在您的脚本中,您将 mode
的值设置为 </code>,这是脚本的第一个参数。尝试这样称呼它:</p>
<pre><code>$ myscript /tmp/in
其中 myscript
是您的 shellscript 文件的名称。
你应该 运行 你的代码通过传递一个参数给它(看看你代码中的语句 mode=
),因为 OP 在评论中确认没有传递任何参数所以代码的 if 条件失败了。
运行喜欢(这里只是举个例子):
./script.sh "/tmp/in"
其中 script.sh
是您的 shell 脚本代码。
由于 OP 在评论中提到如果 OP 在这种情况下不想传递参数,我会说:
Argument passing means you are allowing people/code(whoever running/calling code to pass their own values), so if you don't want that then do something like mode="Your_new_value" in your code in place of mode="". But IMHO please go through complete need/requirement of your code/logic then only take some decision but this is IMHO.