LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   escaping a single quote from a bash script (http://www.linuxquestions.org/questions/programming-9/escaping-a-single-quote-from-a-bash-script-821242/)

atbrew 07-21-2010 08:35 AM

escaping a single quote from a bash script
 
Hi,
There is probably a very simple fix, but its wrecking my buzz

I call the following on the command line

R CMD BATCH '--args Y_filename="one.txt" out="two.txt"' brew.R

I want to call this from a bash script but replace one.txt with $1, and two.txt with $2 (ie the first two arguments from the command line)

I have tried in vane


"R CMD BATCH '--args Y_filename=\"$1\" out=\"$2\"' brew.R"

any ideas?

Thanks in advance,
Brew

miedward 07-21-2010 08:40 AM

Quote:

Originally Posted by atbrew (Post 4040649)
Hi,

"R CMD BATCH '--args Y_filename=\"$1\" out=\"$2\"' brew.R"

Try
`R CMD BATCH \'--args Y_filename=\"$1\" out=\"$2\"\' brew.R`

pixellany 07-21-2010 08:57 AM

Backtics ( ` ) are normally used for command substition, but "$(......)" is now preferred.

Regardless, I'm not sure how this will address the question.

atbrew;
What is the command being run? eg is it "R" or "R CMD BATCH"? If the latter, then what stops the shell from just trying to run "R"?

second, why the single quotes in your first example?

atbrew 07-21-2010 09:05 AM

miedward sorry that aint working...

pixellany so I am calling a stats program call R

the arguments CMD and BATCH are arguments for R, the bit in single quotes are the arguments R then passes on to the R script brew.R

so as I said from the command line I call

R CMD BATCH '--args Y_filename="one.txt" out="two.txt"' brew.R

and I just want/need to be able to replicate that from within a bash script.

atbrew 07-21-2010 09:11 AM

Horrible hack
 
So the horrible hack we have that works is

Code:

#!/bin/bash
CMD="/usr/bin/R CMD BATCH '--args input=\"${1}\" output=\"${2}\"' brew.R"
echo $CMD > temp.sh
exec sh temp.sh


pixellany 07-21-2010 09:14 AM

Again, why the single quotes? (And why is --args inside the quotes?)

Will any of these work?

Code:

R CMD BATCH --args Y_filename=one.txt out=two.txt brew.R

R CMD BATCH --args "Y_filename=one.txt out=two.txt" brew.R

R CMD BATCH --args "Y_filename=$1 out=$2" brew.R


grail 07-21-2010 09:19 AM

So I am a little bit confused (this is easily done some times), but why not just alternate your quotes to get what you want?
Code:

/usr/bin/R CMD BATCH '--args input='"${1}"' output='"${2}" brew.R
This type works when using say awk and trying to include script variables. You may need to play with this a bit as I do not have the R program
so cannot test.

atbrew 07-21-2010 09:28 AM

grail that worked, thanks a million,

grail 07-21-2010 10:21 AM

No probs ... please remember to mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 06:15 PM.