Solved

How to pass an array to a shell script?

Posted on 2015-01-07
5
171 Views
Last Modified: 2015-03-04
Hello,
I have a shell script which accepts 2 variables, (example below of how i execute it), how can i auotomate the script such that i can pass 10 sets of variables? (10 lines of 2 variable each) so that I dont have to execute the script 10 times?
Thank you,
shobha.
0
Comment
Question by:Rao_S
5 Comments
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 167 total points
ID: 40535620
One option would be to pass all of the values one after the other, as in

   Script.sh a1 a2 b1 b2 c1 c2

Then in the script

    while [ "$1" != "" ]; do
        Val1=$1
        Val2=$2
        # now process that pair of values

       # now remove the first 2 parms from list
       shift 2
    done
0
 

Author Comment

by:Rao_S
ID: 40535639
This is how i run it now...

$create_oem_user_emcli -p PPTEST -f TESTING

i need to pass about 80 sets of -p and -f...
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 166 total points
ID: 40536450
Applying  simon3270's suggestion to how you run it now:

while [ $# -ge 2 ]; do
        # now process that pair of values
        create_oem_user_emcli -p $1 -f  $2

       # now remove the first 2 parms from list
       shift 2
done


If you prefer to send in lines of 2 variable each, you might use:

while read p f; do
      # now process that pair of values                                                                                                                      
      create_oem_user_emcli -p $p -f  $f
done
0
 
LVL 48

Accepted Solution

by:
Tintin earned 167 total points
ID: 40536719
expanding on ozo's last suggestion, I'm guessing you wouldn't want to manually type out 80 pairs of parameters, so I'm assuming you'd have them in a file, in which case, you can do:

while read p f; do
      # now process that pair of values                                                                                                                       
      create_oem_user_emcli -p $p -f  $f
done </path/to/file-with-parameters

Open in new window

0
 

Author Closing Comment

by:Rao_S
ID: 40644372
Thank you very much, the script worked!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question