0

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\)/

Tobi Weißhaar
  • 1,577
  • 6
  • 25
  • 34

3 Answers3

1

There is no need to convert the path, you can surround it in single quotes. Just use:

touch 'path'
CRD
  • 51,696
  • 5
  • 69
  • 85
1

You can enclose the parameters that contain spaces with " " marks.

touch "/Users/username/New Folder/"

At least this works at the shell prompt

Merlevede
  • 8,078
  • 1
  • 23
  • 39
0

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.

Community
  • 1
  • 1
that other guy
  • 109,738
  • 11
  • 156
  • 185