Files
oldlinux-files/docs/Install-Guide/install-guide-2.2.2/node150.html
2024-02-19 00:23:35 -05:00

99 lines
5.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 95.1 (Fri Jan 20 1995) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>3.13.1 Shell scripts</TITLE>
</HEAD>
<BODY>
<meta name="description" value="3.13.1 Shell scripts">
<meta name="keywords" value="gs">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<BR> <HR><A NAME=tex2html3653 HREF="node151.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3651 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3645 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3655 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3656 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3654 HREF="node151.html">3.13.2 Shell variables and </A>
<B>Up:</B> <A NAME=tex2html3652 HREF="node149.html">3.13 Customizing your Environment</A>
<B> Previous:</B> <A NAME=tex2html3646 HREF="node149.html">3.13 Customizing your Environment</A>
<BR> <HR> <P>
<H2><A NAME=SECTION005131000000000000000>3.13.1 Shell scripts</A></H2>
<A NAME=secshellscript>&#160;</A>
<A NAME=3973>&#160;</A>
<A NAME=3974>&#160;</A>
Let's say that you use a series of commands often, and
would like to shorten the amount of required typing by grouping all
of them together into a single ``command''. For example, the commands
<P><TT> /home/larry# <em>cat chapter1 chapter2 chapter3 <b>&gt;</b> book</em> <BR>
/home/larry# <em>wc -l book</em> <BR>
/home/larry# <em>lp book</em>
<P></TT>
would concatenate the files <tt>chapter1</tt>, <tt>chapter2</tt>, and
<tt>chapter3</tt> and place the result in the file <tt>book</tt>. Then, a
count of the number of lines in <tt>book</tt> would be displayed, and
finally <tt>book</tt> would be printed with the <tt>lp</tt> command.
<P>
Instead of typing all of these commands, you could group them into a
<b>shell script</b>. We described shell scripts briefly in
Section <A HREF="node150.html#secshellscript">3.13.1</A>. The shell script used to run all of these
commands would look like
<P><TT> <PRE>#!/bin/sh
# A shell script to create and print the book
cat chapter1 chapter2 chapter3 &gt; book
wc -l book
lp book
</PRE> <P></TT>
If this script was saved in the file <tt>makebook</tt>, you could simply
use the command
<P><TT> /home/larry# <em>makebook</em>
<P></TT>
to run all of the commands in the script. Shell scripts are just
plain text files; you can create them with an editor such as <tt>emacs</tt>
or <tt>vi</tt>
<A NAME=tex2html683 HREF="footnode.html#4145"><IMG ALIGN=BOTTOM ALT="gif" SRC="foot_motif.gif"></A>.
<P>
Let's look at this shell script. The first line, ``<tt>#!/bin/sh</tt>'',
identifies the file as a shell script, and tells the shell how to
execute the script. It instructs the shell to pass the script to
<tt>/bin/sh</tt> for execution, where <tt>/bin/sh</tt> is the shell program
itself. Why is this important? On most UNIX systems, <tt>/bin/sh</tt> is a
Bourne-type shell, such as Bash. By forcing the shell script to run
using <tt>/bin/sh</tt>, we are ensuring that the script will run under a
Bourne-syntax shell (instead of, say, a C shell). This will cause your
script to run using the Bourne syntax even if you use Tcsh (or another
C shell) as your login shell.
<P>
<A NAME=4004>&#160;</A>
The second line is a <em>comment</em>. Comments begin with the character
``<tt>#</tt>'' and continue to the end of the line. Comments are
ignored by the shell---they are commonly used to identify the shell script
to the programmer.
<P>
The rest of the lines in the script are just commands, as you
would type them to the shell directly. In effect, the shell reads
each line of the script and runs that line as if you had typed it
at the shell prompt.
<P>
<A NAME=4007>&#160;</A>
<A NAME=4008>&#160;</A>
Permissions are important for shell scripts. If you
create a shell script, you must make sure that you have execute
permission on the script in order to run it<A NAME=tex2html687 HREF="footnode.html#4009"><IMG ALIGN=BOTTOM ALT="gif" SRC="foot_motif.gif"></A>.
The command
<P><TT> /home/larry# <em>chmod u+x makebook</em>
<P></TT>
can be used to give yourself execute permission on the shell script
<tt>makebook</tt>.
<P>
<BR> <HR><A NAME=tex2html3653 HREF="node151.html"><IMG ALIGN=BOTTOM ALT="next" SRC="next_motif.gif"></A> <A NAME=tex2html3651 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="up" SRC="up_motif.gif"></A> <A NAME=tex2html3645 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif"></A> <A NAME=tex2html3655 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif"></A> <A NAME=tex2html3656 HREF="node250.html"><IMG ALIGN=BOTTOM ALT="index" SRC="index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3654 HREF="node151.html">3.13.2 Shell variables and </A>
<B>Up:</B> <A NAME=tex2html3652 HREF="node149.html">3.13 Customizing your Environment</A>
<B> Previous:</B> <A NAME=tex2html3646 HREF="node149.html">3.13 Customizing your Environment</A>
<BR> <HR> <P>
<BR> <HR>
<P><ADDRESS>
<I>Matt Welsh <BR>
mdw@sunsite.unc.edu</I>
</ADDRESS>
</BODY>