0
#!/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

File is saved with name .sh

I am trying to copy a file in array. it throws below error.

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

I went through the same queries posted on stackoverflow but was unable to solve problem. can some one help.

9
  • I have just tested this code with some sample text and it worked as expected, could you provide some sample text from the file that causes the error? Commented May 6, 2014 at 9:27
  • what do you mean by sample text Commented May 6, 2014 at 9:40
  • HI, Can you please let me know if u modified anything Commented May 6, 2014 at 9:43
  • my code was exactly the same as yours, the only things I can think that might be different is your input file or that I use BASH as my shell(most people prefer zsh or ksh because they're faster). Commented May 6, 2014 at 9:46
  • there is some problem.. i am not able to deal with arrays @ all. Event the example program are not running from websites Commented May 6, 2014 at 9:49

1 Answer 1

1

You are using wrong shebang here #!/bin/sh If you want to use BASH array then use:

#!/bin/bash

Also to store various words in array you can use this much simplified script:

#!/bin/bash

array=()
while read line
do 
    array+=( $line )
done < ACTION_TAG.txt
Sign up to request clarification or add additional context in comments.

2 Comments

YOUR CODE GIVES ERROR read.sh: 28: Syntax error: "(" unexpected
You need to make sure to run this script as bash ./script.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.