Benefits of Working with Shell Scripts
Essay by felonee63 • September 24, 2012 • Study Guide • 555 Words (3 Pages) • 2,310 Views
A shell script is a script that is written for the command line interpreter or shell of an operating system such as UNIX. Usually these scripts are for the purpose of printing text, file maintenance, or other utilitarian tasks. A shell script allows the user to execute multiple commands from a minimum number of key strokes, from a single key stroke or event allows the script to run automatically.
A batch job such as converting a folder of jpg images to their png counterparts could be created, saved, and then run as needed instead of having to be scripted each time. This allows a rather tedious task to be automated. An example of such a script is:
#!/bin/bash
for jpg in "$@" ; do # use $jpg in place of each filename given, in turn
png="${jpg%.jpg}.png" # find the PNG version of the filename by replacing .jpg with .png
echo converting "$jpg" ... # output status info to the user running the script
if convert "$jpg" jpg.to.png ; then # use the convert program (common in Linux) to create the PNG in a temp file
mv jpg.to.png "$png" # if it worked, rename the temporary PNG image to the correct name
else # ...otherwise complain and exit from the script
echo 'error: failed output saved in "jpg.to.png".' 1>&2
exit 1
fi # the end of the "if" test construct
done # the end of the "for" loop
echo all conversions successful # tell the user the good news
There can be drawbacks to shell scripts as well. The programmer must ensure that all of the information is entered correctly to prevent changing files in an unintended source. A classic example of this is rm -rf * / instead of the intended rm -rf */ which instead of deleting everything in the subdirectories, would delete everything in the root directory.
References
Shell script. (n.d.). Retrieved on July 7, 2012 from http://en.wikipedia.org/wiki/Shell_script
...
...