linux下一个短小的自解压shell脚本

偶然在网上看到的:

1
2
3
#!/bin/sh
( read l; read l; read l ;exec cat ) < "$0" | gunzip | tar xf - &#038;&#038; ls -l
exit

三个read 1,读取自身的前三行,随后一个cat.即将该脚本的前三行不做处理。从第四行开始,送给gunzip,再送给tar解压,最后显示解压的结果。

test for fun:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[admin@huangye scripts]$ mkdir test
[admin@huangye scripts]$ cp *.sh test
[admin@huangye scripts]$ tar zcvf test.tar.gz test
test/
test/vsftpd2.sh
test/cairo-dock.sh
test/self-extract.sh
test/info.sh
test/conky.sh
[admin@huangye scripts]$ cd test
[admin@huangye test]$ mv ../test.tar.gz .
[admin@huangye test]$ ls
cairo-dock.sh  conky.sh  info.sh  self-extract.sh  test.tar.gz  vsftpd2.sh
[admin@huangye test]$ cat test.tar.gz >> self-extract.sh
[admin@huangye test]$ ls
cairo-dock.sh  conky.sh  info.sh  self-extract.sh  test.tar.gz  vsftpd2.sh
[admin@huangye test]$ mkdir sub
[admin@huangye test]$ mv self-extract.sh sub/
[admin@huangye test]$ ls
cairo-dock.sh  conky.sh  info.sh  sub/  test.tar.gz  vsftpd2.sh
[admin@huangye test]$ cd sub/
[admin@huangye sub]$ ls
self-extract.sh
[admin@huangye sub]$ chmod +x self-extract.sh
[admin@huangye sub]$ ./self-extract.sh
total 8
-rwxr-xr-x 1 admin admin  651 Oct  6 02:49 self-extract.sh
drwxr-xr-x 2 admin admin 4096 Oct  6 02:47 test
[admin@huangye sub]$ ls -s
total 8
4 self-extract.sh  4 test/
[admin@huangye sub]$ ls test/
cairo-dock.sh  conky.sh  info.sh  self-extract.sh  vsftpd2.sh

可以看到解压成功了。

更多
One Response Post a comment
  1. 依云

    哇,这个办法又好又简单~

Leave a Reply

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS