help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: safe shell strings


From: Richard KIrk
Subject: Re: safe shell strings
Date: Fri, 7 Mar 2014 08:00:03 -0800 (PST)

Hi.

Here is a simple example: using a system call to remove a file.
Yes, there are easier way of doing this, but I am wanting to call
a program of mine that you won't know about.

octave:1> system "rm filename"
rm: cannot remove `filename': No such file or directory

I have no file with this name but it is trying to do the right thing.
I can do something similar if the file name is a string variable...

octave:2> fn = "filename"
fn = filename
octave:3> system(["rm " fn])
rm: cannot remove `filename': No such file or directory

However this will not work with file names with spaces...

octave:3> fn = "file name with spaces"
fn = file name with spaces
octave:4> system(["rm " fn])
rm: cannot remove `file': No such file or directory
rm: cannot remove `name': No such file or directory
rm: cannot remove `with': No such file or directory
rm: cannot remove `spaces': No such file or directory

What I am wanting to do is to call...

   "rm file\ name\ with\ spaces"

or

   "rm \"file name with spaces\""

These commands would do the right thing when set out to the Unix bash shell
because I have used escape sequences to stop the shell splitting the string
into separate arguuments.

Sticking quotes about the string is easy. However, the name may contain the
quote character. Or the blackslash character. Or the file name may have
trailing spaces (I came across this recently). So, I would like to turn any
reasonable string into a string that the system shell would interpret as a
single argument. And I want to stop jokers using image names like "; sudo rm
-rf \ ".

You may need different escapes for different system shells. You probably
need different escape sequences for a Windows command.

Is there anything that does this?

Cheers.
RIchard Kirk

 



--
View this message in context: 
http://octave.1599824.n4.nabble.com/safe-shell-strings-tp4662798p4662825.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]