Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to pass an array to a shell script?

Posted on 2015-01-07
5
Medium Priority
?
191 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 20

Assisted Solution

by:simon3270
simon3270 earned 668 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 85

Assisted Solution

by:ozo
ozo earned 664 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 668 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
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…
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

971 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