-->

Previous | Table of Contents | Next

Page 99

These two command lines start the pico editor and the pine mail program in two X11 rxvt terminal windows on the second desktop in an 800-by-600-pixel display. These certainly aren't command lines you'd want to type each time you want to run these programs. Although you can manually start the terminal windows after moving to the other desktop, it may take some time to correctly size the windows and then start the programs. Turn these command lines into an executable file by saving them into a file with your text editor, then use the chmod command to make the file executable:


# chmod +x d2

Now, when you want to run these programs, all you have to type is the following, which is certainly a heck of a lot easier!


# d2

You can make this new command even more flexible by using the shell variables $1 and $2, which represent the first and second command-line arguments to a shell command. Edit the file you've created and change the program names to these variables:


rxvt -geometry 80x11+803+375 -bg white -fg black -e $2 &

rxvt -geometry 80x24+806+2 -bg white -fg black -e $1 &

Note that the order of the variables isn't important. Now when you run your command, you can supply the program names on the command line. For example,


# d2 pine pico

This has the same result, but from now on you'll be able to run nearly any program you want in the terminal windows.

This discussion of using the shell concludes with a simple shell script you can use to safely delete files. There's nothing special about this script in Listing 6.1 , rmv, but it demonstrates some of the power of shell scripts.

Listing 6.1. The rmv bash shell script.


#!/bin/bash

# rmv - a safe delete program

# uses a trash directory under your home directory

mkdir $HOME/.trash 2>/dev/null

cmdlnopts=false

delete=false

empty=false

list=false


                                                               continues

Page 100

Listing 6.1.continued


while getopts "dehl" cmdlnopts; do

  case "$cmdlnopts" in

    d ) /bin/echo "deleting: \c" $2 $3 $4 $5 ; delete=true ;;

    e ) /bin/echo "emptying the trash..." ; empty=true ;;

    h ) /bin/echo "safe file delete v1.0"

        /bin/echo "rmv -d[elete] -e[mpty] -h[elp] -l[ist] file1-4" ;;

    l ) /bin/echo "your .trash directory contains:" ; list=true ;;

  esac

done



if [ $delete = true ]

then

  mv $2 $3 $4 $5 $HOME/.trash

  /bin/echo "rmv finished."

fi

if [ $empty = true ]

then

  /bin/echo "empty the trash? \c"

  read answer

  case "$answer" in

    y) rm -fr $HOME/.trash/* ;;

    n) /bin/echo "trashcan delete aborted." ;;

  esac

fi

if [ $list = true ]

then

  ls -l $HOME/.trash

fi



The first line of the script invokes the bash shell to run the script. After you've typed in this script using your favorite text editor, you should make the script executable by using the chmod command:


# chmod +x rmv

This script moves unwanted files to a directory called .trash in your home directory. When you're sure you want to delete the files, you can then verify the files and empty the trash. Because this script is supposed to act like a regular command, a short built-in help command has been included.

JUST A MINUTE
If you want to try the rmv script, make sure you type it in correctly, or change the line containing "rm -fr $HOME/.trash/* ;;" to "rm -i $HOME/.trash/* ;;" which is much safer than an unconditional delete. If you don't type this line correctly, you could delete your entire home directory!

Page 101

The script works by first creating a .trash directory in your home directory (found with the $HOME environment variable). If the directory exists, any error messages generated by the mkdir command are discarded by sending the standard error output to the /dev/null device (a handy place to send all complaints!). Four internal script variables are then defined: cmdlnopts, delete, empty, and list.

The script uses the bash shell getopts command to look at your command line for any options. If a matching letter is found by the case statement, the script commands up until the two semicolons are executed. For example, if you want a reminder of how to use the script, you can use the -h, or help option:


# rmv -h

safe file delete v1.0

rmv -d[elete] -e[mpty] -h[elp] -l[ist] file1-4

This prints a short help message because the script found the letter h on the command line, then printed the message using the echo command. To delete files, you must use the -d, or delete command-line option:


# rmv -d graf.gif graf.pnm graf.tif

deleting:  graf.gif graf.pnm graf.tifrmv finished.

This deletes the four files by moving them to the .trash directory in your home directory. The d option was detected on the command line and the script then printed a message, echoed the file names back to your display, then set the delete variable to true. Because the delete variable was changed to true, the mv command found in the if ... then statement is executed.

You can verify that the files have been moved by using the -l, or list option, to see the contents of your trash:


# rmv -l

your .trash directory contains:

total 278

-rw-rw-r--   1 bball    bball        3299 Dec 28 22:38 graf.gif

-rw-rw-r--   1 bball    bball      272091 Dec 28 22:38 graf.pnm

-rw-rw-r--   1 bball    bball        6890 Dec 28 22:38 graf.tif

As before, because the letter l was detected on the command line, the script set the list variable to true, then executed the ls command to list the contents of your .trash directory. If you're sure you want to delete these files, you can then use the -e, or empty command-line option:


# rmv -e

emptying the trash...

empty the trash? n

trashcan delete aborted.

# rmv -e

emptying the trash...

empty the trash? y

# rmv -l

your .trash directory contains:

total 0

Previous | Table of Contents | Next