Basic Bash Scripting Question

Discuss Programming
User avatar
Calum
guru
guru
Posts: 1348
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Fri May 16, 2003 8:59 am

well i could easily go and take the spaces out of the filenames one by one but that would be avoiding rather than solving the problem, and i'd also quite like to know how to make the script treat the filenames properly even though they have spaces in them.
when i just type the commands they take filenames with spaces in fine, so it's definitely something to do with the variables we are using to represent the filenames in the script...

Nobber
user
user
Posts: 32
Joined: Thu Jan 23, 2003 8:36 am
Location: Slackville

Post by Nobber » Fri May 16, 2003 9:13 am

Have you tried putting double quotes round the filenames? For example:

Code: Select all

rm "${file%%.wma}.wav"

User avatar
Void Main
Site Admin
Site Admin
Posts: 5712
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Fri May 16, 2003 5:20 pm

Spaces cause problems in most operating systems I have ever used when using them as parameters. Remember when I explained how the shell expanded the wildcards before passing them to the script?

e.g.
Suppose you have three wma files in a directory one containing spaces

Code: Select all

myfile.wma
myfile2.wma
my file.wma
You execute the following command:

Code: Select all

wma2mp3 *.wma
Before the script is executed the shell expands the wildcard so it would be exactly the same as if you had typed:

Code: Select all

wma2mp3 myfile.wma myfile2.wma my file.wma
The script would see the above command as having four parameters because parameters are separated by spaces. You should be able to do this:

Code: Select all

wma2mp3 "*.wma"
and it should work. Here, I just added some more to your script so you can just pass the directory name containing wma files to convert, as well as using wildcards. I also moved the conversion process into a function:

Code: Select all

#!/bin/bash
#############################################################
# wma2mp3 by Calum and Void Main
#
# e.g. wma2mp3 myfile.wma
# e.g. wma2mp3 myfile.wma myfile2.wma myfile3.wma
# e.g. wma2mp3 "my file.wma" "my file 2.wma" "my file 3.wma"
# e.g. wma2mp3 "*.wma"
# e.g. wma2mp3 /directory/containing/wma/files
# e.g. wma2mp3 .
#############################################################

#############################################################
# Move the conversion process into a function that can
# be called.
# The "&&" makes it so each step must be successful before
# the next step will be done.
#############################################################
function wma2mp3 () {
  if [ ! -f "$1" ]; then
     echo "File $1 not found!"
  else
     mplayer -ao pcm -aofile "${1%%.[Ww][Mm][Aa]}.wav" "$1" &&
     lame -h -b 192 "${1%%.[Ww][Mm][Aa]}.wav" "${1%%.[Ww][Mm][Aa]}.mp3" &&
     rm -f "${1%%.[Ww][Mm][Aa]}.wav" ||
     echo "There was a problem with the conversion process!"
  fi
}

#############################################################
# Not enough information to compute
#############################################################
if [ $# -lt 1 ]; then
   echo "Syntax: `basename $0` <wmaFilename(s)|wmaDirectory>"
   exit
fi

#############################################################
# Directory was passed so convert all wma files in directory
#############################################################
if [ $# -eq 1 -a -d "$1" ]; then
  for file in $1/*.[Ww][Mm][Aa]
  do
    wma2mp3 "$file"
  done
  exit
fi

#############################################################
# One or more wma files were passed, convert them
#############################################################
for file in $*
do
  wma2mp3 "$file"
done
exit
I put a copy of the above script here in case you have copy/paste trouble. I noticed in your previous code snips you had right quote marks (') where there should have been left quotes (`). Specifically where I used the "basename" command.

$ wget http://voidmain.is-a-geek.net/files/wma2mp3
$ chmod +x wma2mp3
$ ./wma2mp3

User avatar
Calum
guru
guru
Posts: 1348
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Sat May 17, 2003 3:20 am

nobber, i didn't try that but i did know about it so i should have thought of it (slaps forehead in simulated dumbness)
Void Main wrote: The script would see the above command as having four parameters because parameters are separated by spaces.
yes, that was the effect i was seeing right enough.
Here, I just added some more to your script so you can just pass the directory name containing wma files to convert, as well as using wildcards. I also moved the conversion process into a function:
.....
I put a copy of the above script here in case you have copy/paste trouble.
once again void main, thank you for your time in writing this script out for me, it has illuminated quite a few little points to me about bash scripting.
I noticed in your previous code snips you had right quote marks (') where there should have been left quotes (`). Specifically where I used the "basename" command.
you know i didn't realise i could have left and right quote marks, are they supposed to be separate keys on the keyboard do you reckon?i appear to only have one key for quote marks and one apostrophe key (for single quotes also i presume). weird.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5712
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Sat May 17, 2003 7:16 am

Calum wrote:you know i didn't realise i could have left and right quote marks, are they supposed to be separate keys on the keyboard do you reckon?i appear to only have one key for quote marks and one apostrophe key (for single quotes also i presume). weird.
I don't know how your keyboard is layed out but on the standard US keyboard it is the key in the top left corner right under the ESC key. When using the left quote marks it signifies that what is inside those quote marks should be executed and the result of the executed system command should be placed in the string. The right quote marks means whatever is within the strings will be treated literally. That is if you do '$something' will output '$something' rather than whatever is in the variable.

falosa
n00b
n00b
Posts: 1
Joined: Sat Oct 09, 2004 11:05 am

Post by falosa » Sat Oct 09, 2004 11:18 am

Hi,
This script still does not work for me if I use filenames with spaces or wildcards. does it happens only to me? I have modified it to accept those parameters. I have also modified the help at the beginning because, for example, wildcards does not need double quotes now.


#!/bin/bash
#############################################################
# wma2mp3 v0.2
# by Calum and Void Main
# modified by falosa
#
# e.g. wma2mp3 myfile.wma
# e.g. wma2mp3 myfile.wma myfile2.wma myfile3.wma
# e.g. wma2mp3 "my file.wma" "my file 2.wma" "my file 3.wma"
# e.g. wma2mp3 my\ file.wma my\ file\ 2.wma my\ file\ 3.wma
# e.g. wma2mp3 *.wma
# e.g. wma2mp3 /directory/containing/wma/files
# e.g. wma2mp3 .
#
# http://voidmain.is-a-geek.net/forums/vi ... .php?t=407
#
# History:
# 16 May 2003 - v0.1 wma2mp3 script created
#############################################################

#############################################################
# Move the conversion process into a function that can
# be called.
# The "&&" makes it so each step must be successful before
# the next step will be done.
#############################################################
function wma2mp3 () {
if [ ! -f "$1" ]; then
echo "File $1 not found!"
else
mplayer -ao pcm -aofile "${1%%.[Ww][Mm][Aa]}.wav" "$1" &&
lame -h -b 192 "${1%%.[Ww][Mm][Aa]}.wav" "${1%%.[Ww][Mm][Aa]}.mp3" &&
rm -f "${1%%.[Ww][Mm][Aa]}.wav" ||
echo "There was a problem with the conversion process!"
fi
}

#############################################################
# Not enough information to compute
#############################################################
if [ $# -lt 1 ]; then
echo "Syntax: `basename $0` <wmaFilename(s)|wmaDirectory>"
exit
fi

#############################################################
# Directory was passed so convert all wma files in directory
#############################################################
if [ $# -eq 1 -a -d "$1" ]; then
for file in $1/*.[Ww][Mm][Aa]
do
wma2mp3 "$file"
done
exit
fi

#############################################################
# One or more wma files were passed, convert them
#############################################################
while [ 1 -le $# ]
do
wma2mp3 "$1"
shift
done
exit

User avatar
Calum
guru
guru
Posts: 1348
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Sun Aug 28, 2005 4:19 pm

i just realised that the wma2mp3 script no longer works, due to -aofile being a deprecated switch for mplayer, here it is in an amended form:

Code: Select all

#!/bin/bash
#############################################################
# wma2mp3 v0.1
# by Calum and Void Main
#
# e.g. wma2mp3 myfile.wma
# e.g. wma2mp3 myfile.wma myfile2.wma myfile3.wma
# e.g. wma2mp3 "my file.wma" "my file 2.wma" "my file 3.wma"
# e.g. wma2mp3 "*.wma"
# e.g. wma2mp3 /directory/containing/wma/files
# e.g. wma2mp3 .
#
# http://voidmain.is-a-geek.net/forums/viewtopic.php?t=407
#
# History:
# 16 May 2003 - v0.1 wma2mp3 script created
# 27 August 2005 - v0.11 -aofile had been deprecated, corrected
#############################################################

#############################################################
# Move the conversion process into a function that can
# be called.
# The "&&" makes it so each step must be successful before
# the next step will be done.
#############################################################
function wma2mp3 () {
  if [ ! -f "$1" ]; then
     echo "File $1 not found!"
  else
     mplayer -ao pcm:file="${1%%.[Ww][Mm][Aa]}.wav" "$1" &&
     lame -h -b 192 "${1%%.[Ww][Mm][Aa]}.wav" "${1%%.[Ww][Mm][Aa]}.mp3" &&
     rm -f "${1%%.[Ww][Mm][Aa]}.wav" ||
     echo "There was a problem with the conversion process!"
  fi
}

#############################################################
# Not enough information to compute
#############################################################
if [ $# -lt 1 ]; then
   echo "Syntax: `basename $0` <wmaFilename(s)|wmaDirectory>"
   exit
fi

#############################################################
# Directory was passed so convert all wma files in directory
#############################################################
if [ $# -eq 1 -a -d "$1" ]; then
  for file in $1/*.[Ww][Mm][Aa]
  do
    wma2mp3 "$file"
  done
  exit
fi

#############################################################
# One or more wma files were passed, convert them
#############################################################
for file in $*
do
  wma2mp3 "$file"
done
exit
however, yes i still cannot use files which contain spaces. perhaps it'd be better to get the script to convert spaces to underscores prior to doing its business.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5712
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Sun Aug 28, 2005 5:14 pm

Add this line near the top of your script:

IFS=

Not only will it now work with files with spaces but you don't have to use quotes around wildcarded files anymore. For example:

wma2mp3 *.wma

and

wma2mp3 "my file.wma"

I updated the script on the web site:

http://voidmain.is-a-geek.org/files/scripts/wma2mp3

User avatar
Calum
guru
guru
Posts: 1348
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Sun Nov 13, 2005 10:43 am

ok, well i needed one to do realmedia files too, so i edited wma2mp3 and came up with the suspiciously similar ra2mp3. now both can be found at http://www.polytheism.org.uk/openopen/files/ which also contains other useful files that you (plural) might be interested in.

my hope is to make them interactive and possibly have one script for a variety of inputs and outputs and then possibly to put it in an rpm that makes sure you have all the right dependencies etc. I know there are many here that could do this while whistling, eating a sandwich and watching the simpsons but allow me my little journey, eh? :-)

User avatar
Calum
guru
guru
Posts: 1348
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Mon Dec 19, 2005 2:23 pm

ok dudes!

this script now has it's own website! it can be found at http://wma2mp3.501megs.com

the aim of me telling you this is two fold. Firstly i want to ask voidmain (switches to addressing voidmain in the first rather than third person) if this is alright with you, you being a collaborator in the original script etc. I've stated on the page that the use and augmentation of these scripts is governed by the terms of the GPL.

(now a quick switch to addressing everybody else in the plural) The second thing is to make people aware of it. If you need to convert an audio file (or more specifically many audio files) into an mp3 (or mp3s) without having to type a long esoteric command in then please feel free to use these scripts. I have written the installation instructions to aim at people who have little to no linux or bash experience since those are the people (i think) who will find the most use out of these scripts.

and i am writing the usage instructions right now.
Last edited by Calum on Mon Dec 19, 2005 3:06 pm, edited 1 time in total.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5712
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Dec 19, 2005 2:44 pm

No problem my friend. Feel free to do whatever you want with it and don't feel like you need to consult me on it.

ZiaTioN
administrator
administrator
Posts: 460
Joined: Tue Apr 08, 2003 3:28 pm
Contact:

Post by ZiaTioN » Mon Dec 19, 2005 3:37 pm

Void still owes me royalties for his hill climbing application! LOL..

Forget open source and helping people. I am in it strictly for the cash money. I am hoping to get employed with Microsoft so I can help kill the free source code movement.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5712
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Mon Dec 19, 2005 3:41 pm

Speaking of that, one of our guys just got hired by M$ and is moving up to Redmond. Ewwwwwwwwwww!!!!

worker201
guru
guru
Posts: 668
Joined: Sun Jun 13, 2004 6:38 pm
Location: Hawaii

Post by worker201 » Mon Dec 19, 2005 4:01 pm

Just for kicks, I suggest making it shell independent. So somebody using tcsh or ksh can use the scripts too. If it's somewhat easy. Nothing complicated like a shell sniffer or anything, just making the thing #!/bin/sh compatible. If it's easy, that is. If it would require a complete rewrite/redesign of the whole thing, don't even bother.

User avatar
Calum
guru
guru
Posts: 1348
Joined: Fri Jan 10, 2003 11:32 am
Location: Bonny Scotland
Contact:

Post by Calum » Mon Dec 19, 2005 6:20 pm

well it really only runs mplayer and lame, which aren't shell commands, so it'd not be a huge problem to write it for other shells.

i'm not sure it's worth it though. remember you don't have to be using bash as your default or current shell, you just have to have it installed. i suppose i could just change it to sh...

Post Reply