Operating System - HP-UX
1833323 Members
2978 Online
110051 Solutions
New Discussion

For loop -> reading variables from a text file.

 
SOLVED
Go to solution
Jeff Martin_3
Advisor

For loop -> reading variables from a text file.

I want to create a script that will run the opcnode command (OV) and feed it multiple entries from a text file with multiple columns. Each column will contain a certian variable.

>opcnode -add_node node_name=VARIABLE1 net_type=NETWORK_IP mach_type=MACH_OTHER group _name=VARIABLE2 node_label=VARIABLE3

In other words I want to feed the above command from a text file file which will contain VARIABLE1 VARIABLE2 VARIABLE3

I know how to do this in NT but not in UNIX.
8 REPLIES 8
curt larson_1
Honored Contributor

Re: For loop -> reading variables from a text file.

#!/usr/bin/sh

cat yourFile |
while read v1 v2 v3
do
opcnode -add_node node_name=v1 net_type=NETWORK_IP mach_type=MACH_OTHER group _name=v2 node_label=v3

if [[ "$?" != "0" ]] ;then
your message
fi
done
curt larson_1
Honored Contributor

Re: For loop -> reading variables from a text file.

oops forgot the '$' in front of the v's

opcnode -add_node node_name=v1 net_type=NETWORK_IP mach_type=MACH_OTHER group _name=v2 node_label=v3

should be

opcnode -add_node node_name=$v1 net_type=NETWORK_IP mach_type=MACH_OTHER group _name=$v2 node_label=$v3
Sridhar Bhaskarla
Honored Contributor

Re: For loop -> reading variables from a text file.

Hi,

while read VARIABLE1 VARIABLE2 VARIABLE3
do
opcnode -add_node node_name=$VARIABLE1 net_type=NETWORK_IP mach_type=MACH_OTHER group _name=$VARIABLE2 node_label=$VARIABLE3
done < file

Where file is your text file.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
S.K. Chan
Honored Contributor

Re: For loop -> reading variables from a text file.

Another way of doing this ..(say your file is /tmp/data)..
#!/usr/bin/sh

exec 0while read -r VAR1 VAR2 VAR3
do
opcnode -add_node node_name=$VAR1 net_type=NETWORK_IP mach_type=MACH_OTHER group _name=$VAR2 node_label=$VAR3
done
Jeff Martin_3
Advisor

Re: For loop -> reading variables from a text file.

What does this do?

if [[ "$?" != "0" ]] ;then
your message
fi
done
S.K. Chan
Honored Contributor

Re: For loop -> reading variables from a text file.

The $? is a special var that store the status (if you will) of the previously executed command. If $? is 0 (zero) it means a success, if non-zero it means failed return status. For example ..
$ date
$ echo $?
0
$ ls myfile
myfile not found
$ echo $?
2
So the body of the if statement will be executed if the previous executed command fails.
Renda Skandier
Frequent Advisor

Re: For loop -> reading variables from a text file.

If the text file is one line & you want to break it into columns try:
var1=`cat txtfiles | cut -c1-5`
var2=`cat txtfiles | cut -c6-10`

where c1 is the 1st char of line thru last char
& c6 is the the 1st char of 2nd field
Donny Jekels
Respected Contributor
Solution

Re: For loop -> reading variables from a text file.

This is easier accomplished on unix than on nt!

start with a while loop that reads the contents of a flat file into variables.

while read var1 var2 var3
do
opcnode -add_node node_name=${var1}net_type= mach_type= group _name=${var2} node_label=${var3}
if [[ $? -eq 0 ]]
then
# your job passed ok without any errors
echo "passed ${var1}"
else
echo "failed ${var1}"
fi
done < file


the $?

contain the exit code for the last command.
in unix, 0 (zero) always indicate a success, unless otherwise noted.

see the man page for the command, it always has the exit code towards the bottom.


live free or die
donny
"Vision, is the art of seeing the invisible"