Solved

How to pass an array to a shell script?

Posted on 2015-01-07
5
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
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…
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.

728 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