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

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

#include "Utilities.h"

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.

chExt.sh cpp *.CPP *.Cpp chExt.sh h *.H

Youll 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 cdd into your scriptAsst directory,

echo ants > aardvark.CPP ./chExt1.sh cpp aardvark.CPP 

should rename the file aardvark.CPP to aardvark.cpp.

date > bongo.dat ./chExt1.sh backup bongo.dat 

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:

~cs252/bin/scriptAsst 

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:

~cs252/bin/scriptAsst 

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,

ls > crocodile.foo echo bark > dingo.bar ./chExt.sh dat crocodile.foo bogusName.foo dingo.bar 

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:

~cs252/bin/scriptAsst 

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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!