久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 shell 腳本中嵌入可執行二進制文件

Embed a Executable Binary in a shell script(在 shell 腳本中嵌入可執行二進制文件)
本文介紹了在 shell 腳本中嵌入可執行二進制文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

首先,我已經在 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.

uuencodeuudecode 最初是為了將二進制內容轉移到互聯網的前身而創建的,互聯網不能很好地處理二進制信息.轉換為文本意味著它也可以作為 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 在其輸入(beginend)中查找由 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产精品国产馆在线真实露脸 | 伊人网综合在线观看 | 狠狠色狠狠色综合系列 | 精品一二区| 97人人草| 国产精品日韩高清伦字幕搜索 | 国产精品美女久久久久久久久久久 | 成人3d动漫一区二区三区91 | 日日骚av | 色久五月| 欧美国产日韩在线 | 麻豆一区 | 免费视频二区 | 精品成人av| 亚洲av毛片 | 伊人久久精品 | 久久99国产精品 | 中文字幕一区二区三区四区五区 | 91九色在线观看 | 一区在线免费视频 | 麻豆av一区二区三区久久 | 国产乱xxav | 亚洲欧美日韩一区二区 | 国产精品久久久久久久久图文区 | 国产91综合一区在线观看 | 一级在线观看 | 九九热最新地址 | 久久久久一区二区 | 精品在线观看一区二区 | 欧美成人h版在线观看 | 日本黄色一级片视频 | 亚洲国产成人在线视频 | 国产视频一区二区 | 亚洲精品久久久久久久久久久 | 日韩免费一区二区 | 免费的av网站 | 欧美性生交大片免费 | 在线免费黄色 | 伊人激情综合网 | 成人二区三区 | 四季久久免费一区二区三区四区 |