Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Welcome to the TechnoWorldInc! Community!
Recent Updates
[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Computer / Technical Issues » Hardware
 Power of the Unix 'for' Loop
Pages: [1]   Go Down
  Print  
Author Topic: Power of the Unix 'for' Loop  (Read 846 times)
Stephen Taylor
TWI Hero
**********



Karma: 3
Offline Offline

Posts: 15522

unrealworld007
View Profile
Power of the Unix 'for' Loop
« Posted: July 15, 2007, 03:28:57 PM »



If you are a programmer or engineer working in a unix or linux environment, you will probably find the shell 'for' loop to be a handy tool for automating command line tasks.

Here are three examples of the 'for' loop. All the commands are in italics and should be entered on the command line, followed by a carriage return. Note that, after entering the initial 'for' line, you will get the secondary unix prompt (usually a '>').

1. Rename all ".old" files in the current directory to ".bak":

for i in *.old

do

j=`echo $i|sed 's/old/bak/'`

mv $i $j

done


Here, we looped thru all files with extension ".old", setting the variable "i" to be the file name we are currently looping thru. Then, between the "do" and "done", we have the body of the loop. On each pass, we echo the file name ("i") to the unix stream editor sed. Sed replaces the "old" with "bak" (so file "a.old" becomes "a.bak"), and saves the changed name to variable "j". Then, we use the unix move (mv) command to rename the original file (ex. a.old) to the new file (a.bak).



2. Change all instances of "yes" to "no" in all ".txt" files in the current directory. Back up the original files to ".bak".

for i in *.txt

do

j=`echo $i|sed 's/txt/bak/'`

mv $i $j

sed 's/yes/no/' $j > $i

done


In this case, we rename each file from ".txt" to ".bak". Additionally, we use sed a second time, on the contents of the original file (now with a ".bak" extension) and save the modified text back to the original name (with ".txt").



3. Loop thru a text file containing possible file names. If the file is readable, print the first line, otherwise print an error message:

for i in `cat file_list.txt`

do

if test -r $i

then

echo "Here is the first line of file: $i"

sed 1q $i

else

echo "file $i cannot be open for reading."

fi

done


Here, we loop thru the results of a command (in this case "cat"), rather than looping thru files in the directory. We also use an if statement with the "test" command to test for a condition (in this case, whether the file is readable).

Praveen Puri has been a unix programmer for the last 16 years. He has a blog called Unix Simplicity that is dedicated to unix shell scripting and awk programming.


Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.171 seconds with 24 queries.