11 Replies - 1484 Views - Last Post: 25 April 2012 - 09:29 AM Rate Topic: -----

#1 Cynosure  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 44
  • Joined: 04-February 09

Unix bash script (file replacement help);

Posted 24 April 2012 - 07:18 PM

Note, this is for a class. It is my final assignment.

This script takes the first parameter (which is to be the new extension) and each parameter after that is a file name (which will have its old extension turned into the new one. Mind you, this script works almost perfectly.

However, the test script (for me to turn the assignment in) forces this line to call the script running from the "filderFilesAreIn" directory:
./theScriptName.sh 'dat' 'file1.txt' 'file2.cpp' 'file3.dat' '../folderFilesAreIn/file4.dat'


My script does the first 3 files flawlessly. The 4th file, though, my code produces this output:

mv: `../folderFilesAreIn/file4.dat' and `../folderFilesAreIn/file4.dat' are the same file


In my code, I've used "mv -f" which is supposed to force the command to overwrite the file. It does this with the first 3 files, but the fourth (I'm guessing due to the directory path in front of it) displays this error. This error isn't causing the program to not work but the test script (the one that grades the assignment for pass/fail) isn't letting me pass as this output is "unexpected" by it.

Any help would be appreciated! I can't seem to find a way to cause "mv" to not produce any output. If I could silence the output or use another command to replace the file without getting any extra output it should count.

Thanks for your help! It's very frustrating and the teacher is being no help.

#!/bin/sh 

newExt=$1
shift

for x in $*
do

oldName=$x
olderName=$( echo "$oldName" | sed "s/ /_/g" )


dir=$(dirname $oldName)
currentdir=$(pwd)

if [ $dir = '.' ]
then
	dir="$currentdir"
fi


if [ -f "$oldName" ];
then

	preExt=$(echo $olderName | sed 's/^.*\.//')
	fileName=$(basename $olderName .${preExt})

	newName=${fileName}.${newExt}

	fixName=$( echo "$olderName" | sed "s/_/ /g" )
	fixNewName=$( echo "$newName" | sed "s/_/ /g" )
	if [ "$fixName" = "$fixNewName" ];
	then
		oldName=$oldName
	else
	mv -f "$fixName" $dir/"$fixNewName" 
	fi
else
	echo "$oldName: No such file"
fi
done


Is This A Good Question/Topic? 0
  • +

Replies To: Unix bash script (file replacement help);

#2 Cynosure  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 44
  • Joined: 04-February 09

Re: Unix bash script (file replacement help);

Posted 24 April 2012 - 07:45 PM

I'm currently attempting to write another 'if' statement that simply skips over it if the two files are the same.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Unix bash script (file replacement help);

Posted 24 April 2012 - 08:09 PM

View PostCynosure, on 25 April 2012 - 04:18 AM, said:

In my code, I've used "mv -f" which is supposed to force the command to overwrite the file.


Right, but that's only for when you do mv -f foo bar and bar already exists. It doesn't make a difference if the target and source file are the same file. I'm kind of surprised that you say it works for files in the current directory. If I execute mv -f foo foo on my machine (foo being a file in the current directory), I get the same error message you're getting.

View PostCynosure, on 25 April 2012 - 04:45 AM, said:

I'm currently attempting to write another 'if' statement that simply skips over it if the two files are the same.


That'd be the way to go, yes.
Was This Post Helpful? 1
  • +
  • -

#4 Cynosure  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 44
  • Joined: 04-February 09

Re: Unix bash script (file replacement help);

Posted 24 April 2012 - 09:26 PM

Weird! It definitely works in the same directory.

I was almost done with my new "if/else" statement, then ran across this addition:

mv -f "$fixName" $dir/"$fixNewName" 2>/dev/null


I don't exactly understand what's going on--though I believe it's just redirecting the error--but it worked!

I'm now onto the next part of the assignment which I will attempt to tackle on my own. If I can't figure it out, I'll surely be back.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Unix bash script (file replacement help);

Posted 24 April 2012 - 10:29 PM

Yes, command > file redirects the standard output of the command into the given file and command 2> file redirects the standard error of the command into the file (2 being the file descriptor of the standard error stream). And since /dev/null is a device-file which goes into nothingness, redirecting to /dev/null means that the messages are simply swallowed.
Was This Post Helpful? 1
  • +
  • -

#6 Cynosure  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 44
  • Joined: 04-February 09

Re: Unix bash script (file replacement help);

Posted 24 April 2012 - 10:57 PM

Thanks!

It turns out, I do need more help. =(

This is my problem:
When 'king cobra.dat' is passed as a parameter my script attempts reading it as two files, 'king' and 'cobra.dat'. I thought I had taken care of this already but apparently not.

This is the exact error:
The command
  ./chExt.sh 'cpp' 'aardvark.CPP' 'bongo.dat' 'cat.dog.dat' 'emu.fish.txt' '../scriptAsst/gecko.cpp' '/home/user/UnixCourse/scriptAsst/hippo.cpp' '/home/user/UnixCourse/scriptAsst/../ibex.txt' 'jay.23' 'king cobra.dat'
 did not create the expected file king cobra.cpp.


This is my new code:
#!/bin/sh 

newExt=$1
shift

while test "$1" != "";
do


oldName=$1
olderName=$( echo "$oldName" | sed "s/ /_/g" )

if [ $oldName = king cobra.dat ];
then
	rm $oldName
else

dir=$(dirname $oldName)
currentdir=$(pwd)

if [ $dir = '.' ]
then
	dir="$currentdir"
fi


if [ -f "$oldName" ];
then

	preExt=$(echo $olderName | sed 's/^.*\.//')
	fileName=$(basename $olderName .${preExt})

	newName=${fileName}.${newExt}

	fixName=$( echo "$olderName" | sed "s/_/ /g" )
	fixNewName=$( echo "$newName" | sed "s/_/ /g" )
	newDir=$dir/"$fixNewName"
	
	if [ "$fixName" = "$fixNewName" ];
	then
		oldName=$oldName
			
	else
		
		
		
		mv -f "$fixName" $dir/"$fixNewName" 2>/dev/null
		
	fi
	
	
else
	echo "$oldName: No such file"
fi
shift
fi
done

Was This Post Helpful? 0
  • +
  • -

#7 Cynosure  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 44
  • Joined: 04-February 09

Re: Unix bash script (file replacement help);

Posted 24 April 2012 - 11:09 PM

It won't let me edit the previous post.

Here's the code in a more readable manner and without my attempt to delete the 'king cobra.dat' file:
#!/bin/sh 

newExt=$1
shift

while test "$1" != "";
do


	oldName=$1
	olderName=$( echo "$oldName" | sed "s/ /_/g" )

	

		dir=$(dirname $oldName)
		currentdir=$(pwd)

		if [ $dir = '.' ]
		then
			dir="$currentdir"
		fi


		if [ -f "$oldName" ];
		then

			preExt=$(echo $olderName | sed 's/^.*\.//')
			fileName=$(basename $olderName .${preExt})

			newName=${fileName}.${newExt}

			fixName=$( echo "$olderName" | sed "s/_/ /g" )
			fixNewName=$( echo "$newName" | sed "s/_/ /g" )
			newDir=$dir/"$fixNewName"
	
			if [ "$fixName" = "$fixNewName" ];
			then
				oldName=$oldName
			
			else
				mv -f "$fixName" $dir/"$fixNewName" 2>/dev/null
		
			fi
	
	
		else
			echo "$oldName: No such file"
		fi
shift
	
done


This post has been edited by Cynosure: 24 April 2012 - 11:16 PM

Was This Post Helpful? 0
  • +
  • -

#8 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2118
  • View blog
  • Posts: 3,244
  • Joined: 21-June 11

Re: Unix bash script (file replacement help);

Posted 24 April 2012 - 11:13 PM

if [ $oldName = king cobra.dat ];
then
    rm $oldName
else



What's that for?
Was This Post Helpful? 0
  • +
  • -

#9 Cynosure  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 44
  • Joined: 04-February 09

Re: Unix bash script (file replacement help);

Posted 24 April 2012 - 11:18 PM

View Postsepp2k, on 25 April 2012 - 12:13 AM, said:

if [ $oldName = king cobra.dat ];
then
    rm $oldName
else



What's that for?



I removed it in my second post just before you commented on it.

I was trying to see if "$oldName" was ever picking up 'king cobra.dat'. It didn't work. Even when I used quotes, turned it into a variable, etc. The only way it picked it up if there was no space in the file names in both the parameter and the variable in the script. :surrender:

It's obvious that the parameter 'king cobra.dat' is getting broken up. I'm just not sure how I would go about keeping it together as a single string.

A separate script, for the assignment prior to this, handles the spaces in filenames just fine though it can only handle one file at a time. The script above is the same code just put into a loop so I'm not sure what's up. It has to be the way it's moving from one parameter to the next. I've been fooling around for a while with it but am getting nowhere.

This post has been edited by Cynosure: 24 April 2012 - 11:27 PM

Was This Post Helpful? 0
  • +
  • -

#10 wordswords  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 272
  • Joined: 17-December 11

Re: Unix bash script (file replacement help);

Posted 25 April 2012 - 03:34 AM

View PostCynosure, on 24 April 2012 - 11:18 PM, said:

View Postsepp2k, on 25 April 2012 - 12:13 AM, said:

if [ $oldName = king cobra.dat ];
then
    rm $oldName
else



What's that for?



I removed it in my second post just before you commented on it.

I was trying to see if "$oldName" was ever picking up 'king cobra.dat'. It didn't work. Even when I used quotes, turned it into a variable, etc. The only way it picked it up if there was no space in the file names in both the parameter and the variable in the script. :surrender:

It's obvious that the parameter 'king cobra.dat' is getting broken up. I'm just not sure how I would go about keeping it together as a single string.

A separate script, for the assignment prior to this, handles the spaces in filenames just fine though it can only handle one file at a time. The script above is the same code just put into a loop so I'm not sure what's up. It has to be the way it's moving from one parameter to the next. I've been fooling around for a while with it but am getting nowhere.



Try:


if [ "$oldName" = "king cobra.dat" ];
then
    rm "$oldName"
fi



This post has been edited by wordswords: 25 April 2012 - 03:36 AM

Was This Post Helpful? 0
  • +
  • -

#11 Cynosure  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 44
  • Joined: 04-February 09

Re: Unix bash script (file replacement help);

Posted 25 April 2012 - 07:40 AM

Hey words,

I had that before and it didn't work.

However, that is not an important part of my code, haha. The important part is eliminating this error:
The command
  ./chExt.sh 'cpp' 'aardvark.CPP' 'bongo.dat' 'cat.dog.dat' 'emu.fish.txt' '../scriptAsst/gecko.cpp' '/home/user/UnixCourse/scriptAsst/hippo.cpp' '/home/user/UnixCourse/scriptAsst/../ibex.txt' 'jay.23' 'king cobra.dat'
 did not create the expected file king cobra.cpp.



..with this code:
#!/bin/sh 

newExt=$1
shift

while test "$1" != "";
do


	oldName=$1
	olderName=$( echo "$oldName" | sed "s/ /_/g" )

	

		dir=$(dirname $oldName)
		currentdir=$(pwd)

		if [ $dir = '.' ]
		then
			dir="$currentdir"
		fi


		if [ -f "$oldName" ];
		then

			preExt=$(echo $olderName | sed 's/^.*\.//')
			fileName=$(basename $olderName .${preExt})

			newName=${fileName}.${newExt}

			fixName=$( echo "$olderName" | sed "s/_/ /g" )
			fixNewName=$( echo "$newName" | sed "s/_/ /g" )
			newDir=$dir/"$fixNewName"
	
			if [ "$fixName" = "$fixNewName" ];
			then
				oldName=$oldName
			
			else
				mv -f "$fixName" $dir/"$fixNewName" 2>/dev/null
		
			fi
	
	
		else
			echo "$oldName: No such file"
		fi
shift
	
done



As you can see, it successfully goes through and changes all of the previous file. The script is also called about 5 times before this error with even more parameters. It always fails here at "king cobra.dat".

My guess, as stated above, is that it's the way my program is handling the user-input parameters. It's reading "king cobra.dat" as two files "king" and "cobra.dat" which is causing an error.

I'm still unsure how to fix it.

This post has been edited by Cynosure: 25 April 2012 - 07:50 AM

Was This Post Helpful? 0
  • +
  • -

#12 Cynosure  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 44
  • Joined: 04-February 09

Re: Unix bash script (file replacement help);

Posted 25 April 2012 - 09:29 AM

w000t!

Just letting everyone know that I figured it out.

It was as simple as adding quotes around $oldName in this statement:

dir=$(dirname $oldName)


so that it was

dir=$(dirname "$oldName")


It's crazy how such a simple mistake can cause so many different, misleading errors.

I do appreciate your help, though, guys!

<3
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1