Reputation: 1677
I want to execute a shell command (I want to touch
a file). I use system("shell command")
to execute the command.
For example I want to touch the file at path /Users/username/New Folder/
. Now I need to convert the NSString
in a format that is conform to shell commands like /Users/username/New\ Folder
.
Is there any method that does a conversion like this?
NOTE: It is NOT just replacing a whitespace with \
. If you have a special character in the path like /Users/username/Folder(foo)/
the "shell path" looks like this /Users/username/Folder\(foo\)/
Upvotes: 0
Views: 74
Reputation: 123400
Don't use system
. It's insecure and unpredictable. Surrounding the string with quotes is not sufficient.
Use the execve style functions instead. They are simple and secure.
Upvotes: 0
Reputation: 8170
You can enclose the parameters that contain spaces with " " marks.
touch "/Users/username/New Folder/"
At least this works at the shell prompt
Upvotes: 1
Reputation: 53000
There is no need to convert the path, you can surround it in single quotes. Just use:
touch 'path'
Upvotes: 1