safe shell strings

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

safe shell strings

Richard Kirk
Hi.

Is there any generic function that takes a string such as a filename, and turns it into something that can be quoted in a single command line string for the shell that system() is going to use? I can't find one, but it took me a long time to find strtrim() so there might be one out there. If you know there isn't one, please let me know so I can stop looking.

I don't need help writing such a tool. Well, actually I probably do, but I am going to strtrim() the name and stick it in double quotes. I don't know if that works in Windows shells, but It WIll Do For Now. However, I would much rather be using some well-debugged tool, instead of re-inventing the wheel, as my wheels sometimes come out triangular.

Cheers.
Richard Kirk
Reply | Threaded
Open this post in threaded view
|

RE: safe shell strings

Damian
> Is there any generic function that takes a string such as a filename, and turns it
> into something that can be quoted in a single command line string for the shell
> that system() is going to use? I can't find one, but it took me a long time to find
> strtrim() so there might be one out there. If you know there isn't one, please
> let me know so I can stop looking.

I have a feeling "strcat" is your friend. It stands for "concatenate strings". And no, I have no idea exactly *how* it stands for that...

> strcat("A","B","C")
ans = ABC

Damian
CONFIDENTIAL: The information contained in this email communication is confidential information intended only for the use of the addressee. Unauthorized use, disclosure or copying of this communication is strictly prohibited and may be unlawful. If you have received this communication in error, please notify us immediately by return email and destroy all copies of this communication, including all attachments.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: safe shell strings

Andreas Weber-2
In reply to this post by Richard Kirk
On 06.03.2014 17:38, Richard KIrk wrote:
> Is there any generic function that takes a string such as a filename, and
> turns it into something that can be quoted in a single command line string
> for the shell that system() is going to use?

I don't understand your question but...
If you want to form filenames, have a look at "fullfile" and "fileparts"
These use filesep() which is system-dependent:

on GNU/Linux:
octave:> fullfile("/home","user","myfile.log")
ans = /home/user/myfile.log

On windows you'll get backslashes..

Or are you searching for escape functions like "do_string_escapes" and
"undo_string_escapes"?

> Well, actually I probably do, but I
> am going to strtrim() the name and stick it in double quotes. I don't know
> if that works in Windows shells, but It WIll Do For Now.

If you explain what you are trying to do it's more likely that you get
more applicable answers.

-- Andy
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: safe shell strings

Richard Kirk
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