BASH没有遍历数组

时间:2014-09-03 08:08:50

标签: arrays bash

我有一个愚蠢的问题。我一直在尝试创建一个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

2 个答案:

答案 0 :(得分:1)

演示您的问题和可能的解决方案

title() { printf "\n%-80.80s\n" "===[$*]$(printf "%80s" | tr ' ' '=')";}

title "demo output: lines what should go into array"
egrep 'root|daemon' /etc/passwd

title "your solution - puts all lines into arr[1] "
declare -a arr1=$(egrep 'root|daemon' /etc/passwd)
echo "array size: ${#arr1[@]}"
for ((i=0; i<${#arr1[@]}; i++))
do
    echo "$i: ${arr1[$i]}"
done

title  "alternative - creates 3 array elements - but keeps '\n'"
readarray arr2 < <(egrep 'root|daemon' /etc/passwd)
echo "array size: ${#arr2[@]}"
for ((i=0; i<${#arr2[@]}; i++))
do
    echo "$i: ${arr2[$i]}"
done

title "as list, breaks on IFS (default on spaces too) - more elements as lines"
arr3=($(egrep 'root|daemon' /etc/passwd))
echo "array size: ${#arr3[@]}"
for ((i=0; i<${#arr3[@]}; i++))
do
    echo "$i: ${arr3[$i]}"
done

title "as previous breaks on IFS, but only on '\n' "
IFS=$'\r\n' arr4=($(egrep 'root|daemon' /etc/passwd))
echo "array size: ${#arr4[@]}"
for ((i=0; i<${#arr4[@]}; i++))
do
    echo "$i: ${arr4[$i]}"
done
unset IFS

产生

===[demo output: lines what should go into array]===============================
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_cvmsroot:*:212:212:CVMS Root:/var/empty:/usr/bin/false

===[your solution - puts all lines into arr[1] ]================================
array size: 1
0: root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false
_cvmsroot:*:212:212:CVMS Root:/var/empty:/usr/bin/false

===[alternative - creates 3 array elements - but keeps '\n']====================
array size: 3
0: root:*:0:0:System Administrator:/var/root:/bin/sh

1: daemon:*:1:1:System Services:/var/root:/usr/bin/false

2: _cvmsroot:*:212:212:CVMS Root:/var/empty:/usr/bin/false


===[as list, breaks on IFS (default on spaces too) - more elements as lines]====
array size: 6
0: root:*:0:0:System
1: Administrator:/var/root:/bin/sh
2: daemon:*:1:1:System
3: Services:/var/root:/usr/bin/false
4: _cvmsroot:*:212:212:CVMS
5: Root:/var/empty:/usr/bin/false

===[as previous breaks on IFS, but only on '\n' ]===============================
array size: 3
0: root:*:0:0:System Administrator:/var/root:/bin/sh
1: daemon:*:1:1:System Services:/var/root:/usr/bin/false
2: _cvmsroot:*:212:212:CVMS Root:/var/empty:/usr/bin/false

答案 1 :(得分:0)

为什么要将tLen创建为数组?

declare -a创建一个数组,但是如果提供单个值,它将仅分配给它的第一个元素。要为列表分配列表,必须将列表括在括号中:

TIF_PATH=( $(egrep "INPUT_FILE_DOC_TIF" "${FILE_ARRAY[$@]}" | sed 's#INPUT_FILE_DOC_TIF=##g') )