包含二进制可执行文件的 Bash 脚本 [英] Bash script containing binary executable
问题描述
是否可以编写一个bash脚本,其中包含一个二进制可执行程序?
Is it possible to write a bash script, which would contain a binary executable program inside?
我的意思是一个脚本,它包含一个文本形式的可执行文件的转储,当它被执行时,它会被这个脚本转储回一个可执行文件?
I mean a script, which would contain a dump of an executable in a textual form, which will be dumped back to an executable by this script when it is executed?
我很想知道一个解决方案,它可以开箱即用,无需安装额外的软件包.有可能吗?
I would love to know a solution, which will work out of the box without a need of installing additional packages. Is it possible?
谢谢!
推荐答案
我以前从来没有做过这样的事情 ;)这将编译一些 c 源代码,创建一个包含二进制文件的 b.bash
脚本(以及用于简单开发的原始脚本)
i never done something like this before ;)
this will compile some c source, create a b.bash
script containing the binary (and the original script for simple development)
(a.bash)
#!/bin/bash
if [ "$0" == "b.bash" ];then
tail -n +$[ `grep -n '^BINARY' $0|cut -d ':' -f 1` + 1 ] $0 | base64 -d > a2.out
chmod +x a2.out
./a2.out
echo $?
exit
fi
cat "$0" > b.bash
echo "BINARY" >> b.bash
cat > a.c << EOF
int main(){
return 12;
}
EOF
gcc a.c
base64 a.out >> b.bash
调用(a.bash 生成 b.bash):
invoke with (a.bash generates b.bash):
bash a.bash ;bash b.bash
我不知道如何避免在执行前将二进制文件写入临时文件...
i don't know how to evade writing out the binary into a temporary file before execution...
这篇关于包含二进制可执行文件的 Bash 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!