Search code examples
bashshellslurmflags

Move input arguments from bash script to executable


I am currently trying to write a bash script which runs my executable with some input parameters. Most of the input parameters are set inside the script, some of them are passed to the bash script and evaluated. What I need now is to generalise the script so if I want to set other inputs known by my executable then I would want to write them in the bash script input. In other words my bash script executes at the end the command:

./executable.o -L $1 -g $g -h $2 -w 0.01 -th $thread_num -m 0 -w 0.01 -r $r -op $operator -fun $4 -s $site -b 0 -scale 1 -ch 1  -mu 10 >& ${filename}.log

where the variables are calculate beforehand from the input parameters. I run the bash script as:

sh script.sh 14 0.8 0.05 0 3 0 16

(this is also called from another bash script which runs that script multiple time with different input). My problem is that my executable knows a lot of different input/flags, such as -hn,-hs,-gn,-gs,-dt,... and I don't want to define for each of them an explicit input to the script. Also I want sometimes want to run the script as is without the additional flags. Is there a way to execute the bash script as:

sh script.sh 14 0.8 0.05 0 3 0 16 -hn 1 -hs 0.2 -dt 0.1 

where the last parameters (-hn 1 -hs 0.2 -dt 0.1) are moved to the executable? My executable can also read the flags from an input.txt so I could move everything to a .txt file but if I would run hundreds of such scripts then I would have a mess with all those text files. I appreciate any help/suggestions! (to be more precise the bash script is a runner script on a cluster with jobarray support, so script.sh is run as a job array on slurm, but I don't think that matters much) I also post the real script here just in case: https://pastebin.com/xyF6ZbV8


Solution

  • If I understand correctly, you have 7 parameters that you handle in the script and all the others should just be given to you executable?

    Try this:

    ./executable.o [... all your stuff ...] "${@:8}"  >& ${filename}.log
    

    The "${@:8}" evaluates to the array of arguments you gave to your bash script, starting from position 8, separated by a whitespace.