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,617
  • 6
  • 26
  • 35

3 Answers3

1

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

touch 'path'
CRD
  • 52,522
  • 5
  • 70
  • 86
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,140
  • 1
  • 24
  • 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
  • 116,971
  • 11
  • 170
  • 194