Unix Shell Loops


Examples of shell loops from the command line. Note that this technique is a ksh/bash/zsh kind of thing. Tested specifically under bash, seems to apply to other reasonable ksh's though.

For loop, more like a foreach in perl

  $ for D in bin src local ; do
  > cp -R /usr/$D /usr/$D.bkp
  > done

  ...or...

  $ for D in bin src local; do cp -R /usr/$D /usr/$D.bkp; done

Timed while loop, control-c to exit

  $ while : ; do
  > echo "=="
  > date
  > ls -la *tgz
  > sleep 5
  > done

  ...or...

  $ while :; do echo "=="; date; ls -la .procmailrc; sleep 5; done


keywords: unix
date: 02/07/2005