Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for do\done x 2 1

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

On a Sun Solaris 8 system in a Bourne shell script I want to run parallel do loops. I kinda want it to work like a two arrays.

Code:
TITLES="title1 title2 title3"
FILES="file1 file2 file3"

for FILE in FILES
do
  [COLOR=green]# I would like to output matching title here[/color]
  ls $FILE
done

As the script iterates through each of the FILES I would like to output the corresponding title from TITLES.

What is the easiest way to do this?

Thanks,

Michael42
 

Try this:
Code:
#!/bin/ksh
set -A TITLES title1 title2 title3
set -A FILES file1 file2 file3
i=0
for FILE in ${FILES[*]}
do
  # I would like to output matching title here
  echo "$i ${FILE} ${TITLES[$i]}"
  (( i += 1 ))
done
[noevil]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
LKBrwnDBA,

That looks great!

I only have one issue...I have to write it as a Bourne shell script. :-(

I looked up the -A option in the man pages and it does not seem to be available for Bourne.

Any suggestions?


Thanks again,

Michael
 

sh or bash?

Don't know if sh supports arrays.

for bash use:
Code:
#!/bin/bash
TITLES=(title1 title2 title3)
FILES=(file1 file2 file3)
i=0
for FILE in ${FILES[*]}
do
  # I would like to output matching title here
  echo "$i ${FILE} ${TITLES[$i]}"
  (( i += 1 ))
done
[thumbsup2]



----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

Ok got it for sh:
Code:
#!/bin/sh
TITLES="title1 title2 title3"
FILES="file1 file2 file3"
i=1
for FILE in $FILES
do
  # I would like to output matching title here
  TTL=`echo "$TITLES"|cut -d' ' -f $i`
  echo "$i $FILE $TTL"
  i=`expr $i + 1`  
done

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
LKBrwnDBA,

Wow! Simple and brilliant. :)

Thanks for taking the time to post,

Michael42
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top