Previous | Table of Contents | Next |
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= until false do if [ -f "$FILE" ] then echo "FILE=$1 until false do if [ -f "$FILE" ] then echo "\007 The file $FILE has been created!" break fi sleep 5 done7 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 . | |
|
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
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
| |
---|---|
C Shell | |
If you entered sh press Ctrl-D to return to your csh. | |
|
Previous | Table of Contents | Next |