Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Keep The TechnoWorldInc.com Community Clean: Read Guidelines Here.
Recent Updates
[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[Yesterday at 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 ] » Programming Zone » Shell Script
 Bourne/bash shell script for loop syntax
Pages: [1]   Go Down
  Print  
Author Topic: Bourne/bash shell script for loop syntax  (Read 2912 times)
Mark David
Administrator
Super Elite Member
*****



Karma: 185
Offline Offline

Posts: 1624

!!!Techno King!!!

fabulous_designer
View Profile WWW
Bourne/bash shell script for loop syntax
« Posted: March 16, 2007, 01:27:59 PM »


Bourne/bash shell script for loop syntax

A for loop allows a program to iterate over a set of values. For loops in a Bourne shell script (sh, ksh, bash, zsh, etc.) are a useful means of iterating through files or other lists. This recipe describes the for loop syntax and provides some examples.

The basic for loop syntax is:

Code:
for var in list 
do
   commands;
done

The list can be a specified set of values (1 2 3 4) or anything that will evaluate into a set of values (a wildcard expression will expand the matching filenames into a list). For example, '/usr/bin/[aeiou]*' will expand to the set of files in /usr/bin starting with a vowel (this, of course, comes up all the time). The commands enclosed between do and done will be executed once for each item in the list. The current value from the set can be accessed with the variable $var.

To separate a log file into multiple files based on the month (assuming that the log format contains the three letter month abbreviation):

Code:
#!/bin/sh 
logfile="/var/adm/messages"
for mon in Sun Mon Tue Wed Thu Fri Sat
do
   grep $mon $logfile > $logfile.$mon
done

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

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