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

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.

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

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

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

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

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.