Solved

How to pass an array to a shell script?

Posted on 2015-01-07
5
169 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now