Re: Problem in bash script with single and double quotes



kittythebulldog-1@xxxxxxxxx wrote:
I have an application program called matlab that I run from an
interactive bash shell by using the command

matlab -r "load('a.mat');sum(a)"

The single and double quotes must be present as shown.

Now I would like to run the exact same line from within a bash script,
with the only difference being that the file name a.mat should be
stored in a variable. The script I wrote is:


#!/bin/bash -
l

set -x # display each
line

# interactive line that
works
# matlab -r
"load('a.mat');sum(a)"

F=a.mat
line='matlab -r "load('\'${F}\'');sum(a)"'

In your script just call it as...

matlab -r "load('${F}');sum(a)"

The variable expansion ${F} will be effective because the whole
expression is inside double quotes.

(And fix the linebreaks before you post again.)

Janis


# the echoed line is
correct
echo $line

# neither of the next two lines
works
${line}
#exec $
{line}



When I run the script the output (from having set -x) is


+ source demo.sh
++ set -x
++ F=a.mat
++ line='matlab -r "load('\''a.mat'\'');sum(a)"'
++ echo matlab -r '"load('\''a.mat'\'');sum(a)"'
matlab -r "load('a.mat');sum(a)"
++ matlab -r '"load('\''a.mat'\'');sum(a)"'


The second to last line shows the value of the variable containing the
command that I want to run. This line comes from echo is exactly what
I want. However, the last line shows that when I try to run the line
contained in the variable not all the characters in the variable are
translated, as they are by echo, and matlab doesn't run correctly.
What can I do to run the program, i.e., so that the last line is the
same as the second to last line?

Thanks.
Greg Reese


.



Relevant Pages

  • Re: Yet another "stop the bash quoting" post
    ... |> Output from running the file1.sh command is: ... |> the argument doesn't contain those single quotes; ... | By using set -x in the bash script and running /command/ without the ...
    (comp.os.linux.misc)
  • Re: Yet another "stop the bash quoting" post
    ... |> Output from running the file1.sh command is: ... |> the argument doesn't contain those single quotes; ... | By using set -x in the bash script and running /command/ without the ...
    (comp.unix.shell)
  • Re: shell escape problem?
    ... If that doesn't work, try escaping the quotes: ... bash: echo "Hello": command not found ...
    (Debian-User)
  • Re: Anonymous functions
    ... It seems like it makes shell slightly more like a functional ... Passing the result of $instead of the command makes a function ... 30 works while with your version you have to do percent "echo 10" ... using $is also more convenient as far as quotes are concerned: ...
    (comp.unix.shell)
  • Problem in bash script with single and double quotes
    ... I have an application program called matlab that I run from an ... interactive bash shell by using the command ... command that I want to run. ... This line comes from echo is exactly what ...
    (comp.unix.shell)

Loading