Echo log files cleaned up note that there are other

This preview shows page 11 - 13 out of 907 pages.

echo "Log files cleaned up."#Note that there are other log files in /var/log not affected#+ by this script.exit 0#A zero return value from the script upon exit indicates success#+ to the shell.Since you may not wish to wipe out the entire system log, this version of the script keeps the last section ofthe message log intact. You will constantly discover ways of fine-tuning previously written scripts forincreased effectiveness.* * *Thesha-bang( #!) [6] at the head of a script tells your system that this file is a set of commands to be fed tothe command interpreter indicated. The #! is actually a two-byte [7]magic number, a special marker thatdesignates a file type, or in this case an executable shell script (typeman magicfor more details on thisfascinating topic). Immediately following thesha-bangis apath name. This is the path to the program thatinterprets the commands in the script, whether it be a shell, a programming language, or a utility. Thiscommand interpreter then executes the commands in the script, starting at the top (the line following thesha-bangline), and ignoring comments. [8]#!/bin/sh#!/bin/bash#!/usr/bin/perl#!/usr/bin/tcl#!/bin/sed -f#!/bin/awk -fEach of the above script header lines calls a different command interpreter, be it/bin/sh, the default shell(bashin a Linux system) or otherwise. [9] Using#!/bin/sh, the default Bourne shell in most commercialvariants of UNIX, makes the script portable to non-Linux machines, though you sacrifice Bash-specificfeatures. The script will, however, conform to the POSIX [10]shstandard.Note that the path given at the "sha-bang" must be correct, otherwise an error message -- usually "Commandnot found." -- will be the only result of running the script. [11]#! can be omitted if the script consists only of a set of generic system commands, using no internal shelldirectives. The second example, above, requires the initial #!, since the variable assignment line,lines=50,uses a shell-specific construct. [12] Note again that#!/bin/shinvokes the default shell interpreter, whichdefaults to/bin/bashon a Linux machine.This tutorial encourages a modular approach to constructing a script. Make note of and collect"boilerplate" code snippets that might be useful in future scripts. Eventually you will build quite anAdvanced Bash-Scripting GuideChapter 2. Starting Off With a Sha-Bang5
extensive library of nifty routines. As an example, the following script prolog tests whether the script hasbeen invoked with the correct number of parameters.E_WRONG_ARGS=85script_parameters="-a -h -m -z"#-a = all, -h = help, etc.if [ $# -ne $Number_of_expected_args ]thenecho "Usage: `basename $0` $script_parameters"# `basename $0` is the script's filename.exit $E_WRONG_ARGSfiMany times, you will write a script that carries out one particular task. The first script in this chapter isan example. Later, it might occur to you to generalize the script to do other, similar tasks. Replacing theliteral ("hard-wired") constants by variables is a step in that direction, as is replacing repetitive codeblocks by functions.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 907 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
N/A
Tags
Shell, scripting language, Shell script, C shell

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture