• shell携带附件,Linux下的自解压文件诞生了


    初衷

    windows下有自解压文件,直接双击就能释放文件,并且还能执行释放文件前后要执行的脚本。Linux下我也想要这样的功能,因为我希望直接拷贝一个shell脚本给别人,别人直接运行就能用,而不是一个压缩文件,需要别人解压。

    实现

    #!/bin/bash
    
    # write header
    cat > attachment.sh <<EOF
    #!/bin/bash
    base64 -d > attachment_test <<EOF
    EOF
    
    # write attachment base64 data
    (cat <<EOF
    #include<stdio.h>
    
    int main(int argc,char *argv[])
    {
      int i = 0;
      for (;i<argc;i++) {
        printf("%d:%s
    ",i,argv[i]);
      }
    	return 0;
    }
    EOF
    ) | gcc -s -o /dev/fd/1 -xc - | base64 >> attachment.sh
    
    # write tail
    cat >> attachment.sh <<END
    EOF
    chmod +x attachment_test
    ./attachment_test 1 2 3 4 5
    END
    
    # exec
    bash attachment.sh
    
    下面是结果
    result:
    # ./test.sh
    0:./attachment_test
    1:1
    2:2
    3:3
    4:4
    5:5
    

    执行上述shell脚本会产生如下2个文件,一个是自解压shell脚本,一个是释放的可执行程序。
    attachment.sh attachment_test

  • 相关阅读:
    COGS 2104. [NOIP2015]神奇的幻方
    洛谷 P1387 最大正方形
    包和一些常用的模块
    模块
    模块的导入和使用
    函数迭代器与生成器
    函数的小知识
    函数的闭包和装饰器
    函数的进阶
    初识函数
  • 原文地址:https://www.cnblogs.com/janbar/p/13698977.html
Copyright © 2020-2023  润新知