Putting Comments in Shell Scripts


You can insert comments into your scripts to help you recall what they are for. Comments can also be used to document complex sections of a script, or to help other users understand how a script works. Providing good comments can make your programs more maintainable, meaning that they are easy to edit in the future. Adding comments does not affect the speed or performance of a shell program.

A comment begins with the # (pound) sign. When the shell encounters a statement beginning with #, it ignores everything from the # to the end of the line. (The only exception is when the first line in a file begins with #!. As discussed previously, this causes the shell to execute the file with a specific program.) This example shows how comments may be used to clarify even a relatively short script:

 #!/bin/sh # # backupWork-a program to backup some important files and directories # Version 1, Aug 2006 # # Get the current date in a special format # On Sept 27, 2006 at 9:05 pm, # this would look like 2006.09.27.09:05:00 TIMESTAMP='date +%Y.%m.%d.%T' # Create the new backup directory # Could look like ~/Backups/Backup.2006.09.27.09:05:00 BACKUPDIR="~/Backups/Backup.$TIMESTAMP" mkdir $BACKUPDIR # Copy files to new directory cp -r ~/Work/Project $BACKUPDIR cp -r ~/Mail $BACKUPDIR cp −/important $BACKUPDIR # Send mail to confirm that backup was done echo "Backup to $BACKUPDIR completed." | mail $LOGNAME




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net