LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-17-2007, 01:58 PM   #1
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,370

Rep: Reputation: Disabled
Bash: how do I pass a double quoted string to a command?


I'm trying to write a shell script to rip CDs using cdda2wav. I have the track names in a file and I'm reading these in and then creating a string like so:

Code:
#!/bin/bash

# Save stdin
exec 6<&0

# stdin is now tracks
exec < tracks

count=0
command1="cdda2wav -s -t"

while read line
do
  let "count += 1"
  command2="$command1 $count -x dev=/dev/cdrw \""$line\"
  $command2
done

exec 0<&6 6<&-
If I echo command2, I get what I expect, e.g.

cdda2wav -s -t 1 -x dev=/dev/cdrw "Ghost Is Back".

This is what I'd usually use on the command line. The problem, however is that when the command is executed in my script, the file name output for the above cdda2wav example ends up being "Ghost.wav, i.e. there's a " at the beginning and the title isn't complete.

What am I doing wrong?

Thanks!
 
Old 04-17-2007, 02:21 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 64
Change
Code:
  command2="$command1 $count -x dev=/dev/cdrw \""$line\"
to
Code:
  command2="$command1 $count -x dev=/dev/cdrw \"$line\""
 
Old 04-17-2007, 02:26 PM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,370

Original Poster
Rep: Reputation: Disabled
That hasn't made a difference.
 
Old 04-17-2007, 03:10 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 7,514
Blog Entries: 1

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
Hi,

Sorry, came up with the same as the prvious replyer.......
 
Old 04-17-2007, 03:12 PM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,370

Original Poster
Rep: Reputation: Disabled
OK, there's something wrong with me. I can't see the difference between your line, druuna and osor's line (other than the colours!).
 
Old 04-17-2007, 08:15 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 64
The red double-quote is the one to move…
 
Old 04-17-2007, 11:16 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Instead of using echo, use a program that outputs its command line arguments:
Code:
['~/bin/args.py', 'cdda2wav', '-s', '-t', '1', '-x', 'dev=/dev/cdrw', '"Ghost', 'Is', 'Back"']
So you can see the double quotes are in the right place but weren't interpreted by bash. You need to use eval $command2, eval will cause bash to interpret things properly.
 
Old 04-18-2007, 01:17 AM   #8
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,370

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by osor
The red double-quote is the one to move…
I said that didn't help (well, by itself). It was using "eval $command2" instead of just "$command2" that worked.

Quote:
Originally Posted by ntubski
Instead of using echo, use a program that outputs its command line arguments:
Code:
['~/bin/args.py', 'cdda2wav', '-s', '-t', '1', '-x', 'dev=/dev/cdrw', '"Ghost', 'Is', 'Back"']
So you can see the double quotes are in the right place but weren't interpreted by bash. You need to use eval $command2, eval will cause bash to interpret things properly.
I'm not sure what the first part of your answer means, but using "eval $command2" works. Thanks!

Quick question: I don't really need to save stdin and restore it at the end, do I?

Last edited by Nylex; 04-18-2007 at 01:35 AM.
 
Old 04-18-2007, 09:36 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by Nylex
I'm not sure what the first part of your answer means, but using "eval $command2" works. Thanks!
echo can only show you what the command line looks like as a string:
Code:
~% echo foo bar                                                                
foo bar
~% echo 'foo bar'                                                              
foo bar
~% args.py foo bar                                                             
['~/bin/args.py', 'foo', 'bar']
~% args.py 'foo bar'                                                           
['~/bin/args.py', 'foo bar']
Quote:
Quick question: I don't really need to save stdin and restore it at the end, do I?
how about
Code:
#!/bin/bash
count=0
command1="cdda2wav -s -t"

while read line
do
  let "count += 1"
  command2="$command1 $count -x dev=/dev/cdrw \""$line\"
  eval $command2
done <tracks
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
escape string in bash script so it can be used in command line BuckRogers01 Linux - Software 15 08-12-2010 09:38 AM
Bash script - passing command as string lenzj Programming 3 08-24-2006 11:36 AM
[Bash] execute string as command michael_hk Linux - Newbie 2 06-26-2006 03:34 AM
string to double cynthia Programming 4 10-12-2004 02:40 AM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 03:12 PM


All times are GMT -5. The time now is 03:02 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
Open Source Consulting | Domain Registration