LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 3709|回复: 7

有什么好办法在解压缩过程中同时获取压缩包的内容目录结构的顶层目录呢?[已解决]

[复制链接]
发表于 2004-4-2 20:03:40 | 显示全部楼层 |阅读模式
要求:
1.不能保存解压缩输出为临时文件再对该文件进行分析,要求获取是与解压缩同时进行的
2.要能在屏幕上显示解压缩的输出
发表于 2004-4-2 20:30:10 | 显示全部楼层
汗~~ ;)还是举个例子吧~,不同的解压缩命令有不同的使用方法!
 楼主| 发表于 2004-4-2 20:40:03 | 显示全部楼层
限定为bz2。
发表于 2004-4-3 00:53:59 | 显示全部楼层
/home/javalee:bzip2 --help
bzip2, a block-sorting file compressor.  Version 1.0.2, 30-Dec-2001.

   usage: bzip2 [flags and input files in any order]

   -h --help           print this message
   -d --decompress     force decompression
   -z --compress       force compression
   -k --keep           keep (don't delete) input files
   -f --force          overwrite existing output files
   -t --test           test compressed file integrity
   -c --stdout         output to standard out
   -q --quiet          suppress noncritical error messages
   -v --verbose        be verbose (a 2nd -v gives more)
   -L --license        display software version & license
   -V --version        display software version & license
   -s --small          use less memory (at most 2500k)
   -1 .. -9            set block size to 100k .. 900k
   --fast              alias for -1
   --best              alias for -9

   If invoked as `bzip2', default action is to compress.
              as `bunzip2',  default action is to decompress.
              as `bzcat', default action is to decompress to stdout.

   If no file names are given, bzip2 compresses or decompresses
   from standard input to standard output.  You can combine
   short flags, so `-v -4' means the same as -v4 or -4v, &c.
bzip2 -s -L -v file#compress file
bzip2 -d -v file       #decompress file
试了半天,我自己都不知道在干什么了~~~
 楼主| 发表于 2004-4-3 08:56:30 | 显示全部楼层
呵呵,劳烦javalee老兄了,不好意思。
我想到办法了。

  1. [root@home tmp]# cat gawkscript
  2. [color=blue]
  3. BEGIN {
  4.         j=2;
  5. }
  6. {
  7. if(pool[1]!=$1) {
  8.         pool[j++]=$1;
  9.         pool[1]=$1;
  10. }
  11. print $0;
  12. }
  13. END {
  14.         print "\nArchive directory structure:";
  15.         for(i=2;i<=j;i++) print pool[i];
  16. }
  17. [/color]
  18. [root@home tmp]# tar jcf mplayer.tar.bz2 MPlayer-1.0pre3/ mplayerplug-in/
  19. [root@home tmp]# [color=blue]bunzip2 -dc mplayer.tar.bz2 |tar tf - | gawk -F / -f gawkscript[/color]
  20. ...
  21. Archive directory structure:
  22. MPlayer-1.0pre3
  23. mplayerplug-in
复制代码

1. 在gawk脚本定义一个数组,利用C语言的"监测哨"技术(第一个数组元素保存上次有效值用以对比),滤出冗余的第一域数据;  
2. 用print $0输出的同时进行第一域处理。这样就实现了第二个要求。
3. 注意,这里我使用了tar tf - ,这是tar的列表选项,其实解压缩选项tar xf - 是一样的。

gawk十分强大,它和(GNU)sed是bash的左右手。善用gawk&(GNU)sed。:cool:
发表于 2004-4-3 10:19:58 | 显示全部楼层
tar 的 -v 选项可以显示目录结构, 不知是否是你所需
发表于 2004-4-3 11:32:11 | 显示全部楼层
最初由 home_king 发表
呵呵,劳烦javalee老兄了,不好意思。
我想到办法了。

  1. [root@home tmp]# cat gawkscript
  2. [color=blue]
  3. BEGIN {
  4.         j=2;
  5. }
  6. {
  7. if(pool[1]!=$1) {
  8.         pool[j++]=$1;
  9.         pool[1]=$1;
  10. }
  11. print $0;
  12. }
  13. END {
  14.         print "\nArchive directory structure:";
  15.         for(i=2;i<=j;i++) print pool[i];
  16. }
  17. [/color]
  18. [root@home tmp]# tar jcf mplayer.tar.bz2 MPlayer-1.0pre3/ mplayerplug-in/
  19. [root@home tmp]# [color=blue]bunzip2 -dc mplayer.tar.bz2 |tar tf - | gawk -F / -f gawkscript[/color]
  20. ...
  21. Archive directory structure:
  22. MPlayer-1.0pre3
  23. mplayerplug-in
复制代码

1. 在gawk脚本定义一个数组,利用C语言的"监测哨"技术(第一个数组元素保存上次有效值用以对比),滤出冗余的第一域数据;  
2. 用print $0输出的同时进行第一域处理。这样就实现了第二个要求。
3. 注意,这里我使用了tar tf - ,这是tar的列表选项,其实解压缩选项tar xf - 是一样的。

gawk十分强大,它和(GNU)sed是bash的左右手。善用gawk&(GNU)sed。:cool:

精彩~~,:thank
 楼主| 发表于 2004-4-3 13:23:35 | 显示全部楼层
之所以有冲动感欲编写这类shell代码,并不是无中生有的。
原因有二
很多源码包的内部目录结构与其名字是不相符的。如filename.tar.bz2被解压缩出来后,生成的目录名不一定就是"filename"或是"filename"的模式,甚至不止一个目录。这样如果我们在一个有众多文件的目录下解开这类源码包的时候,就会不知所措。此其一
为了提高"时空效率",不应该把冗长的输出保存下来,解决空间问题;不应该解压缩过后才进行分析,解决时间问题。或许有人说,一个源码包的时空消耗是微不足道的。没错,一个源码包可能如此,但如果上百个源码包呢(把楼上的脚本嵌套进循环结构)?或许有人说,我可以在屏幕输出里记住有多少顶层目录啊——那如果一个源码包包含了上百个或更多目录呢(如系统备份)?此其二

其实shell用途广泛,变化多端。
Bash是Linux的半壁江山,而gawk&(GNU)sed就是Bash的左右手。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表