Solved

How to pass an array to a shell script?

Posted on 2015-01-07
5
168 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much, the script worked!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

771 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

9 Experts available now in Live!

Get 1:1 Help Now