我有一个换行符分隔的字符串列表。我需要遍历每一行,并使用用通配符括起来的参数。最终结果会将找到的文件附加到另一个文本文件中。以下是我到目前为止尝试过的一些方法:
cat < ${INPUT} | while read -r line; do find ${SEARCH_DIR} -name $(eval *"$line"*); done >> ${OUTPUT}
我已经尝试了许多 eval/$(( 等变体,但我还没有找到让两个星号都保留的方法。大多数情况下,我得到类似于*$itemFromList
的东西,但它缺少第二个星号,导致找不到文件。我认为这可能与 bash 扩展有关,但到目前为止我找到的资源没有任何运气。
基本上,需要为 -name
参数提供看起来像 *$itemFromList*
的东西,因为该文件在我正在搜索的值之前和之后都有单词。
有什么想法吗?
使用双引号防止星号被解释为对 shell 的指令而不是find
。
-name "*$line*"
因此:
while read -r line; do
line=${line%$'r'} # strip trailing CRs if input file is in DOS format
find "$SEARCH_DIR" -name "*$line*"
done <"$INPUT" >>"$OUTPUT"
。或者,更好:
#!/usr/bin/env bash
## use lower-case variable names
input=$1
output=$2
args=( -false ) # for our future find command line, start with -false
while read -r line; do
line=${line%$'r'} # strip trailing CR if present
[[ $line ]] || continue # skip empty lines
args+=( -o -name "*$line*" ) # add an OR clause matching if this line's substring exists
done <"$input"
# since our last command is find, use "exec" to let it replace the shell in memory
exec find "$SEARCH_DIR" '(' "${args[@]}" ')' -print >"$output"
注意:
- 指定
bash
的 shebang 可确保扩展语法(如数组(可用。 - 请参阅 BashFAQ #50 讨论为什么数组是用于收集命令行参数列表的正确结构。
- 有关环境和 shell 变量命名约定的相关 POSIX 规范,请参阅 http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html 的第四段:全大写名称用于对 shell 本身或 POSIX 指定的工具有意义的变量;小写名称保留供应用程序使用。你正在写的那个脚本?就规范而言,它是一个应用程序。