NSTask 与“cat”的问题;命令
我尝试使用 cat 命令连接文件。 当我在终端中使用它时,一切正常:
cat /Users/Home/Desktop/test.mp3* > test.mp3
尝试使用 NSTask 重现此错误,给出以下错误:
代码:
NSArray *Args = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@*",[TAFileName stringByDeletingPathExtension]],@">",[TAFileName stringByDeletingPathExtension],nil];
NSLog(@"%@",Args);
NSString *LaunchPath = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@""];
[self startTaskWithLaunchPath:LaunchPath andArguments:Args showingProcess:NO];
NSLog 输出:
(
"/Users/Home/Desktop/test.mp3*",
">",
"/Users/Home/Desktop/test.mp3"
)
错误:
cat: /Users/Home/Desktop/test.mp3*: No such file or directory
cat: >: No such file or directory
cat: /Users/Home/Desktop/test.mp3: No such file or directory
“startTaskWithLaunchPath:andArguments:showingProcess:”与许多其他终端命令一起正常工作,我 100% 确定这不是问题。
I try concatenating files using the cat command.
When I use this in the terminal, everything works fine :
cat /Users/Home/Desktop/test.mp3* > test.mp3
Trying to reproduce this using an NSTask, gives me the error below :
Code :
NSArray *Args = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%@*",[TAFileName stringByDeletingPathExtension]],@">",[TAFileName stringByDeletingPathExtension],nil];
NSLog(@"%@",Args);
NSString *LaunchPath = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@""];
[self startTaskWithLaunchPath:LaunchPath andArguments:Args showingProcess:NO];
NSLog output :
(
"/Users/Home/Desktop/test.mp3*",
">",
"/Users/Home/Desktop/test.mp3"
)
Error :
cat: /Users/Home/Desktop/test.mp3*: No such file or directory
cat: >: No such file or directory
cat: /Users/Home/Desktop/test.mp3: No such file or directory
The "startTaskWithLaunchPath: andArguments: showingProcess:" works fine with lots of other terminal commands, I'm 100% sure that's not the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是输入重定向和通配符,这是由 shell(通常是 #!/bin/bash)处理的,而不是由 cat 命令处理的。因此,使用参数启动任务以将“test.mp3*”扩展为真实的 MP3 名称,然后将其重定向到不同的位置,这不是任务可以执行的操作。
我建议您查看是否可以直接调用 shell 脚本,甚至可能在您的应用程序中动态创建它 - 然后将其作为 bash 应用程序的参数调用。然后它应该做你需要的事情。
The problem is input redirection and globbing, which is handled by the shell (typically #!/bin/bash) and not by the cat command. So starting a task with an argument to expand "test.mp3*" into a real MP3 name and then redirect it to a different location is not something that a task can do.
I would recommend seeing if you can call a shell script directly, perhaps even dynamically create it in your app - and then call it as an argument to the bash application. Then it should do what you need.
我设法使用“/bin/sh”解决了这个问题。这会将命令解析到 shell 中并像这样运行它。使用此方法仍然输出“没有这样的文件”,但我通过更改任务初始工作目录解决了这个问题。
启动任务的代码:
更改目录的代码:
这解决了一切。谢谢迈克尔的帮助。
I managed to solve the problem using "/bin/sh". This parses a command into a shell and runs it like that. Using this method still outputted "No such file", but I solved that by altering the task initial working directory.
Code for launching the task :
Code to change the directory :
That solved everything. Thanks, Michael, for your help.