3 Replies - 1363 Views - Last Post: 12 November 2012 - 08:17 PM Rate Topic: -----

#1 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 196
  • Joined: 19-September 12

Proper development of Shell Scripts

Posted 12 November 2012 - 07:15 PM

I have to develop some shell scripts for a class assignment. I am not trying to get the answers. Firstly I will give you guys an idea about what they must include.
"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
The first line of your shell script must be
#!/bin/sh

For example, (assuming you have cd'd 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.
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.

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.
"

Now those are the exact details of the assignment. Again I don't want this spoon fed to me. I would like a clear explanation as to how I would begin developing the shell scripts. The lecture notes are pretty difficult to understand. Any help would be appreciated thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Proper development of Shell Scripts

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,166
  • Joined: 05-May 12

Re: Proper development of Shell Scripts

Posted 12 November 2012 - 07:44 PM

And how is this a C/C++ programming question? It looks like you are looking for shell script programming help.
Was This Post Helpful? 0
  • +
  • -

#3 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Proper development of Shell Scripts

Posted 12 November 2012 - 08:07 PM

Ok, moving on over to "Other"
Was This Post Helpful? 0
  • +
  • -

#4 R2B Boondocks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 196
  • Joined: 19-September 12

Re: Proper development of Shell Scripts

Posted 12 November 2012 - 08:17 PM

Touche. My fail but here is the code for anyone who happens upon this post.

#!/bin/sh
extension="$1"
shift
for fname in "${@}" ; do
if test -r "$fname" ; then
echo Changing: "$fname"
mv "$fname" "${fname%.*}.$extension"
else
filename="$fname"
echo $filename: No such file
fi
done


*** Edit ***
Please use code tags when posting code.
Posted Image

This post has been edited by GunnerInc: 12 November 2012 - 08:34 PM
Reason for edit:: Added elusive code tags

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1