How do Linux binary installers (.bin, .sh) work?

前端 未结 5 1682
故里飘歌
故里飘歌 2020-11-29 15:47

Some software (for ex. the NetBeans IDE) ship the Linux installers in .sh files. Curious about how exactly they \'package\' a whole IDE into a \'shell script\', I opened the

5条回答
  •  鱼传尺愫
    2020-11-29 16:32

    Basically, it's a shell script prepended to a compressed archive of some sort, such as a tar archive. You use the tail or sed command on yourself (the $0 variable in Bourne shell) to strip off the shell script at the front and pass the rest to your unarchiver.

    For example, create the following script as self-extracting:

    #!/bin/sh -e
    sed -e '1,/^exit$/d' "$0" | tar xzf - && ./project/Setup
    exit
    

    The sed command above deletes all lines from the first line of the file to the first one that starts with "exit", and then passes the rest on through. If what starts immediately after the "exit" line is a tar file, the tar command will extract it. If that's successful, the ./project/Setup file (presumably extracted from the tarball) will be executed.

    Then:

    mkdir project
    echo "#!/bin/sh" > project/Setup
    echo "echo This is the setup script!" >> project/Setup
    chmod +x project/Setup
    tar czf - project >> self-extracting
    

    Now, if you get rid of your old project directory, you can run self-extracting and it will extract that tar file and run the setup script.

提交回复
热议问题