Giving multiple words as argument for bash
I'm trying to make a bash script which should run mpg321 with a couple of options which I don't want to write every time when I start mp3. For that I have to pass the song's name as a command line parameter to the script. If I try $1 as a filename, it gets only the first string.. And one more thing: I start mpg321 with a filename like that:
$ mpg321 -v -o arts blind\ guardian\ -\ the\ curse\ of\ feanor.mp3
You see that '\''s in the filename.. can it make trouble?
Any other ideas would be appreciated :)
If a filname has special characters in them (like your example), the best thing to do is surround them with double qoutes ("). This way you don't have to use the backslash, and more important, bash will recognize everything between double quotes as one argument.
<bashscript> This is a test => bash sees 4 arguments ($1 = This, $2 = is etc.)
<bashscript> "This is a test" => bash sees just one argument.
This (double quoting) can also be used in your mpg123 example, the way you do it should also work (one small thing, the - should also be escaped).
Yeah - it's an interesting *nix thing !
mkdir this is a directory
will create 4 directories for each word, or argument after mkdir
mkdir "this is a directory"
will create one directory with that name.
Hence the prevalence of the - or the _ symbol on the internet ?
A long time ago, as a webmaster, I applied the basic rules of no spaces and no caps to my filenames and never looked back since. It seems a bit odd now, in 2004, but I guess it still applies.
Pfh.. it would be nice if all mp3s was with _s instead of spaces.. That would really save me a lot of troubles..
By the way, it appears that "one two three - song" isn't one argument.. I got the following:
I got the following thing in /usr/local/bin/playsong -> mpg321 -v -o arts $1
mpg321 -v -o arts $1 should be mpg321 -v -o arts "$1"
Sorry 'bout that, wasn't clear in my previous post.
Thanks, that works great! :)
|All times are GMT -5. The time now is 10:18 AM.|