Need easy help with programming

96redformula

Member
Dec 27, 2005
93
0
66
I am not asking to do my homework, but I need help. I have most of it done, I think... Here is what I am trying to do. I am trying to load a file into an array, so here is the code. What do I have wrong??

#!/bin/bash

filename=test

# cat test
#
# 1 2 3 4
# a b c d


declare -a array1

array1=( `cat "$filename"`) # Loads contents
# List file to stdout #+ of $filename into array1.


array1=( `cat "$filename" | tr '\n' ' '`)
# change linefeeds in file to spaces.
# Not necessary because Bash does word splitting,
#+ changing linefeeds to spaces.

echo ${array1[@]} # List the array.
# 1 2 3 4 a b c d
#

element_count=${#array1[*]}
echo $element_count # 8