LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2012, 10:14 PM   #1
R2B Boondocks
LQ Newbie
 
Registered: Nov 2012
Posts: 16

Rep: Reputation: Disabled
Shell scripts help


Hi all,

I am coming up with shell scripts for a class assignment. There are a total of three and the first two function properly. I cannot get the third one to work.

I will post the directions then the shell scripts chExt1.sh, chExt2.sh, chExt.sh. 1 2 and 3 respectively.

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.

Here are my scripts

chExt1.sh

Code:
#!/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
chExt2.sh
Code:
#!/bin/sh
extension="$1"
shift
for fname in "${@}" ; do
if test -r "$fname" ; then
newname="${fname%.*}.$extension"
if test "$fname" = "$newname" ; then
:
else
mv -f "$fname" "${fname%.*}.$extension"
fi
else
echo "$fname": No such file
fi

done
chExt.sh
Code:
#!/bin/sh
extension="$1"
shift
for fname in "${@}" ; do
    mv "$fname" "${fname%.*}.$extension"
if test -r "$fname" ; then
newname="${fname%.*}.$extension"
if test "$fname" = "$newname" ; then  ##This is the test to prevent mv from sit\
ing an error
:
else
mv -f "$fname" "${fname%.*}.$extension"
fi
else
echo "$fname": No such file
fi

done
Thanks for any and all help!
 
Old 11-12-2012, 10:28 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
The bug is that the script moves (renames) the file before testing whether it exists. Here's with the bug fixed (and indented and error messages sent to stderr):
Code:
#!/bin/sh
extension="$1"
shift
for fname in "${@}" ; do
    #mv "$fname" "${fname%.*}.$extension"  <== the bug
    if test -r "$fname" ; then
        newname="${fname%.*}.$extension"
        if test "$fname" = "$newname" ; then 
            echo "$fname": already has extension $extension >&2
        else    
            mv -f "$fname" "${fname%.*}.$extension"
        fi      
    else    
        echo "$fname": No such file >&2
    fi      
done
BTW [[ <test expression> ]] is a better choice than the test command for reasons explained here.
 
Old 11-12-2012, 10:39 PM   #3
R2B Boondocks
LQ Newbie
 
Registered: Nov 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
The bug is that the script moves (renames) the file before testing whether it exists. Here's with the bug fixed (and indented and error messages sent to stderr):
Code:
#!/bin/sh
extension="$1"
shift
for fname in "${@}" ; do
    #mv "$fname" "${fname%.*}.$extension"  <== the bug
    if test -r "$fname" ; then
        newname="${fname%.*}.$extension"
        if test "$fname" = "$newname" ; then 
            echo "$fname": already has extension $extension >&2
        else    
            mv -f "$fname" "${fname%.*}.$extension"
        fi      
    else    
        echo "$fname": No such file >&2
    fi      
done
BTW [[ <test expression> ]] is a better choice than the test command for reasons explained here.
Ah! Simple mistake. Thank you so much for your help as soon as I got rid of that line and the ">&2" it worked perfectly!
 
Old 11-13-2012, 12:39 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Glad it worked for you

Not clear why the >&2 had to be removed for it to work properly ... ?

Threads can be marked SOLVED via the Thread Tools menu.
 
Old 11-13-2012, 02:16 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Maybe for a little extra credit you should check if you in fact get 2 or more arguments to scripts as you assume from the start you will always get 2 or even 1.

I would agree with catkin to that removing / including the redirection to stderr (the correct place for errors by the way) should not affect the script.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make these shell scripts with ash as my shell? riahc3 Programming 13 08-15-2012 03:57 AM
best way to automate shell commands - shell script or python scripts or something els parangsaraf Linux - Newbie 11 08-08-2012 06:17 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
need help on shell scripts!!! krcool32 Programming 5 04-28-2003 03:08 PM
shell scripts nautilus_1987 Linux - General 3 08-30-2002 03:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration