Previous Table of Contents Next


Module 142
true

DESCRIPTION

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

COMMAND FORMAT

Following are the general formats of the true command.

     true
     :

C Shell
[/bin/]true
:
(1)

The colon command may also be used to return only a true exit status. In fact the Korn shell aliases the true command to : as shown in the following code:

     alias true=':'

FURTHER DISCUSSION

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

The most common code using the true command is probably the following:

     while true
      do
       sleep 60
        echo "Just another endless day(loop)!"
      done &

This code will run forever or until someone kills the process or shuts down the system. The following lines of shell code are equivalent:

     while :
      do
       sleep 60
       echo "Just another endless day(loop)!"
      done &

You could also code this with the until loop using the false command.


NOTE:  
The true command returns a 0. 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 false command described in Module 49.

RETURN CODES

The true command always returns a zero return code.

APPLICATIONS

The most common use of the true command is in looping conventions. You can use a true command on a while 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 true command may also be useful in testing shell scripts. You can place the true command on if and while loops to control the shell script and test how it performs in these conditions.

TYPICAL OPERATION

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

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 the shell script, exit vi by typing ZZ.
     CLR=`tput clear`
     while true
      do
       echo "{CLR}\n\n\n\n\n"
       banner "  `date +%H:%M`"
       sleep 59
      done
BSD (Berkeley) version
     CLR=clear
     while (1)
      do
       echo "





       "
       echo "     "`date`
       sleep 59
      done
This shell will display the hours and minutes (date +%H:%M) in large letters (banner) in the center of your screen. Since the first character is not a #, shell is executed as a Bourne shell script.
2.  Change the modes to executable for the script by typing chmod 777 clock and pressing Return.
3.  Now execute the new script by typing clock and pressing Return.
4.  To stop the script press Del (or Ctrl-C).
5.  Turn to Module 49 to continue the learning sequence.


Previous Table of Contents Next