LinuxQuestions.org
Review your favorite Linux distribution.
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 01-20-2008, 01:41 PM   #1
geek_man
Member
 
Registered: Nov 2007
Distribution: Fedora 7, Fedora 9, Solaris 10, Mac OS X, RHEL5
Posts: 71

Rep: Reputation: 15
Question Problem with AWK in a BASH script


Hi!

I am writing a bash script to rename my songs. But as some song's names have spaces I had to use awk, but now I have a problem.
Here is my script:

Code:
#!/bin/bash 

MINPARAMS=4
AWKSCRIPT=' { gsub(" ", "\\ "); print | "mv"} '

if [ $# != "$MINPARAMS" ]
then
	echo "This script needs at least 4 parameters."
	echo "Use: "
	echo "rename_song artist_name disc_name song_name song_to_rename"
	exit
else
	name=$1_$2_$3
	echo $4 $name | awk "$AWKSCRIPT" 
fi
It should work fine, but there is a problem: I am using gsub to scape the spaces in the name, but it leaves a \ at the end of the name and I have to delete this \ for mv to work fine. I know that I can do it in this form:
sub(/\/+$/, ""), but it doesn't work. I am not an expert in AWK, if there is another way to do it, please tell me.

Thanks.
 
Old 01-20-2008, 01:47 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
You shouldn't have to replace the " "s with "\ "s - just quote the string when you run mv, e.g.

$ mv "track 3.mp3" "Orbital - Belfast.mp3"

Dave
 
Old 01-20-2008, 04:19 PM   #3
geek_man
Member
 
Registered: Nov 2007
Distribution: Fedora 7, Fedora 9, Solaris 10, Mac OS X, RHEL5
Posts: 71

Original Poster
Rep: Reputation: 15
Bash Script

Quote:
Originally Posted by ilikejam View Post
You shouldn't have to replace the " "s with "\ "s - just quote the string when you run mv, e.g.

$ mv "track 3.mp3" "Orbital - Belfast.mp3"

Dave
Yes, I know it works perfectly when you run it directly in the command line, but not when you do in a bash script. Is there a way to escape the quotes in a bash script?

Here is my code:

Code:
#!/bin/bash 

MINPARAMS=4

if [ $# != "$MINPARAMS" ]
then
	echo "This script needs at least 4 parameters."
	echo "Use: "
	echo "rename_song artist_name disc_name song_name song_to_rename"
	exit
else
	name_new="$1_$2_$3"
	name_old="$4"
	mv "'$name_old'" "'$name_new'"
fi
Thanks.
 
Old 01-20-2008, 05:00 PM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
It's just:

mv "$name_old" "$name_new"

Dave
 
Old 01-20-2008, 07:16 PM   #5
geek_man
Member
 
Registered: Nov 2007
Distribution: Fedora 7, Fedora 9, Solaris 10, Mac OS X, RHEL5
Posts: 71

Original Poster
Rep: Reputation: 15
Thanks

Quote:
Originally Posted by ilikejam View Post
It's just:

mv "$name_old" "$name_new"

Dave
Now it works fine, thanks. Sometimes the solution is easy, but we think it is really difficult.
 
  


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
Putting awk in bash script the_imax Programming 9 05-12-2007 03:50 PM
Using global variable in awk (bash script) kopeda Programming 2 04-24-2007 01:47 AM
can awk see bash script arguments ? sharapchi Programming 7 12-14-2006 08:03 PM
Bash script question (grep and awk) hamish Linux - Software 6 04-06-2005 03:14 PM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM

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

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