-1

I have the below code where my if condition is not working in a simple shell script.

#!/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

In the if condition if I just do if [ "$mode" = "" ]; then somehow the code runs, but I don't know why the above if condition is failing.

13
  • 1
    IMHO, wouldn't "$mode" = dev_path should be "$mode" = "$dev_path" in your if condition? Commented Feb 5, 2020 at 9:43
  • yes i just changed but still not working Commented Feb 5, 2020 at 9:44
  • 2
    Why did u added java tag to it ? Commented Feb 5, 2020 at 9:44
  • because i am trying to run java code from this shell script if i just take the complete cd script, put it in shell script and then run it works...but a i am trying to make this code flexible then somehow its now working Commented Feb 5, 2020 at 9:45
  • 1
    no i am not passing argument Commented Feb 5, 2020 at 9:54

2 Answers 2

1

You should run your code by passing an argument to it(look at the statement mode=$1 in your code), since OP confirmed in comments that NO arguments are being passed so code's if condition is failing.

Run it like (only an example here):

./script.sh "/tmp/in"

Where script.sh is your shell script code.

Since OP mentioned in comments in case OP don't want to pass arguments in that case, I would say:

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="$1". But IMHO please go through complete need/requirement of your code/logic then only take some decision but this is IMHO.

Sign up to request clarification or add additional context in comments.

5 Comments

just want to know is there any other way around if i dont want to pass argument while running the script ?
@Andrew, Argument passing means you are making people free(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="$1". But IMHO please go through complete need/requirement of your code/logic then only take some decision but this is IMHO.
thanks i got it ...its working fine now...i have one more question actually where i want to make make this code little bit flexible ...i think i will create new question for this
@Andrew, Yes please one thread one question only, cheers and happy learning on this great site SO.
thanks :) i have created new question which is extend of this code. May be you can help quickly : stackoverflow.com/questions/60073491/…
1

In your script you set the value of mode to $1 which is the first argument to your script. Try calling it like:

$ myscript /tmp/in

where myscript is the name of your shellscript file.

2 Comments

is there any other way around if i dont want to pass argument while running the script ?
If you don't want to pass in an argument, then you cannot use mode as its value is $1. In which case, skip the if altogether and just do run $dev_path

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.