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