初衷
windows下有自解壓檔案,直接雙擊就能釋放檔案,并且還能執行釋放檔案前后要執行的腳本,Linux下我也想要這樣的功能,因為我希望直接拷貝一個shell腳本給別人,別人直接運行就能用,而不是一個壓縮檔案,需要別人解壓,
實作
#!/bin/bash
# write header
cat > attachment.sh <<EOF
#!/bin/bash
base64 -d > attachment_test <<EOF
EOF
# write attachment base64 data
(cat <<EOF
#include<stdio.h>
int main(int argc,char *argv[])
{
int i = 0;
for (;i<argc;i++) {
printf("%d:%s\n",i,argv[i]);
}
return 0;
}
EOF
) | gcc -s -o /dev/fd/1 -xc - | base64 >> attachment.sh
# write tail
cat >> attachment.sh <<END
EOF
chmod +x attachment_test
./attachment_test 1 2 3 4 5
END
# exec
bash attachment.sh
下面是結果
result:
# ./test.sh
0:./attachment_test
1:1
2:2
3:3
4:4
5:5
執行上述shell腳本會產生如下2個檔案,一個是自解壓shell腳本,一個是釋放的可執行程式,
attachment.sh attachment_test
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/95228.html
標籤:其他
上一篇:RS422介面與RS485介面
下一篇:性能優化 = 改改代碼?
