Search code examples
bashperlcommand-substitution

Run perl script in bash array


I need to run a perl script in a bash array that will return certain values based on the string you provide. The perl script itself takes a user and a string and it will return the value based on the string you give it. For example. This is how the perl script works

$ perlscript.pl user disabled

This will run the perl script and will return whether or not the user is disabled. There are around 5 strings that it will accept. What I'm trying to do is run that script inside a bash array like so

declare -a perlScriptArray=('disabled' 'fullName' 'email' 'sponsor' 'manager')

Obviously this is not right and will just return the string that you provided. What I want is something like this inside the bash script.

declare -a perlScriptArray=('perlScript.pl disabled' 'perlScript.pl fullName' 'perlScript.pl email' 'perlScript.pl sponsor' 'perlScript.pl manager'

This however, does not work. The bash script itself takes the user as $1 and will pass that to the perl script which will run the string that you provided against the perl script. How should I go about doing this? And is this even possible?


Solution

  • Loop over the array calling the script with each element

    declare -a perlScriptArray=('disabled' 'fullName' 'email' 'sponsor' 'manager')
    user=$1
    
    for option in "${perlScriptArray[@]}"
    do
        perlScipt.pl "$user" "$option"
    done