LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Giving multiple words as argument for bash (http://www.linuxquestions.org/questions/linux-general-1/giving-multiple-words-as-argument-for-bash-131991/)

ivanatora 01-06-2004 07:21 AM

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 :)

druuna 01-06-2004 10:01 AM

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).

bb_matt 01-06-2004 11:11 AM

Yeah - it's an interesting *nix thing !

mkdir this is a directory

will create 4 directories for each word, or argument after mkdir

but

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.

ivanatora 01-06-2004 04:42 PM

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:
Code:

(23:37:11)[ivanatora@/mnt/winc/mp3]$ playsong "wing commander - Diligent Arrives.mp3"
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2, and 3.
Version 0.59q (2002/03/23). Written and copyrights by Joe Drew.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!
wing: No such file or directory
(23:37:23)[ivanatora@/mnt/winc/mp3]$

So it gets only the first string: wing.
I got the following thing in /usr/local/bin/playsong -> mpg321 -v -o arts $1

druuna 01-06-2004 05:12 PM

mpg321 -v -o arts $1 should be mpg321 -v -o arts "$1"

Sorry 'bout that, wasn't clear in my previous post.

ivanatora 01-07-2004 03:04 AM

Thanks, that works great! :)


All times are GMT -5. The time now is 10:18 AM.