Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Sams Teach Yourself Shell Programming in 24 Hours    (ISBN: 0672314819)


 

 List Price: $19.99
 Our Price: $19.99
 Used Price: $4.99

 Release Date: 19 February, 1999
 Manufacturer: Sams (Paperback)
 Sales Rank: 12,559

 Author: Sriranga Veeraraghavan









More Info

 For Loop 2002-09-03 23:00:31
  sh 
Category: source:sh:general
Description: Shows how to make use of a for loop in your shell scripts.
Platform: unix
Author: mind
Viewed: 4582
Rating: 3.2/5 (15 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
for.sh
#!/bin/sh
#
# for_sh - mind (mind@metalshell.com)
#
# Basic example on the for structure
#
#       [ http://www.metalshell.com ]

# make dir a variable that stores /tmp
dir="/tmp"

# make str a variable that stores: This is our string
str="This is our string"

# make i an integer with 0 as the value
i=0

# for name [ in word ... ] do list done
#
# name: for each segment in a sequence of words name will store each
#       value apon each loop..
#
#       example: str="bleh blah bloop"; for name in $str; do echo $name; done
#       the example would show name being used to store each segment of the
#       string sequence and you would get a result of:
#
#       bleh
#       blah
#       bloop
#
# word: this will be your string sequence shown above: 
#       string="a b c d e f g h, etc.."
#   do: open's your for structure and every statement within it 
#       and 'done' will be executed with each loop.
# list: this is where your commands or statements are placed. This 
#       could also hold other for structure's, if statements, while 
#       loops, anything; possibilities are endless..
# done: close's the for structure but will only be passed when the for 
#       structure and statement's are 'done'..
#
# You can do the same thing below on a shell prompt without writing a script 
# to do it.. if you want to do a for statement on your command prompt you 
# can do it just like the below or you can do it in one command: for file in 
# /dir/*; do echo $file; done
#
# and if you want to do it in more than one you can do it the same as shown 
# below, example:
# mind$ for file in /tmp/*
# > do
# > echo $file
# > done
# /tmp/file1
# /tmp/file2
# ...
#
# The only problem with doing the above is that there is no room for error, 
# after you press enter you cannot go back and fix a problem, the for 
# structure must be broken and rewritten or finished and rewritten, either 
# way you will have to write it all over again.. So if you prefer to do the
# for structure on your command prompt i'd suggest doing it all in one line 
# that way you can fix your mistakes if need be..
#

# start our first for structure
echo "--- \"for file in \$dir/*\" example ---"

for file in $dir/*
do
        # The expr utility evaluates <expression> and writes 
        # the result on standard output.
        # expr <expression>

        # the below would increase $i by 1
        i=`expr $i + 1`
        echo "File #$i: $file"
done

# print a blank line for spacing..
echo

# change our integer back to 0 since we previously had used
# it and the value has changed..
i=0

# start our second for structure
echo "--- \"for seg in \$str\" example ---"
for seg in $str
do
        i=`expr $i + 1`
        echo "Part #$i of our string is: $seg"
done
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?