一个shell的自解压程序

http://www.linuxsir.org/bbs/thread366696.html

原程序如下:

#!/bin/sh
( read l; read l; read l ;exec cat ) < "$0" | gunzip | tar xf - && ls -l
exit
其中:
( read l; read l; read l ;exec cat ) 读取文件的前三行并扔掉,从第四行开始cat输出。
程序将从第四行cat的输出交给gunzip解压,并将gunzip的解压结果继续交由tar解压,最后列解压后的文件。
 
现将需要的文件打包,然后将该打包文件插入到该shell文件后面(即从第四行后面为打包的文件内容)。
执行该shell程序达到自解压的目的。
两个sh文件,一个针对gzip的一个针对bzip2的。 程序如下:
self_extract_g.sh 文件:
#!/bin/bash
(read l; read l; read l; exec cat) < "$0" | tar -xzvf - && ls -l
exit 0;
self_extract_b.sh 文件:
#!/bin/bash
(read l; read l; read l; exec cat) < "$0" | tar -xjvf - && ls -l
exit 0;
做一次测试:
1、建立data文件夹并创建三个文件test1, test2, test3
2、将test1, test2, test3文件分别打包为,gzip和bzip2
3、将gizp包文件植入self_extract_g.sh, 将bzip2包文件植入self_extract_b.sh
4、最后执行两个自解压程序。
colinux@colinux:~/myshell/data$ ls
test1 test2 test3
colinux@colinux:~/myshell/data$ tar -czvf data.tar.gzip test*
test1
test2
test3
colinux@colinux:~/myshell/data$ tar -cjvf data.tar.bzip2 test*
test1
test2
test3
colinux@colinux:~/myshell/data$ ll
total 20
-rw-r--r-- 1 colinux colinux 178 2010-04-24 22:49 data.tar.bzip2
-rw-r--r-- 1 colinux colinux 177 2010-04-24 22:49 data.tar.gzip
-rw-r--r-- 1 colinux colinux 13 2010-04-24 21:26 test1
-rw-r--r-- 1 colinux colinux 11 2010-04-24 21:27 test2
-rw-r--r-- 1 colinux colinux 11 2010-04-24 21:26 test3
colinux@colinux:~/myshell/data$ cp ../self_extract_* ./
colinux@colinux:~/myshell/data$ ll
total 28
-rw-r--r-- 1 colinux colinux 178 2010-04-24 22:49 data.tar.bzip2
-rw-r--r-- 1 colinux colinux 177 2010-04-24 22:49 data.tar.gzip
-rw-r--r-- 1 colinux colinux 85 2010-04-24 22:50 self_extract_b.sh
-rw-r--r-- 1 colinux colinux 84 2010-04-24 22:50 self_extract_g.sh
-rw-r--r-- 1 colinux colinux 13 2010-04-24 21:26 test1
-rw-r--r-- 1 colinux colinux 11 2010-04-24 21:27 test2
-rw-r--r-- 1 colinux colinux 11 2010-04-24 21:26 test3
colinux@colinux:~/myshell/data$ chmod +x self_extract_*
colinux@colinux:~/myshell/data$ ll
total 28
-rw-r--r-- 1 colinux colinux 178 2010-04-24 22:49 data.tar.bzip2
-rw-r--r-- 1 colinux colinux 177 2010-04-24 22:49 data.tar.gzip
-rwxr-xr-x 1 colinux colinux 85 2010-04-24 22:50 self_extract_b.sh
-rwxr-xr-x 1 colinux colinux 84 2010-04-24 22:50 self_extract_g.sh
-rw-r--r-- 1 colinux colinux 13 2010-04-24 21:26 test1
-rw-r--r-- 1 colinux colinux 11 2010-04-24 21:27 test2
-rw-r--r-- 1 colinux colinux 11 2010-04-24 21:26 test3
colinux@colinux:~/myshell/data$ cat data.tar.bzip2 >> self_extract_b.sh
colinux@colinux:~/myshell/data$ cat data.tar.gzip >> self_extract_g.sh
colinux@colinux:~/myshell/data$ rm test*
colinux@colinux:~/myshell/data$ ll
total 16
-rw-r--r-- 1 colinux colinux 178 2010-04-24 22:49 data.tar.bzip2
-rw-r--r-- 1 colinux colinux 177 2010-04-24 22:49 data.tar.gzip
-rwxr-xr-x 1 colinux colinux 263 2010-04-24 22:51 self_extract_b.sh
-rwxr-xr-x 1 colinux colinux 261 2010-04-24 22:51 self_extract_g.sh
colinux@colinux:~/myshell/data$ ./self_extract_b.sh
test1
test2
test3
colinux@colinux:~/myshell/data$ ll
total 28
-rw-r--r-- 1 colinux colinux 178 2010-04-24 22:49 data.tar.bzip2
-rw-r--r-- 1 colinux colinux 177 2010-04-24 22:49 data.tar.gzip
-rwxr-xr-x 1 colinux colinux 263 2010-04-24 22:51 self_extract_b.sh
-rwxr-xr-x 1 colinux colinux 261 2010-04-24 22:51 self_extract_g.sh
-rw-r--r-- 1 colinux colinux 13 2010-04-24 21:26 test1
-rw-r--r-- 1 colinux colinux 11 2010-04-24 21:27 test2
-rw-r--r-- 1 colinux colinux 11 2010-04-24 21:26 test3

猜你喜欢

转载自folksy.iteye.com/blog/1004586
今日推荐