包含二进制可执行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屋!