Apple开发_运行Shell脚本代码

复制+ (NSString *)run_Shell:(NSString *)shell_path {
    //CHLog(@"脚本所在地址 ==> %@\n", script_path);
    if(shell_path) {
        NSTask *script_task = [[NSTask alloc] init];
        NSPipe *pipe = [NSPipe pipe];
        [script_task setStandardOutput:pipe];
        [script_task setStandardError:pipe];
        NSFileHandle *file_handle = [pipe fileHandleForReading];
        [script_task setLaunchPath:@"/bin/sh"];
        [script_task setArguments:@[shell_path]];
        [script_task launch];
        
        NSData *data = [file_handle readDataToEndOfFile];
        NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        CHLog (@"脚本运行情况 ==> %@", string);
        
        return string;
    }
    else {
        CHLog(@"脚本不存在,请检查!");
        return nil;
    }
}
  • 注意:运行脚本在app外部,可能运行提示无权限,具体原因不清楚。建议放在app沙盒内操作。
posted @   CH520  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示
哥伦布
15°
01:09发布
哥伦布
01:09发布
15°
西南风
1级
空气质量
相对湿度
56%
今天
小雨
8°/22°
周六
多云
11°/23°
周日
大雨
15°/22°