美文网首页
macos开发,在程序里面执行shell脚本

macos开发,在程序里面执行shell脚本

作者: 朱传武 | 来源:发表于2020-12-14 09:15 被阅读0次

上一篇我写了在xcode里面添加run script来执行脚本,可是打包之后发现,只有用xcode运行的时候才会执行这个shell命令,查了很多资料,最后我觉得应该是我理解错了,这里的run script很可能只是为了方便自动化打包之类的场景适用,而并不是真正的运行时执行脚本。那该怎么实现呢?
参阅博客一
参阅博客二
参阅博客三

- (NSString *)cmd:(NSString *)cmd
{
    // 初始化并设置shell路径
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/bash"];
    // -c 用来执行string-commands(命令字符串),也就说不管后面的字符串里是什么都会被当做shellcode来执行
    NSArray *arguments = [NSArray arrayWithObjects: @"-c", cmd, nil];
    [task setArguments: arguments];
    
    // 新建输出管道作为Task的输出
    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];
    
    // 开始task
    NSFileHandle *file = [pipe fileHandleForReading];
    [task launch];
    
    // 获取运行结果
    NSData *data = [file readDataToEndOfFile];
    return [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
}

但是有一个问题,因为脚本是:

#!/bin/bash
rm -rf ~/Downloads/keystore.xml
#chmod -x write.sh
/usr/local/bin/adb pull /data/data/com.whatsapp/shared_prefs/keystore.xml ~/Downloads/keystore.xml
cat ~/Downloads/keystore.xml

adb需要管理员权限,所以一直提示没有权限执行脚本。

NSString *script =  [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", fullScript];

查了一下资料,可以用with administrator privileges,这时候又报"用户名密码不正确",不知道为啥,最终抛弃了NSTask,找到了这个库,它自动帮我们处理权限问题,最终:

- (void)runSTPrivilegedTask {
    NSString *cmd = @"/bin/sh launcher.sh";
    
    STPrivilegedTask *privilegedTask = [[STPrivilegedTask alloc] init];
    
    NSMutableArray *components = [[cmd componentsSeparatedByString:@" "] mutableCopy];
    NSString *launchPath = components[0];
    [components removeObjectAtIndex:0];
    
    [privilegedTask setLaunchPath:launchPath];
    [privilegedTask setArguments:components];
    [privilegedTask setCurrentDirectoryPath:[[NSBundle mainBundle] resourcePath]];
    
    // Set it off
    OSStatus err = [privilegedTask launch];
    if (err != errAuthorizationSuccess) {
        if (err == errAuthorizationCanceled) {
            NSLog(@"User cancelled");
            return;
        }  else {
            NSLog(@"Something went wrong: %d", (int)err);
            // For error codes, see http://www.opensource.apple.com/source/libsecurity_authorization/libsecurity_authorization-36329/lib/Authorization.h
        }
    }
    
    [privilegedTask waitUntilExit];
    
    // Success! Now, read the output file handle for data
    NSFileHandle *readHandle = [privilegedTask outputFileHandle];
    NSData *outputData = [readHandle readDataToEndOfFile]; // Blocking call
    NSString *outputString = [[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding];
    //[self.outputTextField setString:outputString];
    NSLog(@"输出%@",outputString);
    
    NSString *exitStr = [NSString stringWithFormat:@"Exit status: %d", privilegedTask.terminationStatus];
    NSString *result = [[outputString componentsSeparatedByString:@"\"client_static_keypair\">"]  [1]  componentsSeparatedByString:@"</string>"][0];
      NSLog(@"输出臭豆腐的%@",exitStr);

            
    dic=[[NSMutableDictionary alloc] init];
    [dic setObject:[NSString stringWithFormat:@"%@==",result] forKey:@"client_static_keypair"];
            //  [dic setObject:[NSString stringWithFormat:@"%@==",parser.nodeDict[@"server_static_public"]] forKey:@"server_static_public"];
            // dic=parser.nodeDict;
             
          //   NSLog(@"-->%d",isYES);
        // }
     
    
}

按理说这样应该就没啥问题了,可是这是由又报someting went wrong:6005,又是一顿google,最终发现

image.png
必须不能用sandbox功能,关闭sandbox,果然成功:
image.png
这样每次执行shell的时候都会要求授权,终于搞定,最终我也因此获得了客户的赞誉:
image.png
最终源码

相关文章

  • macos开发,在程序里面执行shell脚本

    上一篇[https://www.jianshu.com/p/0e2b34f25006]我写了在xcode里面添加r...

  • Linux运维学习一

    一、shell脚本 当命令或程序语句不在命令行下执行,而是通过一个程序文件来执行时,该程序就被称为shell脚本。...

  • 8-JShell工具

        Shell是脚本程序的含义,在很多的编程语言里面为了方便使用者进行代码的开发,都会有Shell交互编程环境...

  • shell脚本编写

    shell脚本格式 文件首行指定执行shell的程序以及相关说明 shell 脚本文件后缀,建议命名为.sh 脚本...

  • 2016.9.7 AM [shell的使用]

    0。运行shell脚本程序 1.增加1.sh的执行权限:chmod +x 1.sh执行shell脚本:./1....

  • [Linux]Shell

    shell:命令解释器,驱动linux内核;应用程序调用shell命令 1.Shell脚本的执行方式 脚本格式要求...

  • Shell脚本简单使用

    1. 执行shell脚本 1.作为可执行程序执行新创建的.sh文件需要修改shell脚本为可执行的文件- chmo...

  • shell脚本执行source报错

    在Ubuntu 16.04机器在执行shell脚本时,报source: not found原因是shell脚本执行...

  • Shell -X命令

    shell "-x"选项可用来跟踪脚本的执行,是调试shell脚本的强有力工具。“-x”选项使shell在执行脚本...

  • windows环境编写shell脚本执行失败,linux环境下查

    最近开发shell脚本,为了方便,在window环境下开发,推送到linux环境下执行,结果嘞,总是执行失败,失败...

网友评论

      本文标题:macos开发,在程序里面执行shell脚本

      本文链接:https://www.haomeiwen.com/subject/bxnmgktx.html