简单,如果条件在Shell脚本中不起作用

问题描述 投票:-1回答:2

我有以下代码,其中我的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 "$1"
}

dev_path="/tmp/in"

mode=$1

if [ "$mode" = "$dev_path"  ]; then
   run "$mode" 
fi

if条件下,如果我只是执行if [ "$mode" = "" ];,则代码会以某种方式运行,但我不知道为什么以上if条件会失败。

bash shell command
2个回答
1
投票

在脚本中,将mode的值设置为$1,这是脚本的第一个参数。尝试像这样调用它:

$ myscript /tmp/in

其中myscript是您的ShellScript文件的名称。


1
投票

您应该通过向其传递一个参数来运行代码,因为OP在注释中确认没有传递任何参数,所以如果条件失败,则返回代码。

运行方式(这里只是一个例子):

./script.sh "/tmp/in"

script.sh是您的Shell脚本代码。

© www.soinside.com 2019 - 2024. All rights reserved.