2011-12-03 11 views
1

このコードでNSTaskにどのように引数(この場合はホスト)を渡すのですか?ホストNSStringを受け入れません。 pingを使用してホスト値を渡すと、たとえば、コマンドラインツールを実行しているときのNSTaskと引数

[NSArray arrayWithObjects:@"-c",@"ping -c 5 www.google.com",nil] 

のようになります。しかし、それはホストの議論を別々に取ることはありません。事前に助けてくれてありがとう。

task = [[NSTask alloc] init]; 
[pipe release]; 
pipe = [[NSPipe alloc] init]; 
[task setStandardInput: [NSPipe pipe]]; 

[task setLaunchPath:@"/bin/bash"]; 

NSArray *args = [NSArray arrayWithObjects:@"-c",@"ping -c 5",host,nil]; 

[task setArguments:args]; 
[task setStandardOutput:pipe]; 
NSFileHandle *fh = [pipe fileHandleForReading]; 

答えて

3

使用stringWithFormat方法引用符であなたのコマンドを取る必要-c

task = [[NSTask alloc] init]; 
     [pipe release]; 
     pipe = [[NSPipe alloc] init]; 
     [task setStandardInput: [NSPipe pipe]]; 

    [task setLaunchPath:@"path"]; 

    NSArray *args = [NSArray arrayWithObjects:@"-c",[NSString stringWithFormat: @"%@ %@ %@ %@",@"ping",@"-c",@"5",host],nil]; 

    [task setArguments:args]; 
    [task setStandardOutput:pipe]; 
    NSFileHandle *fh = [pipe fileHandleForReading]; 
+0

ありがとうございます。それは魅力のように働いた。 – ZionKing

1

あなたの議論は正しくありません。まず、launchpathを/ bin/pingに設定するか、タスクがどこにあっても、引数は普通はコマンドラインに入力する引数の配列でなければならないが、スペースで区切る必要がある。

これを正しく行う方法の詳細については、このチュートリアルWrapping UNIX commandsを参照してください。 NSStringクラスの

0
NSMutableArray *args = [NSMutableArray array]; 
NSArray *args = [NSArray arrayWithObjects:@"-c", @"\"ping -c 5", host, @"\"",nil] 
[task setArguments:args]; 

バッシュ。

関連する問題