Roflcakzorz
Roflcakzorz

Reputation: 37

Execute shell script w/ arguments stored in another file

I have a Python script that requires me to set many flags. I am trying to write some shell scripts to simplify the process of executing this Python script.

I would like to have two shell scripts, params.sh and run.sh. In params.sh, I set the shell variables to the desired values like so:

VAR1=val1
VAR2=val2
...

In run.sh, I want to take the variables that I've set in params.sh, create a unique name from the values of each variable, and then use those variables as the flags to execute the Python script. So in pseudocode, what I'd like to do is

PARAMS=get_params()
UNIQUE_NAME=make_unique_name(PARAMS)
python main.py --name=$UNIQUE_NAME --VAR1=${VAR1} --VAR2=${VAR2} ...

I'm stuck on how to create a shell function that takes an arbitrary number of arguments, and how I might process the list of params to execute the Python script as it appears in the last line of pseudocode. Any suggestions would be greatly appreciated.

Upvotes: 0

Views: 234

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295650

Consider:

#!/usr/bin/env bash

# read lines from file "vars", prefixing each with "--"
while IFS= read -r line; do
  args+=( "--$line" )
done <vars

# hash the contents of that variable
read -r _ unique_name _ < <(printf '%s\0' "${args[@]}" | openssl dgst -md5)

# use the hash as a name; then pass through the arguments
python main.py --name="$unique_name" "${args[@]}"

Upvotes: 1

Related Questions