Previous Table of Contents Next


Module 49
false

DESCRIPTION

The internal false command always returns an exit status of one. It is used to guarantee a nonzero return code for a conditional statement. The shell considers a return code of zero as true and nonzero as false.

COMMAND FORMAT

Following is the general format of the false command.

     false
     let 1

The following alias is automatically set by the Korn shell when you log in:

     alias false='let 1'

FURTHER DISCUSSION

The false command lends itself well to shell programming. It is a sure-bet command that always returns a status of one (false). Thus you can program infinite loops and break out based on various conditions.

Common code using the false command resembles the following:

     FILE=$1
     until false
      do
      if [ -f "$FILE" ]
      then
         echo "\007 The file $FILE has been created!"
         break
      fi
      sleep 5
     done

This code will run until the FILE specified by the first parameter is created. When the file is created, the echo line is displayed and the loop is exited via the break command.


NOTE:  
The false command returns a 1. The shell uses 0 as a true condition. Most programming languages, such as C, use 1 or nonzero numbers as true conditions. The reason for this convention is that return codes from programs are 0 if the program completed successfully and nonzero if a problem occurred.

RELATED COMMANDS

Refer to the true command described in Module 142.

RETURN CODES

The false command always returns a nonzero return code.

APPLICATIONS

The most common use of the false command is in looping conventions. You can use a false command in an until loop statement to create an infinite (endless) loop. Some other condition, such as a case or if command, must execute a break or exit to leave the loop. The false command may also be useful in testing shell scripts. You can place the false command on if statements and while or until loops to test how a shell script performs in these conditions.

TYPICAL OPERATION

In this activity you use the false command to loop indefinitely while printing the time to your screen. Begin at the shell prompt.


C Shell
If you are using the C shell enter the shell by typing sh and pressing Return.

1.  Use the vi editor to create the following shell script. Type vi clock and press Return to enter the editor. After you have typed in the shell script code, exit vi by typing ZZ.
     CLR=`tput clear`               # set CLR variable to clear sequence
     until false                    # until loop return code = 0
      do
       echo "${CLR}\n\n\n"          # CLeaR the screen and print new-lines
       banner `date +%H:%M`         # Display time
       sleep 59                     # Sleep for 59 seconds
      done

BSD (Berkeley)
     CLR=`clear`                    # set CLR variable to clear sequence
     until false                    # until loop return code = 0
      do
       echo "${CLR}"                # CLeaR the screen and print new-lines
     echo " " `date`                # Display time
     sleep 59                       # Sleep for 59 seconds
     done

This shell will display the hours and minutes (date +%H:%M) in large letters (banner) in the center of your screen. BSD does not support the banner command; therefore, remove it.
2.  Change the modes to executable for the script by typing chmod 777 clock and pressing Return.
3.  To run your new clock command type clock and press Return.
4.  To stop your clock press Delete.

C Shell
If you entered sh press Ctrl-D to return to your csh.

5.  Turn to Module 135 to continue the learning sequence.


Previous Table of Contents Next