[英]how to assign variable in shell script
I have below shell script code which is working fine.我有下面的 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/runner/lib/*:/tmp/in/lib/* \
baag.runner.Application \
--config /tmp/in/config/import.dev.properties.TODO \
--workflow import \
--inputDir "$1"
}
dev_path="/data/etl-dev/in/eurex"
simu_path="/data/etl-simu/in/eurex"
mode=$1
case "$mode" in
"$dev_path" | "$simu_path" )
run "$mode"
;;
*) echo "error: invalid mode" >&2
exit 1
;;
esac
But currently i can run this code only on dev database as you can see in run function script the import.dev.properties.TODO
is set as dev.但是目前我只能在开发数据库上运行此代码,正如您在运行函数脚本中看到的那样, import.dev.properties.TODO
设置为 dev。 I want to make this flexible such that if the path is "/tmp/in/simu" for simu_path variable then the properties should be import.simu.properties.TODO
and for dev_path it should be import.dev.properties.TODO
so that it will run on the respective database.我想让它变得灵活,如果 simu_path 变量的路径是“/tmp/in/simu”,那么属性应该是import.simu.properties.TODO
而对于 dev_path 它应该是import.dev.properties.TODO
这样它将在相应的数据库上运行。
I am not sure if its possible to set parametarized variable here.我不确定是否可以在这里设置参数化变量。 For example something like this import.${varaible_here_dev_or_simu}.properties.TODO
例如像这样的东西import.${varaible_here_dev_or_simu}.properties.TODO
I want to keep the dev_path and simu_path as it is as it can be changed as i am passing this in argument我想保持 dev_path 和 simu_path 原样,因为我在参数中传递它时可以更改它
Change import.dev.properties.TODO
to import."$2".properties.TODO
in run function.在运行函数import.dev.properties.TODO
更改为import."$2".properties.TODO
。 And use it like this.并像这样使用它。
run "$mode" "$whatever_you_name_it"
And we could ref this a bit我们可以参考一下
#!/bin/sh
path="${1:-/tmp/in}" # set /tmp/in as default path
mode="${2:-dev}" # set dev as default mode
# or switch them if mode needs to be changed more recent
#path="${2:-/tmp/in}"
#mode="${1:-dev}"
run() {
cd "$path"/current
java -classpath "$path"/runner/lib/*:"$path"/lib/* baag.runner.Application \
-Dlog4j.configurationFile="$path"/logging/log4j2_Importer.xml \
--config "$path"/config/import.$1.properties.TODO" \
-Djava.security.egd=file:///dev/urandom \
--inputDir "$path/$1" \
--workflow import
}
case "$mode" in
"dev|simu") run "$mode" ;;
* ) echo "error: invalid mode" >&2; exit 1;;
esac
As far as I know, bash unfortunately does not support constructs like associative arrays, which could be a possible solution, prior version 4.据我所知,不幸的是 bash 不支持像关联数组这样的结构,这可能是一个可能的解决方案,在版本 4 之前。
If the paths for the environments all look like the same, you could write it like this.如果环境的路径看起来都一样,你可以这样写。
#!/bin/sh
base_path="/tmp/in"
dev_env="dev"
simu_env="simu"
run() {
cd /tmp/in/current; java -Dlog4j.configurationFile=/tmp/in/logging/log4j2_Importer.xml -Djava.security.egd=file:///dev/urandom -classpath /tmp/in/runner/lib/*:/tmp/in/lib/* baag.runner.Application --config /tmp/in/config/import.$1.properties.TODO --workflow import --inputDir "$base_path/$1"
}
mode=$1
case "$mode" in
"$dev_env" | "$simu_env" )
run "$mode"
;;
*) echo "error: invalid mode" >&2
exit 1
;;
esac
Note: In this implementation you would have to pass dev
or simu
to the script instead of the whole path.注意:在此实现中,您必须将dev
或simu
传递给脚本而不是整个路径。 If you need to pass the complete path you have to change the "$dev_env" | "$simu_env" )
如果需要传递完整路径,则必须更改"$dev_env" | "$simu_env" )
"$dev_env" | "$simu_env" )
to "$base_path/$dev_env" | "$base_path/$simu_env" )
"$dev_env" | "$simu_env" )
到"$base_path/$dev_env" | "$base_path/$simu_env" )
"$base_path/$dev_env" | "$base_path/$simu_env" )
UPDATE更新
Assuming the path structure and the environments are fixed, you can extract the environment with a simple regex and pass it to the function as the seconds parameter, like so:假设路径结构和环境是固定的,您可以使用简单的正则表达式提取环境并将其作为秒参数传递给函数,如下所示:
#!/bin/sh
dev_path="/data/etl-dev/in/eurex"
simu_path="/data/etl-simu/in/eurex"
prod_path="/data/etl-prod/in/eurex"
environments="(dev|simu|prod)"
run() {
cd /tmp/in/current; java -Dlog4j.configurationFile=/tmp/in/logging/log4j2_Importer.xml -Djava.security.egd=file:///dev/urandom -classpath /tmp/in/runner/lib/*:/tmp/in/lib/* baag.runner.Application --config /tmp/in/config/import.$2.properties.TODO --workflow import --inputDir "$1"
}
mode=$1
case "$mode" in
"$dev_path" | "$simu_path" )
environment=$(echo $mode | sed -E "s/.*${environments}.*/\\1/")
run "$mode" $environment
;;
*) echo "error: invalid mode" >&2
exit 1
;;
esac
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.