UNIX/LINUX Assignment Overview: In this assignment, you will demonstrate your ab
ID: 3917011 • Letter: U
Question
UNIX/LINUX Assignment
Overview:
In this assignment, you will demonstrate your ability to write simple shell scripts. This is a cumulative assignment that will challenge you to pull together a variety of lessons from throughout the course.
Assignment:
Every now and then, I find myself with a large number of files that have inapppriate sum18_ (the set of characters in the file name after the last ‘.’) that need to be changed. For example, a complicated C++ program, developed by someone on a Windows machine where file names are not case sensitive, might have a number of files ending in “.CPP” and “.H”. Not only are these inconsistent with the conventional “.cpp” and “.h” endings, but they can pose a real issue with compiling the code. If the code contains statements like
and the file name is “Utilities.H”, the code will not compile on *nix systems, though it might on a Windows system.
In this assignment you will be working towards a script that can be used to fix this and similar problems by giving a desired extension and then a group of files that we wish renamed to use that extension instead of whatever final extension they have at the moment, e.g.
You’ll be working on this assignment in 3 stages.
STAGE 1
Create a directory ~/UnixCourse/scriptAsst.
Within that directory, create a shell script, chExt1.sh taking two parameters:
the desired file extension
the name of a single file to be renamed with that extension
For example, (assuming you have cd’d into your scriptAsst directory,
should rename the file “aardvark.CPP” to “aardvark.cpp”.
should rename the file “bongo.dat” to “bongo.backup”.
Hint: Try to get the current name of the file into a shell variable (e.g., $oldName). Then use a sed command to rewrite that value to remove the file extension, and store the result in a second shell variable (e.g., $newName). Then append the desired new extension onto that. Finally, issue the actual command to rename the file. There are probably other ways to achieve this effect as well, but all of the info you need for the approach suggested here has been covered in the Lecture Notes.
Give the command:
to check your script.
STAGE 2
To make the script a bit more robust, it would be good if it checked to see if the file that we want to rename actually exists.
Within the same directory, create a shell script, chExt2.sh taking the same two parameters, that behaves the same as the first script for files that exists, but for files that do not exist, prints a message
fileName: No such file
where fileName is the name of the file given in the second parameter.
No other messages should be issued, including error messages from commands invoked by your script.
Give the command:
to check your script.
STAGE 3
Finally, within the same directory, create a shell script, chExt.sh that takes one or more parameters, where the first is a desired extension and the remainder are names of files to be renamed. For each file in the command line, this script should rename the file, as above, if the file exists or print the error message described in the previous step if the file does not exist.
For example,
should result in crocodile.foo being renamed crocodile.dat, an error message “bogusName.foo: No such file”, and dingo.bar being renamed dingo.dat.
Give the command:
to check your scripts.
NOTES
This assignment requires you to pull together techniques from the entire run of the course, including redirection and pipes, regular expressions, quoting, environment variables, and general knowledge about Unix file and directory names.
Scripts are programs. They are written in a scripting language instead of in a compiled programming language like C++, but they are still programs.
As in any programming assignment you get in any class, you are responsible for testing your program before turning it in for evaluation (in this case, by running ~cs252/bin/scriptAsst). So I expect that you will create some text files (e.g., using emacs) containing various text and run your scripts on those files before you turn them in.
If you tested your scripts and they did not work, congratulations! Finding a test case on which your code fails is a sign that you have done a good job testing. Now go use the techniques described in Debugging Your Scripts to track down the problem.
Explanation / Answer
Shell script
This article is about scripting in UNIX-
like systems. For batch programming in
DOS, OS/2 and Windows, see Batch
file . For batch programming in Windows
PowerShell shell, see Windows
PowerShell#Scripting .
A shell script is a script written for the
shell , or command line interpreter , of
an operating system. The shell is often
considered a simple domain-specific
programming language.[1] Typical
operations performed by shell scripts
include file manipulation, program
execution, and printing text.
Many shell script interpreters double as
command line interface, such as the
various Unix shells, Windows
PowerShell or the MS-DOS
COMMAND.COM. Others, such as
AppleScript or the graphical Windows
Script Host (WScript.exe), add scripting
capability to computing environments
without requiring a command line
interface. Other examples of
programming languages primarily
intended for shell scripting include DCL
and JCL .
Capabilities
Shortcuts
In its most basic form, a shell script can
provide a convenient variation of a
system command where special
environment settings, command
options, or post-processing is applied
automatically, but in a way that allows
the new script to still act as a fully
normal Unix command.
One example would be to create a
version of ls, the command to list files,
giving it a shorter command name of l,
which would be normally saved in a
user's bin directory as /home/
username/bin/l, and a default set of
command options pre-supplied.
#!/bin/sh
LC_COLLATE=C ls -FCas "$@"
Here, the first line (Shebang) indicates
which interpreter should be used to
execute the rest of the script, and the
second line makes a listing with options
for file format indicators, columns, all
files (none omitted), and a size in
blocks. The LC_COLLATE=C sets the
default collation order to not fold upper
and lower case together, and the "$@"
causes any parameters given to l to be
passed through as parameters to ls, so
that all of the normal options and other
syntax known to ls can still be used.
The user would then be able to simply
use l for the most commonly used
short listing.
Another example of a shell script that
could be used as a shortcut would be to
print a list of all the files and directories
within a given directory.
#!/bin/sh
clear
ls -l -a
In this case, the shell script would start
with its normal starting line of #!/
bin/sh . Following this, the script
executes the command clear which
clears terminal of all text before going
to the next line. The following line
provides the main function of the
script. The ls -l -a command list
the files and directories that are in the
directory that the script is being run in.
The ls command attributes could be
changed to reflect the needs of the
user.
Batch jobs
Shell scripts allow several commands
that would be entered manually at a
command line interface to be executed
automatically, and without having to
wait for a user to trigger each stage of
the sequence. For example, in a
directory with three C source code
files, rather than manually running the
four commands required to build the
final program from them, one could
instead create a C shell script, here
named build and kept in the directory
with them, which would compile them
automatically:
#!/bin/csh
echo compiling...
cc -c foo.c
cc -c bar.c
cc -c qux.c
cc -o myprog foo.o bar.o qux.o
echo done.
The script would allow a user to save
the file being edited, pause the editor,
and then just run ./build to create the
updated program, test it, and then
return to the editor. Since the 1980s or
so, however, scripts of this type have
been replaced with utilities like make
which are specialized for building
programs.
Generalization
Simple batch jobs are not unusual for
isolated tasks, but using shell loops,
tests, and variables provides much
more flexibility to users. A Bash (Unix
shell) script to convert JPEG images to
PNG images, where the image names
are provided on the command line -
possibly via wildcards - instead of each
being listed within the script, can be
created with this file, typically saved in
a file like /home/username/bin/
jpg2png
#!/bin/bash
for jpg; 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".' >&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
exit 0
The jpg2png command can then be run
on an entire directory full of JPEG
images with just jpg2png *.jpg
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.