ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Fedora 7, Fedora 9, Solaris 10, Mac OS X, RHEL5
Posts: 71
Rep:
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.
Distribution: Fedora 7, Fedora 9, Solaris 10, Mac OS X, RHEL5
Posts: 71
Original Poster
Rep:
Bash Script
Quote:
Originally Posted by ilikejam
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.