NSTask 与“cat”的问题;命令

发布于 2024-10-09 03:43:29 字数 958 浏览 6 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2024-10-16 03:43:30

问题是输入重定向和通配符,这是由 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.

贵在坚持 2024-10-16 03:43:30

我设法使用“/bin/sh”解决了这个问题。这会将命令解析到 shell 中并像这样运行它。使用此方法仍然输出“没有这样的文件”,但我通过更改任务初始工作目录解决了这个问题。

启动任务的代码:

NSArray *Args = [NSArray arrayWithObjects:@"-c",[NSString stringWithFormat:@"cat %@.* >> %@",[[TAFileName lastPathComponent] stringByDeletingPathExtension],[[TAFileName lastPathComponent] stringByDeletingPathExtension]],nil];
NSString *LaunchPath = @"/bin/sh";
[self startTaskWithLaunchPath:LaunchPath andArguments:Args showingProcess:NO];

更改目录的代码:

[TATask setCurrentDirectoryPath:[NSString stringWithFormat:@"%@/",[TAFileName stringByDeletingLastPathComponent]]];

这解决了一切。谢谢迈克尔的帮助。

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 :

NSArray *Args = [NSArray arrayWithObjects:@"-c",[NSString stringWithFormat:@"cat %@.* >> %@",[[TAFileName lastPathComponent] stringByDeletingPathExtension],[[TAFileName lastPathComponent] stringByDeletingPathExtension]],nil];
NSString *LaunchPath = @"/bin/sh";
[self startTaskWithLaunchPath:LaunchPath andArguments:Args showingProcess:NO];

Code to change the directory :

[TATask setCurrentDirectoryPath:[NSString stringWithFormat:@"%@/",[TAFileName stringByDeletingLastPathComponent]]];

That solved everything. Thanks, Michael, for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文