OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Simple Applications

Even without shift, it’s possible to write useful applications using positional parame- ters. By way of example, here is a simple file information program:


#!/bin/bash


# file_info: simple file information program PROGNAME=$(basename $0)

if [[ -e $1 ]]; then

echo -e "\nFile Type:" file $1

echo -e "\nFile Status:" stat $1

else

echo "$PROGNAME: usage: $PROGNAME file" >&2 exit 1

fi

#!/bin/bash


# file_info: simple file information program PROGNAME=$(basename $0)

if [[ -e $1 ]]; then

echo -e "\nFile Type:" file $1

echo -e "\nFile Status:" stat $1

else

echo "$PROGNAME: usage: $PROGNAME file" >&2 exit 1

fi


This program displays the file type (determined by the file command) and the file sta- tus (from the stat command) of a specified file. One interesting feature of this program is the PROGNAME variable. It is given the value that results from the basename $0 command. The basename command removes the leading portion of a pathname, leav- ing only the base name of a file. In our example, basename removes the leading portion of the pathname contained in the $0 parameter, the full pathname of our example pro- gram. This value is useful when constructing messages such as the usage message at the end of the program. By coding it this way, the script can be renamed and the message au- tomatically adjusts to contain the name of the program.


Top OS Cloud Computing at OnWorks: