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?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Michael
  • 37
  • 1
  • 7

1 Answers1

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
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 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? – Michael Apr 17 '20 at 20:46
  • 1
    Don't try to put multi-line scripts in comments, there's no formatting. If you need to clarify, add it to the question. – Barmar Apr 17 '20 at 20:58
  • But that doesn't really make sense. If you exit the script there, it only runs the first command, not the whole array. – Barmar Apr 17 '20 at 20:59
  • Maybe you want to stop the loop if any command gets an error? `perlScript.pl "$user" "$option" || exit` – Barmar Apr 17 '20 at 21:00