我有一个要在主文档上运行的脚本。该脚本将查看带有完整文件名和路径并用双引号引起来的txt文档。然后它将使用文件名和父文件夹(因为完整的文件夹结构将不匹配)并搜索主文档。然后将结果输出到另一个文本文档中,这是我到目前为止的工作,如果将带引号的文件直接放入字符串中,则可以正常工作,但是当我尝试从包含带引号的文件的文件中读取它时,它就会损坏
要从中读取的示例源文本文件:
"This-is/An-Example-pFa/ati4_review_2014033434314-15532223545/article19_20140314-155812_REVIEW.JPG"
"This-is/An-Example-pFa/ati4_review_2014033434314-15532223545/article19_20140314-155812_REVIEW.JPG"
"This-is/An-Example-pFa/ati4_review_2014033434314-15532223545/article19_20140314-155812_REVIEW.JPG"
#!/bin/bash
AWS="/home/user/Documents/awslist.csv" #variable for master document
LSFLDR=$(while read k; do echo "$k"; #variable to read source text file
done < /home/user/Documents/FP.txt)
strings=(
$LSFLDR
)
for i in "${strings[@]}";
do (
VAR="$i" #variable for line in source text file
DIR="$(basename "$(dirname "$VAR")")" #variable to locate parent folder
FIL=$(basename "${VAR}") #variable to locate file name
CMP="${DIR}"\/"${FIL}" #variable to search master document
if grep -q "$CMP" $AWS; #searching master document for parent folder and file name
then
echo $i found >> /home/user/Documents/Scanresults.txt
else
echo $i not found >> /home/user/Documents/Scanresults.txt
fi
)
done
答案 0 :(得分:1)
您可能想读How can I read a file (data stream, variable) line-by-line (and/or field-by-field)? -填充strings
数组的方式令人震惊。
如果要用文件的行填充数组:
mapfile -t strings < filename
如果您只是想对文件的行进行迭代(不必将整个文件读入内存):
while IFS= read -r line; do
do_something_with "$line"
done < filename
答案 1 :(得分:0)
谢谢格伦!是的,我确定我的剧本让你畏缩了。映射文件正是我要使其正常工作所需要的!