Документ взят из кэша поисковой машины. Адрес оригинального документа : http://comet.sai.msu.ru/UNIXhelp/scrpt/scrpt2.6.2.1.html
Дата изменения: Fri Jan 17 12:43:20 1997
Дата индексирования: Tue Oct 2 02:05:15 2012
Кодировка:

Поисковые слова: п п п п п п п п п п п п п п
Examples of using the for statement

Examples of using the for statement

To take each argument in turn and see if that person is logged onto the system.

   cat snooper
   #!/bin/sh
   # see if a number of people are logged in
   for i in $*
   do
     if who | grep -s $i > /dev/null
     then
       echo $i is logged in
     else
       echo $i not available
     fi
   done

For each username given as an argument an if statement is used to test if that person is logged on and an appropriate message is then displayed.


To go through each file in the current directory and compare it with the same filename in another directory:

   #!/bin/sh
   # compare files to same file in directory "old"
   for i in *
   do
     echo $i:
     cmp $i old/$i
     echo
   done

If the list-of-words is omitted, then the loop is executed once for each positional argument (i.e. assumes $* in the for statement). In this case the loop will create the empty files whose names are given as arguments.

   #!/bin/sh
   # create all named files
   for i
   do
     > $i
   done

Some examples of command substitution in for loops:

   #!/bin/sh
   # do something for all files in current
   # directory according to time modified
   for i in `ls -t`
   do
     ...
   done
   # do something for all non-fred files.
   for i in `cat filelist | grep -v fred`
   do
     ...
   done
   # do something to each sub-directory found
   for i in `for i in *
     do
       if test -d $i
       then
         echo $i
       fi
     done`
   do
     ...
   done

[Home] [Search] [Index]