Search code examples
bashqsub

create a comma separated list containing the names of all variables starting with a prefix


I am currently trying to write a Bash script that can submit multiple jobs to an SGE QSUB cluster and submit some environmental variables to each job.

In order to pass variables to each job, I need to list all the variables that I want to pass as a single string (in the form of a comma separated list such as 'var1={var1},var2={var2}').

I am wondering how to automate this process. Specifically, I would like to write a script that does the following:

1) collect the names of all variables that begin with a certain prefix 2) strip the prefix from those names 3) create a comma-separated list

As an example, consider the following script (where the prefix is jobvar):

jobvar_my_vector=(0.1 2.00 3.00)
jobvar_my_string="hello"
other_variable="yesyesyes"

Here the command would basically produce a variable list_of_variables which would be a string like: "my_string=${my_string},my_vector=${my_vector}"


Solution

  • You can use set command in POSIX mode to list all variable. Then use a pipe to awk to parse the variable and get your desired output.

    s=$(set -o posix; set | awk -F '(jobvar_|=)' '/^jobvar_/{printf "%s=${%s},", $2, $2}')
    echo "$s"
    my_string=${my_string},my_vector=${my_vector},
    

    In BASH you can also use compgen -v to get all variables:

    s=$(compgen -v | awk -F '(jobvar_|=)' '/^jobvar_/{printf "%s=${%s},", $2, $2}')
    echo "$s"
    my_string=${my_string},my_vector=${my_vector},