1

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?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this works in itself. However it is part of a much larger script. I would like it to return the value and then exit the script. This is what I have: if [ ! $# == 2 ] then for option in "${perlScriptArray[@]}" do $perlScript "$1" "$option" exit done fi However, it does not work. Any thoughts?
Don't try to put multi-line scripts in comments, there's no formatting. If you need to clarify, add it to the question.
But that doesn't really make sense. If you exit the script there, it only runs the first command, not the whole array.
Maybe you want to stop the loop if any command gets an error? perlScript.pl "$user" "$option" || exit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.