How to loop through array who's name is entered in command line?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to loop through array who's name is entered in command line?
# 1  
Old 07-25-2012
How to loop through array who's name is entered in command line?

Say I have a ksh program called test.ksh which has several defined arrays inside it such as array1,array2,array3...These arrays contain strings.


I also have a method in the program:
Code:
for x in $1
do
....(#do something)
done

So, when the user enteres: ./test.ksh array1, I want to loop thruogh the contents of array1 using my for loop. However, with the for loop I have, the program takes the user's entry as a string and tries to perform the actions on that. How do I make it so the program will look for the array which the user enters?
# 2  
Old 07-25-2012
The code inside the for loop
Code:
temp=${x}[@] 
echo ${!temp}

# 3  
Old 07-25-2012
Quote:
Originally Posted by huaihaizi3
The code inside the for loop
Code:
temp=${x}[@] 
echo ${!temp}


What do you mean? Put those lines after "do" ?

I tried that and I get these errors:

Code:
test.ksh[37]: temp:  not found
test.ksh[38]: ${!temp}: bad substitution

# 4  
Old 07-26-2012
The code inside the for loop
This is a bash solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to accept command line argument as character or text if number is entered?

Hello Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex (6 Replies)
Discussion started by: aadityapatel198
6 Replies

2. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

3. Shell Programming and Scripting

Perl : accept multiple user entered command line arguemnts

I am trying to create a script that will accept multi input from the user (really just me), then execute those command on a remote device. My question is if the I enter "No" at the confirmation point "Are these statements correct y or n ?", what is the best way to go back and start over ? I... (3 Replies)
Discussion started by: popeye
3 Replies

4. Shell Programming and Scripting

Bash - Loading a command's output line by line into an array

I have been trying this a lot of different ways and haven't found too much online. Here's what I've got so far: j=0 declare -a first zero=(`cat $tmpfile`) for i in "${zero}" do command $i >> "${first}" ... (4 Replies)
Discussion started by: Azrael
4 Replies

5. UNIX for Dummies Questions & Answers

skipping echo stmnt first time a while loop entered

one more question. I want to skip the first echo statement the first time the loop gets entered while #keep prompting for more funds until selling price achieved do echo "You have inserted a total of ${total_inserted} cents. Please insert $total_remaining more cents" echo... (1 Reply)
Discussion started by: danieldcc
1 Replies

6. Shell Programming and Scripting

Loop assistance, getting array of random numbers and feeding to a command, how-to?

Hi all, I need a little assistance to complete the following script. I would like to take a file with a single number on each line and for each number, run it through a command. The loop will terminate once all numbers have been checked. Here is what I have thus far... COUNTER=`wc -l... (2 Replies)
Discussion started by: boolean2222
2 Replies

7. Shell Programming and Scripting

Repeat last entered command ?

Hi, how to do that ? I mean only print it but not execute. I'm using putty to interact with ksh. (in windows cmd up arrow does the job) thanks vilius (5 Replies)
Discussion started by: vilius
5 Replies

8. UNIX for Dummies Questions & Answers

Append 0 for single digit entered from command line

I have a script like this-- #!/bin/ksh echo "To pad a 0 before digits from 1-9" for i in $* do echo $i | sed 's//'0'/g' done I run this script as ksh name 1 2 23 34 The output should be 01 02 23 34 Help me in modifying this script. Thanks Namish (2 Replies)
Discussion started by: namishtiwari
2 Replies

9. Shell Programming and Scripting

getting rid of $ when entered at the command line

Hi everyone, Can someone possibly help me with this problem I am having please. I wrote a Korn shell script to manipulate currency amounts in a way that a person could use this script to determine the minimum number of coins required to make a certain amount. for example when entered on the... (2 Replies)
Discussion started by: bashirpopal
2 Replies
Login or Register to Ask a Question