Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Slackware This Forum is for the discussion of Slackware Linux.


  Search this Thread
Old 02-12-2004, 12:21 PM   #1
Registered: Feb 2004
Posts: 47

Rep: Reputation: 15
Unhappy MAJOR problem ... bash script array HELP !!!!!


I am writing a bash script to do a compare between 8 files and fine the common lowest multipler.

I can not use diff because of the file format and how it changes.

So I started off with an array loop


declare -a masnew

cat file1 | while read line
let "index = $index + 1"
echo ${masnew[6]} ### used for test

echo ${masnew[6]} ### used for test

Ok ... now inside the loop the echo statement prints the 6th element of the array repeatedly as it should. Out side the loop it print " ". That is right a null value. The array is completely empty out side the loop ?

I having tried this on slackware 9.1, 9.0 and 8.1 with all the same results. I have tried different shells. I have even compiled bash from source.

I have not get this to work, I this for a project I am doing so it is important.

Any suggestions would help !!!!

Old 02-13-2004, 07:51 AM   #2
LQ Newbie
Registered: Jul 2003
Posts: 16

Rep: Reputation: 0
bash script...

Hey, I saw your question and came up with this jibberish, in attempt to learn a bit of bash script:


declare -a lineArray

if [ -e "$1" ]; then
FILE=`cat -E $1`
FILE=`cat -E file1`

for line in $FILE; do
lineArray[$i]="${lineArray[$i]} ${line%*\$}"
if [ "$line" != "${line%*\$}" ]; then
let "i++"

for ((s=0;s<i;s++)); do
echo ${lineArray[$s]}
The problem with your script may be 1)directly flipping the cat into loop's test w/out buffer 2)the incrementation of i in your while loop(could be a different num of elements than expected: with 5 line file 12 i's or so), or 3) bash is borked, inherently, when it comes to parsing within files--probably not 2, maybe a bit of both 3 and 1.

That stuff I wrote above seems to function to an extent, but when using 'larger' files like XF86Config it outputs some shady material. With a larger file(large maybe being line length instead of #of lines causing the problem in the string parts) there's an extra line or two about the files in my current directory; plus there's that confusing for/while read/line scope wierdness

Maybe a better solution is:


exit 0

P.S. Could someone who understands bash scripting well test the above script on an xf86config or something and see if they get the same 'extra' lines? I'm curious about the possibility that it's a bug or goof on my system, rather than one of my average logic oversights. Removed comments to make it more readable.

Last edited by notAslacker; 02-17-2004 at 03:56 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script split array robertngo Programming 13 06-20-2011 12:01 AM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 05:37 AM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 06:45 PM
Bash script text line into an array toolshed Programming 1 06-13-2005 06:49 PM
MAJOR problem .. bash scripts ... HELP !!! michael_util Linux - General 1 02-12-2004 01:08 PM

All times are GMT -5. The time now is 07:59 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration