NSTask 与 bash 脚本问题
例如,我有这个简单的 bash 脚本:
#!/bin/sh
cd $1;
和它的可可包装器:
NSTask *cd = [[NSTask alloc] init];
NSString *testFolder = [NSString stringWithString:@"/Users/test/Desktop/test 1"];
[cd setLaunchPath:@"/bin/sh"];
[cd setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle]
pathForResource:@"cd" ofType:@"sh"],testFolder, nil]];
[cd launch];
[cd release];
这不能正确工作。问题是 testFolder 中的文件夹名称中有空格。 我试图像这样设置 testFolder
:
NSString *testFolder = [NSString stringWithString:@"/Users/test/Desktop/test\\ 1"]
但这是也输出相同的错误:
cd.sh: line 9: cd: /Users/test/Desktop/test: No such file or directory
路径不带空格(例如:@"/Users/ test/Desktop/test1"
)也可以。
For example, i have this simple bash script:
#!/bin/sh
cd $1;
And this cocoa wrapper for it:
NSTask *cd = [[NSTask alloc] init];
NSString *testFolder = [NSString stringWithString:@"/Users/test/Desktop/test 1"];
[cd setLaunchPath:@"/bin/sh"];
[cd setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle]
pathForResource:@"cd" ofType:@"sh"],testFolder, nil]];
[cd launch];
[cd release];
This is doesn't work correct. And the problem is space in folder name in testFolder.
I'm trying to set testFolder
like this:
NSString *testFolder = [NSString stringWithString:@"/Users/test/Desktop/test\\ 1"]
But this is also output same error:
cd.sh: line 9: cd: /Users/test/Desktop/test: No such file or directory
Paths without spaces (for example: @"/Users/test/Desktop/test1"
) works as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于
NSTask
来说,这并不是真正的问题。打开你的终端(如果你使用tcsh
,则运行bash
),然后执行此操作。你需要做的
是。这是因为
sh
的扩展规则。阅读 bash 手册,例如这里< /a>,名为EXPANSION的部分。 来自 shell 脚本入门的这一部分也可能有帮助。That's not really a problem about
NSTask
. Open your terminal (and runbash
if you usetcsh
), and doThis doesn't work. You need to do
instead. This is because of the expansion rule of the
sh
. Read the manual of bash e.g. here, the section called EXPANSION. This section from the shell scripting primer might help, too.