Shell Scripts
A shell script is simply a file that
contains a set of commands to be run by the shell when invoked. By
storing commands as a shell script, you make it easy to execute them
again and again. As an example, consider a file named
deleter
, which contains the following lines:
echo -n Deleting the temporary files... rm -f *.tmp echo Done.
The echo commands simply print text
on the console. The -n option of the
first echo command causes omission
of the trailing newline character normally written by the echo command, so both echo commands write their text on a single
line. The rm command removes all
files having names ending in .tmp
from the
current working directory.
You can execute this script by issuing the sh command, as follows:
sh deleter
Tip
If you invoke the sh command without an argument specifying a script file, a new interactive shell is launched. To exit the new shell and return to your previous session, issue the exit command.
If the deleter
file were in a directory other
than the current working directory, you’d have to type an
absolute path, for example:
sh /home/bill/deleter
You can make it a bit easier to execute the script by changing its access mode to include execute access. To do so, issue the following command:
chmod 555 deleter
This gives you, members of your group, and everyone else the ability to execute the file. To do so, simply type the absolute path of the file, for example:
/home/bill/deleter
If the file is in the current directory, you can issue ...
Get Learning Red Hat Linux, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.