問題描述
首先,我已經在 Google 上搜索過,但只找到了將壓縮文件(例如 .tar.gz
)嵌入到 shell 腳本中的示例.
First, I already googled but only found examples where a compressed file (say a .tar.gz
) is embedded into a shell script.
基本上,如果我有一個打印字符串的 C 程序 (hello.c
),比如 Hello World!.
Basically if I have a C program (hello.c
) that prints a string, say Hello World!.
我編譯它以獲得可執行的二進制文件
I compile it to get an executable binary
gcc hello.c -o hello
現在我有一個 shell 腳本 testEmbed.sh
Now I have a shell script testEmbed.sh
我要問的是是否可以將二進制文件 (hello) 嵌入到 shell 腳本中,以便在我運行時
What I am asking is if it is possible to embed the binary (hello) inside the shell script so that when I run
./testEmbed.sh
它執行二進制文件以打印 Hello World!.
it executes the binary to print Hello World!.
說明:一種替代方法是我將可執行文件壓縮到存檔中,然后在腳本運行時將其解壓縮.我要問的是是否可以在沒有它的情況下運行該程序.
Clarification: One alternative is that I compress the executable into an archive and then extract it when the script runs. What I am asking is if it is possible to run the program without that.
到目前為止,我一直在嘗試這里的方法.但這對我不起作用.我猜作者是在另一個架構上使用其他發行版.所以,基本上這對我不起作用.:P
Up until now, I was trying the method here. But it does not work for me. I guess the author was using some other distribution on another architecture. So, basically this did not work for me. :P
另外,如果 C 程序的工作流程與 Java jar
不同,我也想知道!
Also, if the workflow for a C program differs from a Java jar
, I would like to know that too!
推薦答案
是的,可以這樣做.它實際上與您鏈接的文章在概念上非常相似.訣竅是使用 uuencode
將二進制文件編碼為文本格式,然后將其附加到腳本的末尾.
Yes, this can be done. It's actually quite similar in concept to your linked article. The trick is to use uuencode
to encode the binary into text format then tack it on to the end of your script.
然后您的腳本以這樣的方式編寫,它在自身上運行 uudecode
以創建二進制文件,更改權限然后執行它.
Your script is then written in such a way that it runs uudecode
on itself to create a binary file, change the permissions then execute it.
uuencode
和 uudecode
最初是為了將二進制內容轉移到互聯網的前身而創建的,互聯網不能很好地處理二進制信息.轉換為文本意味著它也可以作為 shell 腳本提供.如果,由于某種原因,當您嘗試運行 uuencode
時您的發行版報錯,這可能意味著您必須安裝它.例如,在 Debian Squeeze 上:
uuencode
and uudecode
were originally created for shifting binary content around on the precursor to the internet, which didn't handles binary information that well. The conversion into text means that it can be shipped as a shell script as well. If, for some reason your distribution complains when you try to run uuencode
, it probably means you have to install it. For example, on Debian Squeeze:
sudo aptitude install sharutils
將為您獲取相關的可執行文件.這是我經歷的過程.首先創建并編譯你的 C 程序 hello.c
:
will get the relevant executables for you. Here's the process I went through. First create and compile your C program hello.c
:
pax> cat hello.c
#include <stdio.h>
int main (void) {
printf ("Hello
");
return 0;
}
pax> gcc -o hello hello.c
然后創建一個shell腳本testEmbed.sh
,它會自己解碼:
Then create a shell script testEmbed.sh
, which will decode itself:
pax> cat testEmbed.sh
#!/bin/bash
rm -f hello
uudecode $0
./hello
rm -f hello
exit
第一個 rm
語句表明 hello
可執行文件是由該腳本重新創建的,而不是在您的編譯過程中徘徊.由于您還需要文件中的有效負載,請將編碼的可執行文件附加到文件的末尾:
The first rm
statement demonstrates that the hello
executable is being created anew by this script, not left hanging around from your compilation. Since you need the payload in the file as well, attach the encoded executable to the end of it:
pax> uuencode hello hello >>testEmbed.sh
之后,當您執行腳本 testEmbed.sh
時,它會提取可執行文件并運行它.
Afterwards, when you execute the script testEmbed.sh
, it extracts the executable and runs it.
之所以如此,是因為 uudecode
在其輸入(begin
和 end
)中查找由 uuencode
,所以它只嘗試解碼編碼的程序,而不是整個腳本:
The reason this works is because uudecode
looks for certain marker lines in its input (begin
and end
) which are put there by uuencode
, so it only tries to decode the encoded program, not the entire script:
pax> cat testEmbed.sh
#!/bin/bash
rm -f hello
uudecode $0
./hello
rm -f hello
exit
begin 755 hello
M?T5,1@$!`0````````````(``P`!````$(,$"#0```#`!@```````#0`(``'
M`"@`'@`;``8````T````-(`$"#2`!`C@````X`````4````$`````P```!0!
: : :
M:&%N9&QE`%]?1%1/4E]%3D1?7P!?7VQI8F-?8W-U7VEN:70`7U]B<W-?<W1A
M<G0`7V5N9`!P=71S0$!'3$E"0UR+C``7V5D871A`%]?:38X-BYG971?<&-?
4=&AU;FLN8G@`;6%I;@!?:6YI=```
`
end
您可能還應該擔心其他一些事情,例如您的程序可能需要目標系統上不存在的共享庫,但上述過程基本上就是您所需要的.
There are other things you should probably worry about, such as the possibility that your program may require shared libraries that don't exist on the target system, but the process above is basically what you need.
JAR 文件的過程非常相似,只是運行它的方式不同.它仍然是一個文件,但您需要替換該行:
The process for a JAR file is very similar, except that the way you run it is different. It's still a single file but you need to replace the line:
./hello
帶有能夠運行 JAR 文件的東西,例如:
with something capable of running JAR files, such as:
java -jar hello.jar
這篇關于在 shell 腳本中嵌入可執行二進制文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!