LinuxQuestions.org
Help answer threads with 0 replies.
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 04-28-2012, 04:45 AM   #16
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037

Or, if you apply the suggestions I gave in my last post, like this:

Code:
#!/bin/bash

extension="$1"
shift

for fname in "${@}" ; do

     if [[ -r "$fname" ]] ; then

          newname="${fname%.*}.$extension"

          ##This is the test to prevent mv from siting an error
          if [[ "$fname" == "$newname" ]] ; then

               :

          else

               mv -f "$fname" "${fname%.*}.$extension"

          fi

     else

          echo "$fname: No such file"

     fi

done
 
Old 11-12-2012, 10:03 PM   #17
R2B Boondocks
LQ Newbie
 
Registered: Nov 2012
Posts: 16

Rep: Reputation: Disabled
Hi I came across this post and it has helped me greatly. I am still having trouble with the final shell script though.
"
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.
"

This is the script so far:

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
This gives me the following errors:

./chExt.sh 'cpp' 'aardvark.CPP' 'bongo.dat'
mv: cannot stat `aardvark.CPP': No such file or directory
aardvark.CPP: No such file
mv: cannot stat `bongo.dat': No such file or directory
bongo.dat: No such file

Thanks for all help!
 
Old 11-12-2012, 10:32 PM   #18
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
R2B Boondocks question is answered in their LQ thread here.
 
  


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
Conditional editing of a file through Shell script Linuxahoy Linux - General 2 01-23-2012 04:45 AM
Shell script for modifying file extensions VTGuitarMan Linux - Newbie 6 03-08-2008 07:24 PM
Editing Ascii File Using Shell Script rohit2983 Programming 7 01-03-2007 06:27 AM
Editing a text file in shell Lotmr Linux - Software 5 06-24-2003 12:12 PM
ID3 Tag Editing with a Shell Script chrisk5527 Linux - General 5 03-20-2003 10:38 PM

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

All times are GMT -5. The time now is 01:15 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