问题
I get the error executing bash syntax error near unexpected token `('
I know the error is caused by the ')' but I thought placing the commands in-between ' ' is suppose to allow the parenthesis in a directory name. How can I fix this without renaming the name?
The matlab / octave code is:
syscmd=strcat({'bash -c '},{''''},{'cd '},dirpathpls,newdirname,{' && exec bash xfade.sh'},{''''}) %used to run script to join files in stretch directory
system(syscmd);
and it produces what is below:
bash -c 'cd /tmp/h1/clients/04212015142432811_Fs_1000_ahh/pls/03sox_a_Fs_1000_ahh_(000_bit)_(0.0000
0sig_in_deg)_to_(508_bit)_(30.00000sig_in_deg) && exec bash xfade.sh'
please note: It's being called from inside octave 3.8.1 a math program like matlab
回答1:
Using '
within a bash command line does allow the use of reserved characters like (
without escaping; however, that is not what you are doing. Everything within your '
s is being passed to bash for interpretation, bash isn't interpreting the '
s as part of the command. Something like this should work:
syscmd=strcat({'bash -c '},{''''},{'cd "'},dirpathpls,newdirname,{'" && exec bash xfade.sh'},{''''}) %used to run script to join files in stretch directory
system(syscmd);
I don't know matlab/octave, but I hope that conveys the idea. The "
should effectively escape the parens. The only pitfall there is if your directory name might have a $
or "
in it, in that case, or you have '
AND "
in your dir name, things are going to get silly.
回答2:
As I told you in your other question on this topic: Don't use bash -c
; it's not necessary for octave to run an external command, and you're doing nothing but making your life harder by trying.
command=strcat({'cd '''},
strrep(strcat(dirpathpls,newdirname),
'''',
'''"''"'''''),
{''' && exec bash xfade.sh'})
system(syscmd);
Two key differences:
- We're using the
sh -c
implicitly created by the system() call - We're escaping the filenames, preventing any malicious content within them from escaping the quotes and being executed.
How that escaping works:
Single-quoted strings in POSIX shells are ended only by a following single-quote. To insert a literal single quote into them, one needs to end the single-quoted string and then enter a different quoting type. Thus:
'"'"'
...in which the first '
ends the prior quoting type; the "
enters a double-quoted context (in which a single-quote literal can be recognized; the '
after it is then your literal single-quote character; the "
following ends the double-quoted context, and the final '
resumes a single-quoted context.
This is all made more complicated by doubling the '
s to ''
s for Octave's syntax; this is how one gets
strrep(content, '''', '''"''"''''')
...to replace all '
s with '"'"'
s.
来源:https://stackoverflow.com/questions/29781414/running-bash-script-from-within-octave-matlab-getting-error-executing-bash-syn