3 Replies - 787 Views - Last Post: 18 May 2013 - 11:22 AM Rate Topic: -----

#1 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 111
  • Joined: 26-June 12

Linux script skipping function (I think)

Posted 17 May 2013 - 07:48 PM

First off, forgive me if this is in the wrong forum, I didn't see much for Linux. Feel free to point me in the right direction.

I have an assignment, as follows:

Quote

In this assignment, you will demonstrate your ability to write simple shell scripts. This is also the closest thing to a final exam that this course has, so it will challenge you to pull together a variety of lessons from throughout the course.

Every now and then, I find myself with a large number of files that have inapppriate extensions (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 moement, e.g.

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



You'll be working on this assignment in 3 stages.

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
The first line of your shell script must be
#!/bin/sh

or
#!/bin/csh

This requirement applies to all the scripts you will develop in this assignment.

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.

Give the command:

~cs252/bin/scriptAsst.pl

to complete the assignment.


Part one I have finished, part two is giving me problems.
#!/bin/sh
extension="$1"
shift
  for fname in "${@}"; do
    if test -r "$fname" ; then
       newname="${fname%.*}.$extension"
         if test -r  "$fname"="$newname";then
            :
         else 
            mv -f "$fname" "${fname%.*}.$extension"
    fi
    else
       echo "$fname": No such file
    fi
done



Is what I have so far. I keep getting the message /.chExt2.sh 'dat' 'ocelot.dat' generated unexpected messages. For some reason, it's generating the 'no such file' message. Any tips are much appreciated. Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Linux script skipping function (I think)

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




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

Re: Linux script skipping function (I think)

Posted 17 May 2013 - 08:13 PM

Not too sure, but following the sample directions:
ls > crocodile.foo
 echo bark > dingo.bar
 ./chExt.sh dat crocodile.foo bogusName.foo dingo.bar

Using your supplied code, all works:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 111
  • Joined: 26-June 12

Re: Linux script skipping function (I think)

Posted 17 May 2013 - 08:38 PM

I believe the problem is coming in because it's .dat with a file that is already .dat. so the command is ./chExt.sh dat ocelote.dat. It's generating an error message such that it much be skipping the first test -r, or now qualifying. ocelote.dat exists, so it should just remain the same instead of skipping that step and moving to the no such file. At least that's what I think is happening, what I can't yet figure out is why.

This post has been edited by CoryMore: 18 May 2013 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

#4 CoryMore  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 111
  • Joined: 26-June 12

Re: Linux script skipping function (I think)

Posted 18 May 2013 - 11:22 AM

Just for future generations...I figured it out <g.

#!/bin/sh
extension="$1"
shift
for fname in "${@}"; do
if test -r "$fname" ; then
newname="${fname%.*}.$extension"
if test -r "$fname"="$newname";then
echo "$fname": already has extension $extension
else mv -f "$fname" "${fname%.*}.$extension" 2> /dev/null
fi
else
echo "$fname": No such file
fi
done


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1