【发布时间】:2014-09-03 17:21:02
【问题描述】:
我有一个愚蠢的问题。我一直在尝试创建一个 bash 脚本,用于根据自动生成的 .txt 文件中的路径检查 .tif 文件是否存在。然而,问题是我无法让我的代码回显我的 TIF_PATH 数组的所有内容,它只显示找到的第一个条目。
.txt 文件示例:
INPUT_FILE_DOC_TIF=/home/user/documents/tif.tif
知道我做错了什么吗?如你所见,我是个大菜鸟。
提前致谢。
count_recursive ()
{
command find "${1:-.}" -type f -name "${2:-*}" -print0 |
command tr -dc '\0' | command wc -c;
return 0
}
#FIND_TXT = Finds .txt files for the array.
FIND_TXT=$(find . -type f -name "*.txt")
用于创建数组,并查找自动生成的 .txt 文件。
#Save and change Internal Field Seperator (used to remove potential blank spaces, breaks and seperators)
OLDIFS=$IFS
IFS=$'\n'
#Read all file names into an array
declare -a FILE_ARRAY=($FIND_TXT)
#Restore it
IFS=$OLDIFS
#Get length of array
declare -a tLen=${#FILE_ARRAY[@]}
#Output path to a new variable, TIF_PATH, via egrep.
declare -a TIF_PATH=$(egrep "INPUT_FILE_DOC_TIF" "${FILE_ARRAY[$@]}" | sed 's#INPUT_FILE_DOC_TIF=##g')
#Output path without file name.
#declare -a PISS_PATH=$("${TIF_PATH}" | sed 's/..........$//')
#Use for loop to read all filenames
for (( i=0; i<${tLen}; i++ ));
do
echo "$i"
echo "${FILE_ARRAY[$i]}"
#Can be changed to output all, instead of increment.
echo "${TIF_PATH[$i]}"
#find . -type f -print | xargs grep ""
#echo "${PISS_PATH[$@]}"
#find . -type f -name "$TIF_PATH")
done
结果:
./txt/asd.txt
/home/user/Documents/Scripts/tiffile.tif
1
./txt/1000001.txt
2
./txt/111100001.txt
3
./txt/0047-001124-000000001.txt
4
./test/0047-001124-000000001.txt
5
./0047-001124-000000001.txt
【问题讨论】:
-
试试
tLen=${#FILE_ARRAY[@]} -
不相关,但是:尽量避免使用 UPPERCASE_ONLY 变量名。可能与环境变量发生冲突...
-
好吧,它不再是一个数组了。好的,我不知道大写变量 - 我会记住这一点!