LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2008, 08:23 AM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Rep: Reputation: 17
bash array question


Hi all,

I think this is the correct forum to post this question.

I have a file which is 2 or 4 or 6 lines long, which I would to read all of the contents into a bash shell array.

There are 9 pieces of data per line.

I've seen several examples on the internet however for some reason they don't work.

$ cat /var/tmp/inqraid.out
c6t50060E8010028101d119s2 CL1-B 77010255 169 - s/s/ss 0000 5:05-00 DF600F
c7t50060E8010028105d119s2 CL2-B 77010255 169 - s/s/ss 0000 5:05-00 DF600F

Could someone please write an example of how to read the file into a bash array and I can work from this.

Thanks
 
Old 11-20-2008, 08:42 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
By something like this?
Code:
#!/bin/bash
declare -a array
array=($(cat "testfile"))

for element in $(seq 0 $((${#array[@]} - 1)))
do
  echo -n "${array[$element]}"
  echo -n " SEP "
  ((count ++))
done

echo
echo "Total number of elements:          " ${#array[@]}
echo "Total number of elements (check):  " $count
 
Old 11-20-2008, 09:15 AM   #3
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
1. The only problem is that I don't have the seq command (Solaris 8 and 10).

2. By the following, echo -n " SEP ", do you mean seperator, which is white-space but not necessarily one space.
 
Old 11-20-2008, 09:35 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by dazdaz View Post
1. The only problem is that I don't have the seq command (Solaris 8 and 10).
In bash you can try the following brace expansion (it works since bash version 3, only):
Code:
for element in $(eval echo {0..$((${#array[@]} - 1))})
if this does not work, you can always use a while loop which will be executed until $element is equal to the length of the array:
Code:
#!/bin/bash
declare -a array
array=($(cat "testfile"))

element=0
while [ $element -le $((${#array[@]} - 1)) ]
do
  echo -n "${array[$element]}"
  echo -n " SEP "
  ((element++))
done
Quote:
Originally Posted by dazdaz View Post
2. By the following, echo -n " SEP ", do you mean seperator, which is white-space but not necessarily one space.
Yes, but all the code inside the for loop is just for demonstration. Just to echo the results. The actual assignment of the array is simply array=($(cat "testfile")).
 
Old 11-20-2008, 09:40 AM   #5
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
pwc101 is sure to complain about the useless use of cat in your example.

Code:
array=($(<new))
 
Old 11-20-2008, 09:47 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Oh, you're right! I always forget about this syntax. Hope pwc101 does not walk around!
 
Old 11-20-2008, 10:19 AM   #7
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
Thanks for all the posts. When I execute the script I receive the following error :

$ ./array.sh
c6t50060E8010028101d119s2 SEP ./array.sh: ((: element++: syntax error: operand expected (error token is "+")
c6t50060E8010028101d119s2 SEP ./array.sh: ((: element++: syntax error: operand expected (error token is "+")


#!/bin/bash

declare -a array
array=($(cat "/var/tmp/inqraid.out"))

element=0
while [ $element -le $((${#array[@]} - 1)) ]
do
echo -n "${array[$element]}"
echo -n " SEP "
((element++))
done

exit 0


$ bash --version
GNU bash, version 2.03.0(1)-release (sparc-sun-solaris)
Copyright 1998 Free Software Foundation, Inc.

I thought I had done a lot in shell, but shell arrays are all new to me.

Last edited by dazdaz; 11-20-2008 at 10:20 AM.
 
Old 11-20-2008, 10:33 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well, since you have bash version 2, use
Code:
element=$((element + 1))
instead of ((element++)). Also take a look at the chapter "Arrays" on the Advanced Bash Scripting Guide. A lot of examples there.
 
Old 11-21-2008, 11:40 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by dazdaz View Post
1. The only problem is that I don't have the seq command (Solaris 8 and 10).

2. By the following, echo -n " SEP ", do you mean seperator, which is white-space but not necessarily one space.
in Solaris, use nawk. it has much "neater" array syntax, as well as better array capabilities than bash.
 
  


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
array in bash ramesh_manu Linux - Newbie 1 02-24-2007 11:19 AM
BASH - Array Variables Micro420 Programming 5 12-15-2006 05:49 PM
Bash array question krock923 Programming 3 06-22-2006 11:39 AM
Array Help in BASH! ?*%$ johnnybhoy67 Linux - Software 2 02-22-2006 10:39 AM
array in bin/bash how to x2000koh Programming 12 05-09-2003 04:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:02 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