oc 執行shell 腳本


 

-(id) InvokingShellScriptAtPath :(NSString*) shellScriptPath

{

    NSTask *shellTask = [[NSTask alloc]init];

    [shellTask setLaunchPath:@"/bin/sh"];

    NSString *shellStr = [NSString stringWithFormat:@"sh %@ 參數1",shellScriptPath];

   

 

//    -c 表示將后面的內容當成shellcode來執行

 

    [shellTask setArguments:[NSArray arrayWithObjects:@"-c",shellStr, nil]];

        

    NSPipe *pipe = [[NSPipe alloc]init];

    [shellTask setStandardOutput:pipe];

        

    [shellTask launch];

        

    NSFileHandle *file = [pipe fileHandleForReading];

    NSData *data =[file readDataToEndOfFile];

    NSString *strReturnFromShell = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"The return content from shell script is: %@",strReturnFromShell);

        

    return strReturnFromShell;

 

 

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM