linux下一个短小的自解压shell脚本 » 荒野无灯weblog

Keep It Simple, Stupid.

荒野无灯weblog

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

偶然在网上看到的:

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

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

test for fun:

[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

可以看到解压成功了。

Tagged in :

All Comments (0)
Gravatar image
No Comments