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
 



UNIX Shells by Example (3rd Edition)    (ISBN: 013066538X)


 

 List Price: $49.99
 Our Price: $34.99
 Used Price: $24.50

 Release Date: 24 October, 2001
 Manufacturer: Prentice Hall (Paperback)
 Sales Rank: 5,922

 Author: Ellie Quigley









More Info

 While Loop 2002-04-19 15:32:19
  sh 
Category: source:sh:general
Description: Shell script example on while loops.
Platform: unix
Author: mind
Viewed: 14212
Rating: 4.3/5 (33 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

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

# while list do list done
#
# list: this is where your while structure will actually begin and determine
#       if the while statement should proceed and when the statement should end.
#   do: open's your while 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 while structure's, if statements, for structure's, anything;
#       possibilities are endless..
# done: close's the while structure but will only be passed when the while 
#       structure and statement's are 'done'.. So if your while statement should 
#       only proceed if the statement is true then when the statement is false 
#       the structure will be finished, and vise versa.
#
# Like the for structure the while structure can be done on your command prompt, 
# either in one line or in many lines, I also recommend you use the one line 
# statement rather than the multiple for the simple fact the multiple lined way 
# has no room for error..
#
# One line way -  
# i=0; y=10; while [$i -ne $y]; do echo "on number $i of $y"; i=`expr $i+1`; done
#
# Multiple lines:
#
# mind$ i=0
# mind$ y=10
# mind$ while [ $i -ne $y ]
# > do
# > echo "on number $i of $y"
# > i=`expr $i + 1`
# > done

# This half should be used in a while structure.
#
# The below are conditional expressions used to test file attributes and
# perform string and arithmetic comparisons. All expressions below are true if..
#
#       -a file (file exists)
#       -b file (file exists and is a block special)
#       -c file (file exists and is a character special)
#       -d file (file exists and is a directory)
#       -e file (file exists)
#       -f file (file exists and is a regular file)
#       -g file (file exists and is set-group-id)
#       -h file (file exists and is a symbolic link)
#       -k file (file exists and is sticky)
#       -p file (file exists and is a named pipe)
#       -r file (file exists and is readable)
#       -s file (file exists and has a size greater than zero)
#       -t fd   (file descriptor is open and refers to a terminal)
#       -u file (file exists and is set-user-id)
#       -w file (file exists and is writable)
#       -x file (file exists and is executable)
#       -O file (file exists and is owned by the current user)
#       -G file (file exists and is owned by the current user group)
#       -L file (file exists and is a symbolic link)
#       -S file (file exists and is a socket)
#       -N file (file exists and has been modified since last read)
#
#       file1 -nt file2 (file1 is newer by modification date than file2)
#       file1 -ot file2 (file1 is older by modification date than file2)
#       file1 -ef file2 (file1 and file2 have the same device and inode)
#
#       -o optname (shell option optname is enabled)
#
#       -z string (length of string is zero)
#       -n string (length of string is non-zero)
#
#       string1 == string2 (strings are equal)
#       string1 != string2 (string are not equal)
#       string1 < string2 (string1 sorts before string2)
#       string1 > string2 (string1 sorts after string2)
#
# The below are arithmetic binary operators
#
#       arg1 OP arg2
#       OP:
#               -eq (arg1 is equal to arg2)
#               -ne (arg1 is not equal to arg2)
#               -lt (arg1 is less than arg2)
#               -le (arg1 is less than or equal to arg2)
#               -gt (arg1 is greater than arg2)
#               -ge (arg1 is greater than or equal to arg2)

# A while structure will only proceed if the statement is true, so if 
# you had:
#
# while [ 1 != 1 ]; do echo "test"; done
#
# This would be an improper way of doing it, because in that example the
# statement will never be true. A good example would be:
#
# i=0; while [ $i != 10 ]; do echo "test"; i=`expr $i + 1`; done
#  or
# while [ 1 != 2 ]; do echo "test; done
#  or
# while [ 1 == 1 ]; do echo "test"; done
#
# The first example will only loop 10 times or however many times until $i 
# is equal to 10 or whatever the while structure is trying to compare $i to..
# However the last two examples will continue forever or until the program is
# interrupted or killed because 1 will never equal 2 and neither are
# variable's that can be changed in a future sense..
#

#
# Below are examples on how to use the while structure..
#

# In our first example we will set a low number, then set a high number (i/y)
# and start our while structure and with each loop increase our lower number 
# until it is equal to our higher number.

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

# define y as an integer with the value 10
y=10

echo "--- \"while [ \$i -ne \$y ]\" example ---"

# start our first while structure

while [ $i -ne $y ]
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 "On number $i of $y"
done

# In this example we are going to use the for structure in the while
# structure to try and make our while structure false.

# define str as a variable with a string of: test
str="test"

# define str2 as a variable with a string of: tes"
str2="tes"

# define alp as a variable with a string of: a b c d e f g...
alp="a b c d e f g h i j k l m n o p q r s t u v w x y z"

# print a blank line for spacing
echo

echo "--- \"while [ \"\$str\" -ne \"\$str2\" ]\" example ---"

# start our second while structure

while [ "$str" != "$str2" ]
do
      # for each letter or segment in our string sequence loop the
      # below statement
      for let in $alp
      do
        # append the next segment of our string sequence to str2
        tmp="$str2$let"

        echo "Attempting to match $tmp with $str"

        # try to match str with tmp
        if [ "$str" = "$tmp" ]
          then
           # print success

           echo "Appeneded $let to $str2 and found a match between \$str and \$str2"
           # change str2 so on the next loop our while structure will be false and end

           str2="$tmp"
           # break the for structure
           break
         fi
     done
done


# In this example we show a never ending loop that can be broken by
# user input.. We are going to base this on a password type scheme..

# define pass as a string with: testing
pass="testing"

# print a blank line for spacing
echo

echo "--- \"while [ 1 -eq 1 ]\" example ---"

echo "The password is $pass but you can enter misc things and see how the"
echo "never ending loop would work"
echo

# start our third while structure

while [ 1 -eq 1 ]
do
        # print the below without a newline character (terminating char)
        echo -n "Enter pass: "

        # read the input from stdin
        read psw

        # try to match the user input with our pass
        if [ "$psw" = "$pass" ]
        then
                # print success
                echo "Correct! continuing.."

                # break our while structure - this is needed because our
                # while structure will never be false so in return our while
                # structure will never end unless it is killed.
                break
        fi

        # print failure, this half would only be printed if the if statement
        # was false. ie. the pass the user supplied didn't match.
        echo "Incorrect, Please try again.."
        echo
done
Rate this code:
(Not Helpful)  (Very Helpful) 

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


Got Money?