954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with FOR loop

Hi, I am new in shell script and I road this script, but it does not give the output I want.

The output show look like this:
Instance on fac1 inst1 instance
Instance on fac2 inst2 instance
Instance on fac3 inst3 instance

Here is the script:

INSTID="inst1 inst2 inst3"
FANCID="fac1 fac2 fac3"

for instid in $INSTID 
do 
echo ""
done
   for  fancid in  $FANCID 
    do
echo "Instance on ${fancid} ${instid} instance"
done
exit 0


thx savage1

savage1
Newbie Poster
4 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Here is the fix:

INSTID="inst1 inst2 inst3"
FANCID="fac1 fac2 fac3"

for instid in $INSTID 
do 
echo ""
done
   for  fancid in  $FANCID 
   do
     echo "Instance on ${fancid} ${instid} instance"
   done
done
exit 0


You forgot to close one of the for loops.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

dont thnk that will work either, you are trying to nest a for loop inside of another for, correct?
should be somehting like this:

for instid in $INSTID 
do 
   echo ""
   for  fancid in  $FANCID 
   do
     echo "Instance on ${fancid} ${instid} instance"
   done
done
exit 0
sn4rf3r
Junior Poster
135 posts since Sep 2006
Reputation Points: 10
Solved Threads: 2
 

Ach, completely missed that first done before the second for.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Thank you for your suggestion. But, still don't work. Your code gives me the following output:

Instance on fac1 inst1 instance
Instance on fac2 inst1 instance
Instance on fac3 inst1 instance

Instance on fac1 inst2 instance
Instance on fac2 inst2 instance
Instance on fac3 inst2 instance

Instance on fac1 inst3 instance
Instance on fac2 inst3 instance
Instance on fac3 inst3 instance

But, I want the following result from the code:

Instance on fac1 inst1 instance
Instance on fac2 inst2 instance
Instance on fac3 inst3 instance

Thx

dont thnk that will work either, you are trying to nest a for loop inside of another for, correct? should be somehting like this:

for instid in $INSTID 
do 
   echo ""
   for  fancid in  $FANCID 
   do
     echo "Instance on ${fancid} ${instid} instance"
   done
done
exit 0
savage1
Newbie Poster
4 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Then use korn shell arrays and a single while loop as follows:

#!/bin/ksh

set -A INSTID inst1 inst2 inst3
set -A FANCID fac1 fac2 fac3

count=0

while [ $count -lt ${#INSTID[*]} ]
do
  echo "Instance on ${fancid[$count]} ${instid[$count]} instance"
  count=$(( $count + 1 ));
done
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Hi,

This works great. But, what if I am ready the INSTID and FANCID values from another file. I have main script and a data file. I put the values in data file.

Thx

Then use korn shell arrays and a single while loop as follows:

#!/bin/ksh

set -A INSTID inst1 inst2 inst3
set -A FANCID fac1 fac2 fac3

count=0

while [ $count -lt ${#INSTID[*]} ]
do
  echo "Instance on ${fancid[$count]} ${instid[$count]} instance"
  count=$(( $count + 1 ));
done
savage1
Newbie Poster
4 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Never mind. I get it working with data file.

Thx for your help.

Hi,

This works great. But, what if I am ready the INSTID and FANCID values from another file. I have main script and a data file. I put the values in data file.

Thx

savage1
Newbie Poster
4 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Glad to hear it. Good luck, and have fun.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You