0

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

4
  • It would make your run function a lot easier to read if you put each statement on a separate line. Commented Feb 5, 2020 at 10:24
  • import.$(basename "$1").properties.TODO? Commented Feb 5, 2020 at 10:26
  • @TomFenech sure i did this..@kamil i will try with import.$(basename "$1").properties.TODO Commented Feb 5, 2020 at 10:27
  • If it's one long command, don't forget to escape the newlines with \ (I edited to do that) Commented Feb 5, 2020 at 10:31

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

i dont understand "$whatever_you_name_it" what should i put here ...can you please explain ?
Whatever you want in place of $2 in import."$2".properties.TODO
i think in this case should i use if condition instead of case beacause i am putting case condition togethere "$dev_path" | "$simu_path" so not understood
i think this syntax needs to changed in case condition dev|simu ? and also what argument should i pass while running the script ? previous i was running like like ./script.sh "/tmp/in" and the main problem is i dont want to change dev_path or simu path as this path i am pasiing in argument while running and it can be little bit different
i have updated my answer ..as i said what if my dev_path and simu_path are like this then i think this will not work..
|
1

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

9 Comments

the main problem is i dont want to change dev_path or simu path as this path i am pasiing in argument while running and it can be little bit different.. i have mentioned this in question
Okay understood. Another option would be to put create two arrays. One with the paths and a second with the corresponding environments. You would need a function to determine the index in the paths array and use it to get the correct value from the environments array. Seems a bit ugly but would work.
can we just simply put simu or dev wherever in mode and pass it the same during running the script..for example ./script.sh "dev" or ./script.sh "simu" then it will put the dev or simu variable value wherever necessary in script ? because right now my dev_path and simu_path are not fixed
As long as there is a specific naming scheme in your paths, you could extract the value. Otherwise you need to bring the path and environment together somehow.
I updated my post. Simply spoken, you can extract the environment with a line like this: environment=$(echo $mode | sed -E "s/.*(dev|simu).*/\\1/")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.