SH: two variables in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SH: two variables in for loop
# 8  
Old 08-01-2008
Quote:
Originally Posted by danmero
KISS principle states that design simplicity should be a key goal and unnecessary complexity avoided. Smilie
Code:
paste -d ' ' list1 list2


That works for that specific case, but doesn't address the general problem as originally posed.
# 9  
Old 08-01-2008
Quote:
Originally Posted by Exolon
It seems you can't have multiple "x in y" terms in a for loop, so the next best thing is probably to rewrite the loop as an index iterator:
Code:
is=(a b c d)
js=(1 2 3 4)

for ((i = 0; i < 4; i++)) 
	do echo ${is[i]} ${js[i]}
done

It's not as nice, but still relatively easy to read, especially if you define your lists together. Smilie

That's OK, but not POSIX compatible.

And it would be better to use the actual size of the array, not a hard-coded value:
Code:
n=0
while [ $n -lt ${#is[@]} ]
do
  printf "%s %s\n" "${is[$n]}" "${js[$n]}"
  n=$(( $n + 1 ))
done

If you want it to be POSIX compliant:
Code:
is="a b c d"
js="1 2 3 4"
set -f
while [ -n "$is" ]
do
  set -- $is; i=$1; shift; is=$*
  set -- $js; j=$1; shift; js=$*
  printf "%s %s\n" "$i" "$j"
done

# 10  
Old 10-18-2008
what is the best way to learn write script?

I was inspired by the way cfajohnson solve up the problems. I have learn C some months ago, and like C above other languages which I needed to learn for school at that moment. But if I can use it to write scripts as well, it will be fantastic
# 11  
Old 06-30-2009
What if upper limit of array is not known

Hi,
I am taking the inputs given to script into an array like:
Code:
array1=( `echo " $@ "` )
array2=( `echo " $@ " |tr '[a-z]' '[A-Z]'` )

So when I want to use values of both the above arrays in Single for loop, then what upper limit do i set for below code :

Code:
for ((i = 0; i < 4; i++))
[I]echo ${array[i]} ${array1}

What should be there instead of 4 ?
Cause I don't know how many arguments user will give while executing the script..it could be 2 to 10 arguments.
like :
Code:
./script.sh arg1 arg2 arg3


Last edited by rbatte1; 09-21-2016 at 07:50 AM..
# 12  
Old 06-30-2009
Code:
$#

it will give the no of parameters passed

Last edited by rbatte1; 09-21-2016 at 07:50 AM..
# 13  
Old 06-30-2009
Quote:
Originally Posted by vidya.b27
Hi,
I am taking the inputs given to script into an array like:
array1=( `echo " $@ "` )

There's no need for echo and command substitution, and it will cause the script to fail if any of the arguments contain spaces.

Code:
array1=( "$@" )

Quote:
array2=( `echo " $@ " |tr '[a-z]' '[A-Z]'` )

The same here. In bash 4.0 you can do:

Code:
array2=( "${@^^}" )

Quote:
So when I want to use values of both the above arrays in Single for loop, then what upper limit do i set for below code :

for ((i = 0; i < 4; i++))
[I]echo ${array[i]} ${array1}

What should be there instead of 4 ?
Cause I dont know how many arguments user will give while executing the script..it could be 2 to 10 arguments.
like : ./script.sh arg1 arg2 arg3

I repeat, "use the actual size of the array":

Code:
number_of_elements=${#array[@]}

# 14  
Old 06-30-2009
One more, using set to array.
Code:
#!/usr/bin/ksh
set -A array1 $(cat list1)
set -A array2 $(cat list2)
n=${#array1[*]}
i=0
while (( i < n ))
do
     print "${array1[$i]} ${array2[$i]}"
    ((  i+=1  ))
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with variables in loop

Hello, please assist: users="test1 test2" keytest1="abcd" keytest2="dbcd" for i in $users do echo "$key${i}" > fileout done So, my objective is to take the current user (ie test1) in loop and echo its associated keyname (ie keytest1) variable to a file. The echo... (2 Replies)
Discussion started by: motdman
2 Replies

2. Shell Programming and Scripting

Loop through variables

I am pretty new to Unix. Trying to pick up some slack while a coworker is out on vacation. Basically the script is working fine however when I go through the testing phase and have to make mods it is a pita. Here is an example of what I have #!/bin/ksh if then echo... (8 Replies)
Discussion started by: biobill
8 Replies

3. Shell Programming and Scripting

Need to loop three variables

Hi, I have a out from a command i need to grep a report. For that i need loop 3 variable for that. How i can loop need help. Symmetrix ID : 123456 Masking View Name : Host16 Last updated at : 04:13:06 PM on Thu Mar 17,2011 Initiator Group Name : Host16 Host... (3 Replies)
Discussion started by: ranjancom2000
3 Replies

4. Shell Programming and Scripting

for loop with 2 variables

i am having a file contants as below my requirement is for file in `awk -F "," '{print $8,$9}'` <temp.txt echo "$file" echo "$file">test.txt a=`awk -F "," '{print $1}' `<test.txt b=`awk -F "," '{print $2}' `<test.txt but script reads , i want both the vales for further... (5 Replies)
Discussion started by: sagar_1986
5 Replies

5. Shell Programming and Scripting

Help with a For loop and variables

Greetings. I'm completely new to shell scripting and quickly trying to catch on. Here's my scenario: I have a text file, named ip.txt, containing IP addresses. I want to automatically perform a whois query on each address in the file, search the output for the country, and then put both the IP... (4 Replies)
Discussion started by: molnir
4 Replies

6. Shell Programming and Scripting

Need help in for loop with 2 variables

Hi, I need help on for loop need to add domain and IP In domain list 1.com 2.com 3.com In Ip list 1.1.0.1 1.2.0.1 1.3.0.1 1.com 1.1.0.1 2.com 1.2.0.1 3.com 1.3.0.1 I need to excute this command (4 Replies)
Discussion started by: ranjancom2000
4 Replies

7. Shell Programming and Scripting

Two variables in a for loop

Can we assign two variables in a for loop? I have an input file: 000301|20100502 835101|20100502 I want to read this file in a for loop and assign values to two different variables. I did this now but did not work for STORE,RUNDATE in `awk -F\| '{print $1,$2}' inputfile ... (4 Replies)
Discussion started by: gpaulose
4 Replies

8. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

9. Shell Programming and Scripting

Is there a better way I could have run this loop. (For loop with two variables)

Sorry for such a dreadful title, but I'm not sure how to be more descriptive. I'm hoping some of the more gurutastic out there can take a look at a solution I came up with to a problem, and advice if there are better ways to have gone about it. To make a long story short around 20K pieces of... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

using variables outside a while loop

Hi Guys, I have a scripts that uses a while loop to read a file and set 2 variables. How can I do this so the variables can be used outside the while loop ? Below is an example....# ./junk2 -m -e user EXE=user master=TRUE DB_TAG=PRODUCT In loop MST=MST=testsvr1:3110 In loop ARGS=... (2 Replies)
Discussion started by: Tornado
2 Replies
Login or Register to Ask a Question