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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop through a file

Status
Not open for further replies.

anatazi

Programmer
Jun 24, 2002
33
0
0
US
hi all
i just have a quick question.
i am writing a script that will check the number of links for each file in the user's directory and output the appropriate message.

here it is:
#!/bin/sh

ls -l >outputfile

HERE IS MYQUESTION: how do I loop through each line of the file outputfile

for (...)
if $2>=2; then
echo "$9 has $2 links."
else
echo "$9 has one link>"
fi

done

thanks a lot
 
cat outputfile | while read LINE
do
...
...
...
done

where LINE is a variable representing each line.
HTH
 
UUOC!

while read LINE
do
...
...
...
done < outputfile vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi all, I tried both of your suggestions and they both work fine however, now my inside loop does not work. It seems that the
if $2>=2; then
echo &quot;$9 has $2 links.&quot;
else
echo &quot;$9 has one link>&quot;

goes looking for agruments in the command line. so the output I get is as follow.
has links.
has links.
has links.
has links.
has links.
has links.
has links.
has links.
has links.
Can someone please point out what I am doing wrong.
Thanks

 
ls -l /tmp | nawk '{ printf(&quot;%s has %d link%s\n&quot;, $9, $2, ($2 >= 2) ? &quot;s&quot; : &quot;&quot;)
;}'
vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top