Linux二进制安装程序(.bin,.sh)如何工作? [英] How do Linux binary installers (.bin, .sh) work?
问题描述
某些软件(例如 NetBeans IDE )将Linux安装程序以.sh文件形式提供.我很好奇他们如何将整个IDE打包为"shell脚本",我在编辑器中打开了该文件.我看到了一些纯文本shell脚本代码,然后看到了一些乱码,我认为这是二进制"或非普通文本.
我想知道它们如何混合普通的shell脚本,然后可能将它们称为不可读"的东西,即二进制文件.
对此有何见解?
基本上,它是一个Shell脚本,位于某种压缩存档中,例如tar存档.您可以在自己上使用tail
或sed
命令(Bourne shell中的$0
变量)来剥离前面的shell脚本,并将其余的脚本传递给您的非归档文件.
例如,将以下脚本创建为self-extracting
:
#!/bin/sh -e
sed -e '1,/^exit$/d' "$0" | tar xzf - && ./project/Setup
exit
上面的sed
命令删除从文件的第一行到以"exit"开头的第一行的所有行,然后将其余的行继续通过.如果在"exit"行之后立即开始的是tar文件,则tar
命令将提取该文件.如果成功,将执行./project/Setup
文件(可能是从压缩包中提取的).
然后:
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
现在,如果您摆脱了旧的project
目录,则可以运行self-extracting
,它将提取该tar文件并运行安装脚本.
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 file in an editor. I saw some plain text shell scripting code and then some random gibberish, which I reckon is 'binary' or non-plain text.
I am wondering how they mix plain shell scripts and then probably call the 'non-readable' stuff, which would be the binaries.
Any insight on this?
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.
这篇关于Linux二进制安装程序(.bin,.sh)如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!