LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 02-12-2004, 11:21 AM   #1
michael_util
Member
 
Registered: Feb 2004
Posts: 47

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


Hello,

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

#!/bin/bash

declare -a masnew

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

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 !!!!

Michael.
 
Old 02-13-2004, 06:51 AM   #2
notAslacker
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:

Quote:
#!/bin/bash

declare -a lineArray

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

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

for ((s=0;s<i;s++)); do
echo ${lineArray[$s]}
done
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:

#!/bin/bash
perl parseStuff.pl

exit 0
-notaslacker

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 02:56 PM.
 
  


Reply



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-19-2011 11:01 PM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 05:45 PM
Bash script text line into an array toolshed Programming 1 06-13-2005 05:49 PM
MAJOR problem .. bash scripts ... HELP !!! michael_util Linux - General 1 02-12-2004 12:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 06:05 AM.

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