日期:2014-05-16  浏览次数:20675 次

关于shell脚本的tar命令“-”,表示神马意思。。。。呼叫大神驾到
  1 #! /bin/bash
  2
  3 A="/root/tmp/test2"
  4 B="/root/tmp/test4"
  5
  6 cd $A
  7 tar cvf - . | tar xvf - -C $B
这个脚本实现的把A目录下的所有内容拷贝到B的目录下。
但是,我的问题是:
tar cvf - . 或者tar xvf - -C $B中的“-”表示神马意思?
我不要“-”的话,也就是tar cvf . | tar xvf -C $B会报错的。
嗯~~我自己怀疑是临时文件,,,但是找不到根据,,,
求大神驾到啊~~~~
------解决方案--------------------
##参考来源:
1. The dash "-" represents standard input. dash表示标准输入设备.
http://www.abbeyworkshop.com/howto/unix/nix_gtar/index.html
2.
If the  name of the file is - (dash), tar writes to standard output or reads
      from standard input, whichever is appropriate.  Thus, tar can be used
      as the head or tail of a filter chain.  The tar utility can also be
      used to move hierarchies with the command:
   cd fromdir; tar cf - . 
------解决方案--------------------
 (cd todir; tar xpf -)
http://h30097.www3.hp.com/docs/base_doc/DOCUMENTATION/V51B_HTML/MAN/MAN1/0153____.HTM

##理解:
1. tar cvf - . 表示把当前文件夹(.)下的内创建(c)一个包,包名(f)为标准输入设备
2. 
------解决方案--------------------
 管道只能从标准输入设备中输入数据(所以上面要创建文件到标准输入设备)[注1]
3. tar xvf -C $B 再转到(C) $B 目录,从标准输入设备(-)中解包(x)出(原来的)文件.

##注:
1. 看看UNIX痛恨者手册会了解Linux的一些限制和不爽的地方.