• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Need easy help with programming

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

96redformula

Member
Joined
Aug 10, 2004
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
 
Last edited:
Can you explain to us where it's going wrong? Also, with console based programs, I like to add in some echos, for debugging purposes. Start having it list out some of the details as you go along, so when something odd comes up, you can start investigating back from there.
 
Back