Reputation: 3869
I have below shell script code which is working fine.
#!/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. 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.
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
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
Upvotes: 0
Views: 1145
Reputation: 654
As far as I know, bash unfortunately does not support constructs like associative arrays, which could be a possible solution, prior version 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. If you need to pass the complete path you have to change the "$dev_env" | "$simu_env" )
to "$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
Upvotes: 1
Reputation: 7277
Change import.dev.properties.TODO
to import."$2".properties.TODO
in run function. 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
Upvotes: 2