📄 0099-0101.html
字号:
<HTML>
<HEAD>
<TITLE>Sams Teach Yourself Linux in 24 Hours:Using the Shell:EarthWeb Inc.-</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311623 //-->
<!-- TITLE=Sams Teach Yourself Linux in 24 Hours//-->
<!-- AUTHOR=Bill Ball//-->
<!-- PUBLISHER=Macmillan Computer Publishing//-->
<!-- IMPRINT=Sams//-->
<!-- CHAPTER=06 //-->
<!-- PAGES=0083-0102 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0096-0098.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0102-0102.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-99"><P>Page 99</P></A>
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
# chmod +x d2
</PRE>
<!-- END CODE SNIP //-->
<P>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!
</P>
<!-- CODE SNIP //-->
<PRE>
# d2
</PRE>
<!-- END CODE SNIP //-->
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
rxvt -geometry 80x11+803+375 -bg white -fg black -e $2 &
rxvt -geometry 80x24+806+2 -bg white -fg black -e $1 &
</PRE>
<!-- END CODE SNIP //-->
<P>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,
</P>
<!-- CODE SNIP //-->
<PRE>
# d2 pine pico
</PRE>
<!-- END CODE SNIP //-->
<P>This has the same result, but from now on you'll be able to run nearly any program
you want in the terminal windows.
</P>
<P>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.
</P>
<P>Listing 6.1. The rmv bash shell script.
</P>
<!-- CODE //-->
<PRE>
#!/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
</PRE>
<!-- CODE SNIP //-->
<PRE>
continues
</PRE>
<!-- END CODE SNIP //-->
<A NAME="PAGENUM-100"><P>Page 100</P></A>
<P>Listing 6.1.continued</P>
<!-- CODE //-->
<PRE>
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
</PRE>
<!-- END CODE //-->
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
# chmod +x rmv
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<TABLE BGCOLOR=#FFFF99><TR><TD>JUST A MINUTE</TD></TR><TR><TD>
<BLOCKQUOTE>
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!
</BLOCKQUOTE></TD></TR></TABLE>
<A NAME="PAGENUM-101"><P>Page 101</P></A>
<P>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.
</P>
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
# rmv -h
safe file delete v1.0
rmv -d[elete] -e[mpty] -h[elp] -l[ist] file1-4
</PRE>
<!-- END CODE SNIP //-->
<P>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:
</P>
<!-- CODE SNIP //-->
<PRE>
# rmv -d graf.gif graf.pnm graf.tif
deleting: graf.gif graf.pnm graf.tifrmv finished.
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<P>You can verify that the files have been moved by using the -l, or list option, to see
the contents of your trash:
</P>
<!-- CODE //-->
<PRE>
# 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
</PRE>
<!-- END CODE //-->
<P>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:
</P>
<!-- CODE //-->
<PRE>
# 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
</PRE>
<!-- END CODE //-->
<P><CENTER>
<a href="0096-0098.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0102-0102.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -