![]() | Shell Scripts - An Illustrated View At the risk of sounding redundant, let's recap: shell scripts are simply files containing a list of commands to be executed in sequence. Now let's go a bit further and look at a shell script, line-by-line. Any Korn shell script should contain this line at the very beginning: #!/usr/bin/ksh As you probably already know, the # sign marks anything that follows it on the line as a comment - anything coming after it won't be interpreted or processed as part of the script. But, when the # character is followed by a ! (commonly called "bang"), the meaning changes. The line above specifies that the Korn shell will be (or should be) executing the script. If nothing is specified, the system will attempt to execute the script using whatever its default shell type is, not necessarily a Korn shell. Since the Korn shell supports some commands that other shells do not, this can sometimes cause a problem. To be valid, this line must be on the very first line of the script. Shell scripts are often used to automate day-to-day tasks. For example, a system administrator might use the following script, named diskuse here, to keep track of disk space usage: #!/usr/bin/ksh # diskuse # Shows disk usage in blocks for /home cd /var/log cp disk.log disk.log.0 cd /home du -sk * > /var/log/disk.log cat /var/log/disk.log Shown again - but this time with annotation - the script's processing steps are clear: #!/usr/bin/ksh # SCRIPT NAME: diskuse # SCRIPT PURPOSE: Shows disk usage in blocks for /home # change to the directory where disk.log resides cd /var/log # make a copy of disk.log cp disk.log disk.log.0 # change to the target directory cd /home # run the du -sk * command on all files # in /home and redirect the output # to /var/log/disk.log du -sk * > /var/log/disk.log # display the output of the du -sk * # command to standard output cat /var/log/disk.log It's not a good idea to hard-code pathnames into your scripts like we did in the previous example. We specified /var/log as the target directory several times, but what if the location of the files changed? In a short script like this one, the impact is not great. However, some scripts can be hundreds of lines long, creating a maintenance headache if files are moved. A way around this is to create a variable to take the place of the full pathname, such as: LOGDIR=/var/log The fourth line of the script would change from: cp disk.log disk.log.0 to: cp ${LOGDIR}/disk.log ${LOGDIR}/disk.log.0 Then, if the locations of disk.log changes in the future, you would only have to change the variable definition to update the script. Also note that since you are defining the pathname with the LOGDIR variable, the cd /var/log line in the script is unnecessary. Likewise, the du -sk * > /var/log/disk.log and cat /var/log/disk.log lines can substitute ${LOGDIR} for /var/log. |
| * You are viewing a page from LiveFire Labs' sample online UNIX training course. Click here if you do not see a blue and green frame around this page, or view the detailed description for this course. LiveFire Labs - Online UNIX Training |