2017-07-28 74 views
1

執行我使用NSTask的這個例子bash命令的Objective-C的結果通過NSTask

NSTask *task = [[NSTask alloc] init]; 
[task setLaunchPath:@"/bin/bash"]; 
[task setArguments:@[ @"-c", @"cp /Directory/file /users/user_name/Desktop" ]]; 
[task launch]; 

我想知道,如果[task setArguments:]返回成功或失敗的狀態執行該命令,並保存之後要檢查的狀態。我怎樣才能得到這個結果?

+0

爲什麼要用這種方式複製文件,而不是像['NSFileManager'](https://developer.apple.com/documentation/foundation/nsfilemanager)那樣使用更直接的API? (它有['-copyItemAtPath:toPath:error:'](https://developer.apple.com/documentation/foundation/nsfilemanager/1407903-copyitematpath?language=objc)) –

回答

1

I want to know if the [task setArguments:] returns a state of success or failure for executing that command, and save the state to check afterwards.

爲什麼您認爲設置任務的參數尚未啓動並正在運行可能會返回運行命令的狀態?

How can I get that result?

閱讀NSTask的文件,其方法waitUntilExit,其財產terminationStatus

也就是說,正如@ItaiFerber在評論中提出的那樣,希望這只是一個例子,並且您並未真正使用NSTask來運行cp