Linux二进制安装程序(.bin,.sh)如何工作? [英] How do Linux binary installers (.bin, .sh) work?

查看:198
本文介绍了Linux二进制安装程序(.bin,.sh)如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些软件(例如 NetBeans IDE )将Linux安装程序以.sh文件形式提供.我很好奇他们如何将整个IDE打包为"shell脚本",我在编辑器中打开了该文件.我看到了一些纯文本shell脚本代码,然后看到了一些乱码,我认为这是二进制"或非普通文本.

我想知道它们如何混合普通的shell脚本,然后可能将它们称为不可读"的东西,即二进制文件.

对此有何见解?

解决方案

基本上,它是一个Shell脚本,位于某种压缩存档中,例如tar存档.您可以在自己上使用tailsed命令(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆