LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 01-06-2004, 06:21 AM   #1
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Rep: Reputation: 31
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
 
Old 01-06-2004, 09:01 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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).
 
Old 01-06-2004, 10:11 AM   #3
bb_matt
Member
 
Registered: Jan 2004
Distribution: Slackware 9.1
Posts: 35

Rep: Reputation: 15
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.

Last edited by bb_matt; 01-06-2004 at 10:13 AM.
 
Old 01-06-2004, 03:42 PM   #4
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Original Poster
Rep: Reputation: 31
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
 
Old 01-06-2004, 04:12 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
mpg321 -v -o arts $1 should be mpg321 -v -o arts "$1"

Sorry 'bout that, wasn't clear in my previous post.
 
Old 01-07-2004, 02:04 AM   #6
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Original Poster
Rep: Reputation: 31
Thanks, that works great!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash] Put words from file to array mispunt Programming 4 11-04-2004 10:53 AM
BASH: First words in a line JordanH Programming 7 10-24-2004 10:00 AM
bash: /bin/rm: Argument list too long? FiveFlat Linux - General 5 08-11-2004 11:29 PM
-bash: /bin/mv: Argument list too long balanagireddy Linux - General 2 07-27-2004 11:19 PM
how to find out if argument is number: bash kubicon Linux - Newbie 1 09-19-2003 04:12 PM


All times are GMT -5. The time now is 12:48 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration