免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: huanghaojie
打印 上一主题 下一主题

shell 脚本上包含压缩文件如何做 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-08-07 13:00 |只看该作者
另外,指出一点,tar不是压缩文件,只是打包文件,一个1M的文件,打包后不会小于1M,哪怕这个文件有多少冗余的信息。要压缩用gz、bz2(这两个命令不能打包,只能压缩),或者直接用7z和zip打包+压缩。

论坛徽章:
0
12 [报告]
发表于 2008-08-07 13:37 |只看该作者
原帖由 waker 于 2008-8-7 12:55 发表
另外,比较一下
cat >test.sh  



呵呵,这个我还有疑问呢,以为你多写了个\呢

因为在我这里solaris8上
执行你那个脚本就报错:说没找到结束符\EOF

论坛徽章:
0
13 [报告]
发表于 2008-08-07 13:48 |只看该作者
原帖由 walkerxk 于 2008-8-7 12:48 发表

如果不做的话你要吧脚本里面相应的gunzip $$.tar.gz删掉,否则会出错,而且.tar.gz都要改成.tar


我觉得$$.tar.gz和后面的命令:
tar -czf - test 生成的内容没关系吧?

我这里显示$$.tar.gz是ppid.tar.gz
而不是test.tar.gz

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
14 [报告]
发表于 2008-08-07 13:57 |只看该作者

回复 #12 welcome008 的帖子

那就别用cat生成脚本
用vi生成

论坛徽章:
0
15 [报告]
发表于 2008-08-07 14:19 |只看该作者
原帖由 waker 于 2008-8-7 13:57 发表
那就别用cat生成脚本
用vi生成



能否解释下/的作用?

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
16 [报告]
发表于 2008-08-07 14:39 |只看该作者

回复 #15 welcome008 的帖子

Here Documents
       This type of redirection instructs the shell to read  input  from  the
       current  source  until  a  line containing only word (with no trailing
       blanks) is seen.  All of the lines read up to that point are then used
       as the standard input for a command.

       The format of here-documents is:

              <<[-]word
                      here-document
              delimiter

       No parameter expansion, command substitution, arithmetic expansion, or
       pathname expansion is performed on word.  If any  characters  in  word
       are  quoted, the delimiter is the result of quote removal on word, and
       the lines in the here-document are not expanded
.  If word is unquoted,
       all  lines  of the here-document are subjected to parameter expansion,
       command substitution, and arithmetic expansion.  In the  latter  case,
       the  character  sequence  \<newline> is ignored, and \ must be used to
       quote the characters \, $, and '.


这个问题很月经

论坛徽章:
0
17 [报告]
发表于 2008-08-07 15:13 |只看该作者
原帖由 welcome008 于 2008-8-7 13:48 发表


我觉得$$.tar.gz和后面的命令:
tar -czf - test 生成的内容没关系吧?

我这里显示$$.tar.gz是ppid.tar.gz
而不是test.tar.gz

没有任何地方提到过test.tar.gz
$$是你运行的进程的PID,比如1234,那么文件名就是1234.tar.gz,用tar zxf解开之后,里面有一个test.gz,其实waker用test.gz的意思是弄一个二进制文件,然后用这个二进制仍然可以解压来证明就是原来的文件。可能就是这个问题导致你弄不清楚压缩,打包,压缩的问题,但是我想用md5会更好一点。
tar -czf - test就是把上面的test.gz再加一层tar打包,然后再用gz压缩。然后附加到test.sh后面。
然后test.sh前面的脚本部分要解压这个1234.tar.gz,两个地方要对应,否则是会出错的。

论坛徽章:
0
18 [报告]
发表于 2008-08-07 15:33 |只看该作者
原帖由 waker 于 2008-8-7 14:39 发表
Here Documents
       This type of redirection instructs the shell to read  input  from  the
       current  source  until  a  line containing only word (with no trailing
       blanks) is see ...



谢谢,明白了加\是要禁止扩展

以前没注意这个问题,谢谢!

论坛徽章:
0
19 [报告]
发表于 2008-08-07 15:43 |只看该作者
原帖由 walkerxk 于 2008-8-7 15:13 发表

没有任何地方提到过test.tar.gz
$$是你运行的进程的PID,比如1234,那么文件名就是1234.tar.gz,用tar zxf解开之后,里面有一个test.gz,其实waker用test.gz的意思是弄一个二进制文件,然后用这个二进制仍然 ...



cat >test.sh <<\EOF
#! /bin/bash
sed '1,/^__ARCHIVE_FOLLOWS__/d' $0 >$$.tar.gz
tar tzvf $$.tar.gz
rm -f $$.tar.gz
exit
__ARCHIVE_FOLLOWS__
EOF
chmod +x test.sh
echo this is a test >test
tar -czf - test >>test.sh
./test.sh

我的理解是:
test.sh是一个脚本,前面是ascii嘛,后面一部分是二进制(由tar -czf - test >>test.sh得到)的
这个脚本的作用是把该脚本本身里的__ARCHIVE_FOLLOWS__删除,然后把删除后的剩余结果送入到文件ppid.tar.gz里
然后再展示ppid.tar.gz的内容
删除ppid.tar.gz
退出

没错吧?

论坛徽章:
0
20 [报告]
发表于 2008-08-07 16:01 |只看该作者

回复 #19 welcome008 的帖子

是删除第一行到__ARCHIVE_FOLLOWS__的内容,$$是pid,不是ppid,你试试echo $$。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP