0

我正在努力完成这项任务:

编写一个脚本,将目录(路径)名称和文件名库(例如“.”、“*.txt”等)作为输入。该脚本将搜索给定的目录树,找到与给定文件名匹配的所有文件,并将它们捆绑到一个文件中。将给定文件作为脚本执行应返回原始文件。

谁能帮我?

首先,我尝试像这样进行查找部分:

#!/bin/bash

filebase=$2
path=$1

find $path \( -name $base \)

然后我找到了这个捆绑代码,但我不知道如何组合它们。

for i in $@; do
 echo "echo unpacking file $i"
 echo "cat > $i <<EOF"
 cat $i
 echo "EOF"
 done
4

1 回答 1

1

Going on tripleee's comment you can use shar to generate a self extracting archive. You can take the output of find and pass it through to shar in order to generate the archive.

#!/bin/bash

path="$1"
filebase="$2"
archive="$3"

find "$path" -type f -name "$filebase" | xargs shar > "$archive"

The -type f option passed to find will restrict the search to files (i.e. excludes directories), which seems to be a required limitation.

If the above script is called archive_script.sh, and is executable, then you can call it as below for example:

./archive_script.sh /etc '*.txt' etc-text.shar

This will create a shar archive of all the .txt files in /etc.

于 2012-10-31T14:47:39.623 回答