In programming, a "parameter" is an item of data that is passed from one procedure to another. They are frequently used to pass data from the bash shell to a script. For example, suppose we want to write a script that will erase all files with the filename extension of "tmp", but which will document that action by writing a list of the filenames to be erased and the date that they were erased to a log file. We could accomplish such a task with the following script:
#!/bin/bash echo -e "Erasing all files from `pwd` with the extension: tmp\n" echo -e "Logging erasure of the following files from `pwd`:\n"> erasure.log ls *.tmp >> erasure.log rm *.tmp echo -e "Completed on: `date`.\n" >> erasure.log
Notice the use of the backwards apostrophes in this example to perform "command substitution" quoting, in which the output produced by each such quoted command is substituted inside the quotes by the shell before the command is executed. For example, assuming a home directory named "/home/jsmith", the statement which reads
echo -e "Erasing all files from `pwd` with the extension: tmp\n"
would be replaced with
echo -e "Erasing all files from /home/jsmith with the extension: tmp\n"
when it was executed. Another syntax that can be used for command substitution is "$(command)". For example the command substitution indicated in the prior example, could instead be written as:
echo -e "Erasing all files from $(pwd) with the extension: tmp\n"
Regardless of how the substitution was written, the script would be executed (after making it executable: chmod u+x logerase ) by simply entering its name (for example "logerase") including the path to it ("./") on the command line, as in:
./logerase
In the script above, the extension "tmp" is a "string constant", meaning literal text which does not change each time the script runs. This script can be used only to erase files with the "tmp" extension. However the script could be enhanced to be more versatile by making it interactive and having it ask the user for the extension of the file(s) we want to erase. But that approach would make the script slower and more complicated to use. The user would have to read the prompt produced by the script and then enter a response. We could streamline that process by using "positional parameters", which are simply separate items of data listed on the command line with script name, as in:
./logerase tmp
In the command above, the program name "logerase" is positional parameter 0 (zero) and the first "argument" (item of data to be processed) is positional parameter 1. The bash shell identifies these positional parameters as $0 and $1 respectively. This allows us to modify the original script to refer to the first argument (positional parameter 1) as $1, resulting in a revised script of:
#!/bin/bash echo -e "Erasing all files from `pwd` with the extension: $1\n" echo -e "Logging erasure of the following files from `pwd`:\n" > erasure.log ls *.$1 >> erasure.log rm *.$1 echo -e "Completed on: `date`.\n" >> erasure.log
Notice the substitution of "$1" wherever the string given as the first argument is needed in the script. Using this method, our script could now be used to log the erasure of files with any extension, by simply providing that extension as the first argument following the script name on the command line. For example, the following command could now be used to erase all files with "bak" extensions.:
./logerase bak
Of course, these scripts do not contain any provision for handling usage errors, but they could be enhanced further to do so by adding conditional "if" statements as described in Chapter 27 of your textbook.