执行终端命令 [英] Execute a terminal command

查看:120
本文介绍了执行终端命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Objective-C项目中运行终端命令.

I want to run a terminal command from my objective-c project.

从终端运行时,我使用:

When I run it from the teminal I use :

cd /Users/user/Desktop/project/;ant release

现在我在Objective-C项目中使用了它:

now I used this in the Objective-C project:

NSTask *task = [NSTask new];
[task setLaunchPath:@"cd /Users/user/Desktop/project/;ant"];
[task setArguments:[NSArray arrayWithObjects:@"release", nil]];

NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];

[task launch];

NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];

[task waitUntilExit];
[task release];

NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog (@"got\n%@", string);
[string release];

,然后在[task launch];之后出现错误:

and after [task launch]; I get error:

launch path not accessible

编辑

我尝试使用此命令进行检查:

Edit

I tried to use this command for check:

[task setCurrentDirectoryPath:@"/Users/user/Desktop/Czech/"];
[task setLaunchPath:@"/bin/ls"];

仍然给我一个警告:

working directory doesn't exist.

推荐答案

您需要以其他方式设置工作目录:

You need to set the working directory in a different way:

[task setCurrentDirectoryPath:@"/Users/user/Desktop/project"];

然后将您的setLaunchPath:调用更改为指向实际可执行文件的位置:

Then change your setLaunchPath: call to point to the location of the actual executable:

[task setLaunchPath:@"/usr/bin/ant"];

这篇关于执行终端命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆