Pere Munar
Pere Munar

Reputation: 49

How to pass a variable into bash script without writing all the other input variables

I am writing a bash script that takes multiple input variables (7 at maximum), but only the first 4 are mandatory. The remaining 3 are optional and are set already within the script, unless specified when calling the script form the terminal prompt.

My problem comes when I want to change only one of the optional variables. If I want to change the 7th variable, for instance, I have to write down also the 5th and 6th variable values, because otherwise the script will think that this is the 5th argument, and not the 7th, of course. How can I do that?

Here is an example of what this part of my script looks like now:

#!/bin/bash

tstart=$1
tstop=$2
coord1=$3
coord4=$4
optional1=${5:-100}
optional2=${6:-50000}
optional3=${7:-4}

...

When calling the script, if I want to change the variable optional3, I have to write also the vaules of optional1 and optional2. Is there a way similar to:

$ myscript.sh var1 var2 var3 var7=vaule_7

(similar to what you do with default variable values in functions in python)

Thank you very much!!!

Pere.

Upvotes: 0

Views: 1173

Answers (2)

Mike
Mike

Reputation: 89

An approach that you might want to consider if you wish that have a script where the parameter position doesn't matter, you could have them named and then parse the list to assign the parameter to the right variable.

for instance you could have this:

while [ $# -gt 0 ]; do
    case $1 in
        -strt)  tstart=$2 ; shift  ;;
        -stp) tstop=$2 ; shift  ;;
        -crd1) coord1=$2 ; shift ;;
        -crd4) coord4=$2 ; shift ;;
        -opt1) optional1=$2 ; shift ;; 
        -opt2) optional2=$2 ; shift ;;
        -opt3) optional3=$2 ; shift ;; 
        (*) echo "$0: error - unrecognized option $1" 1>&2;;
    esac
   shift
done

what'll do is read the parameter in position 1 to know to which case it correspond and the assign value 2 to the variable, then shift all parameter from 1 to the left and you have another shift outside the case mean that parameter in position 3 is now in position 1 and parameter 4 is now in position 2 and so on until you run out of parameter to assign.

you can then call you script with something that would look like this:

myscript.sh -strt var1 -stp var2 -crd1 var3 -crd4 val4 -opt3 val7

you would have you 4 mandatory parameters plus your 3rd optional one and you could input them in any order

Upvotes: 0

chepner
chepner

Reputation: 531165

Sort of. First, change your script so that it uses environment variables instead positional parameters for the optional arguments.

#!/bin/bash

tstart=$1
tstop=$2
coord1=$3
coord4=$4
: ${optional1:=100}
: ${optional2:=50000}
: ${optional3:=4}

To call your script with a value for any of the three, precede the call with the assignment(s):

$ optional3=value_7 myscript.sh var1 var2 var3 var4

There is a shell option you can set in which any argument that looks like an assignment will be treated the same as a pre-command environment modifier.

$ set -k
$ myscript.sh var1 var2 var3 var4 optional3=value_7

Upvotes: 1

Related Questions