shell编程连载【1】-创建归档文件

本文介绍了一个简单的Bash脚本,用于将多个文件打包成一个分发包,并详细解释了如何通过该脚本恢复原始文件的过程。文中还涉及了标准输出重定向到标准错误的概念。

(1) vi  bundle.sh

#! /bin/bash #bundle :group files into distribution package echo "# To unbundle, bash this file" for i do echo "echo $i 1>&2" echo "cat >$i <<'End of $i'" cat $i echo "End of $i" done


(2)

cat h1.txt

hello1

hello2

cat h2.txt hello3

 

(3) 执行sh boudle.sh h1.txt h2.txt >bakfiles

(4)

rm h1.txt h2.txt


(5) 执行 bash bakfiles可恢复h1.txt,h2.txt

 

 【解析】

(1)为什么执行bash bakfiles可恢复h1.txt,h2.txt?

      先来看一下bakfiles的内容,执行cat bakfiles

# To unbundle, bash this file
echo h1.txt 1>&2
cat >h1.txt <<'End of h1.txt'
hello1
hello2

End of h1.txt
echo h2.txt 1>&2
cat >h2.txt <<'End of h2.txt'
hello3
End of h2.txt
原来在执行sh boudle.sh h1.txt h2.txt >bakfiles后,bakfiles中生成了2条文件重定向语句:

cat >h1.txt <<'End of h1.txt'
cat >h2.txt <<'End of h2.txt'
再用bash解析bakfiles也就达到了恢复文件的目的。

(2) 1>&2的涵义

0:标准输入

1:标准输出

2:标准错误

>&:可复制的文件描述符

1>&2即是将标准输出重定向为标准错误。

 

 

转载于:https://www.cnblogs.com/J2EEPLUS/archive/2011/09/27/2487998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值