我有一个名为bundle的脚本,它使用Here-documents将文本文件合并到一个文件中。这些文件的名称在调用bundle脚本时作为参数传递,放在一个单独的文件(这里称为filebundle)中,这个文件然后可以作为bash脚本执行,以便将这些文件分解成单独的文件。Bash:搜索一个命令块的脚本,然后执行这些命令
这里是束脚本:
#! /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
此,当执行如下
$ bash bundle file1.txt file2.txt > filebundle
结果在下面的文件,命名为filebundle:
# To unbundle, bash this file.
echo file1.txt 1>&2
cat >file1.txt <<'End of file1.txt'
This is file1.
End of file1.txt
echo file2.txt 1>&2
cat >file2.txt <<'End of file2.txt'
This is file2.
End of file2.txt
,正如我说,可以bashed解包file1.txt和file2.txt
我的问题如下:我必须重新编写捆绑脚本,以便可以使用或不使用文件名作为参数来执行由它产生的filebundle文件,并且可以相应地解包其中包含的文件。
例如:
$ bash filebundle file2.txt
将分拆仅FILE2.TXT而不是FILE1.TXT。另外,没有参数的bashing filebundle会解包filebundle中的所有文件。
我想我应该用“如果......那么......否则”控制结构根据传递的参数解包文件,而我只能想到用一些像
for i in [email protected]; do
grep "$i" <<'End of $i' | bash
done
找到filebundle内的特定文件并解压缩它们。然而,我似乎无法将它们放在一起工作。
您的想法和建议非常感谢。
这工作完美。谢谢。 – jvasilakes