无法复制Array:Shell Script中的文件

时间:2014-05-06 09:21:45

标签: arrays shell

#!/bin/sh

i=0


while read line
do 

    for WORD in $line
    do
        #copy to array
    array[$i]=$WORD
    i=$((i+1))
    done

done < ACTION_TAG.txt

文件以名称.sh

保存

我正在尝试复制数组中的文件。它会抛出错误。

read.sh: 44: array[0]=sg: not found
read.sh: 44: array[1]=sg: not found
read.sh: 44: array[2]=sg: not found
read.sh: 44: array[3]=sg: not found
read.sh: 44: array[4]=sg: not found
read.sh: 44: array[5]=sg: not found
read.sh: 44: array[6]=sg: not found
read.sh: 44: array[7]=sg: not found
read.sh: 44: array[8]=sg: not found
read.sh: 44: array[9]=sg: not found
read.sh: 44: array[10]=sg: not found
read.sh: 44: array[11]=sg: not found
read.sh: 44: array[12]=sg: not found
read.sh: 44: array[13]=sg: not found
read.sh: 44: array[14]=sg: not found
read.sh: 44: array[15]=sg: not found

我经历了在stackoverflow上发布的相同查询,但无法解决问题。可以帮助一些人。

1 个答案:

答案 0 :(得分:1)

你在这里使用错误的shebang #!/bin/sh如果你想使用BASH数组,那么使用:

#!/bin/bash

另外,为了在数组中存储各种单词,您可以使用这个简化的脚本:

#!/bin/bash

array=()
while read line
do 
    array+=( $line )
done < ACTION_TAG.txt