LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
LinkBack Search this Thread
Old 04-13-2007, 02:19 PM   #1
TheMeeks
LQ Newbie
 
Registered: Apr 2007
Location: Waterloo
Distribution: Fedora
Posts: 3

Rep: Reputation: 0
Bash script, read file into array (Newbie)


Hey, i have read the similar postings and have had no luck thus far. I have also been reading through the site for quite some time, but still no luck.

I am trying to write a bash script that will read one file line by line, into an array that i will use later in the script. the file is all text, and has one ip address per line, no ending spaces, or semi-colons, just ips.

example
10.0.0.1
10.20.0.2
10.30.20.3

any help would be greatly appreciated.

Thanks, from a grateful
 
Old 04-13-2007, 02:53 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
How does your bash script look so far?
 
Old 04-13-2007, 03:28 PM   #3
TheMeeks
LQ Newbie
 
Registered: Apr 2007
Location: Waterloo
Distribution: Fedora
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
#!/bin/sh
echo Enter IP list, file name
read ips

exec<$ips
total=0
while read line
do
total=`expr $total + 1`;
echo $line;
done
echo Total IPs in the file $total";
I have been messing with it more, and made some progress, but not much. It is reading the file and i have it outputting the content of the file, and a total number of entries in the file. I still cant get it into an array as of yet, and the script issues a logout at the end for some reason and disconnects my ssh session.

Thanks
 
Old 04-13-2007, 04:39 PM   #4
omnio
Member
 
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Blog Entries: 1

Rep: Reputation: 16
Quote:
Originally Posted by TheMeeks
I am trying to write a bash script that will read one file line by line, into an array
Something like this?
Code:
#!/bin/bash
# reads from the $ips file and assigns to $MYARRAY

echo "Enter the file name"
read ips

index=0

while read line ; do
	MYARRAY[$index]="$line"
	index=$(($index+1))
done < $ips

echo "MYARRAY is: ${MYARRAY[*]}"
echo "Total IPs in the file: ${index}"
Make sure your file (with IPs) ends with a newline.
Quote:
Originally Posted by TheMeeks
the file is all text, and has one ip address per line, no ending spaces, or semi-colons, just ips.
Well I guess this makes the task somehow easier.

Last edited by omnio; 04-13-2007 at 05:17 PM.
 
1 members found this post helpful.
Old 04-13-2007, 05:39 PM   #5
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

Bash has a construct that is similar to perl in that an assignment can handle most of the work. The perl construct is:
Code:
@a = < FILEHANDLE >;
which reads one line into each element in the array "a", but bash uses parentheses. For example:
Code:
#!/bin/sh

# @(#) s2       Demonstrate array setting.

# See http://www.tldp.org/LDP/abs/html/arrays.html for details on
# array notation.

sh --version

debug=":"
debug="echo"

SEQ=/usr/bin/seq

# Create data file - omit if you have your own external file.

cat >data1 <<EOF
10.0.0.1
10.20.0.2
10.30.20.3
EOF

a=( $( cat data1 ) )

$debug " Number of elements in array is $(( ${#a[@]} ))"
for i in $($SEQ 0 $((${#a[@]} - 1)))
do
  echo ${a[$i]}
done

exit 0
Which produces:
Code:
% ./s2
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
Copyright (C) 2002 Free Software Foundation, Inc.
 Number of elements in array is 3
10.0.0.1
10.20.0.2
10.30.20.3
See the abs, noted above for details, best wishes ... cheers, makyo
 
1 members found this post helpful.
Old 04-13-2007, 07:03 PM   #6
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by TheMeeks
Code:
#!/bin/sh
echo Enter IP list, file name
read ips

exec<$ips
total=0
while read line
do
total=`expr $total + 1`;

In bash (or any posix shell) arithmetic is built in; you don't need an external command:

Code:
total=$(( $total + 1 ))
Quote:
Code:
echo $line;
done
echo Total IPs in the file $total";
I have been messing with it more, and made some progress, but not much. It is reading the file and i have it outputting the content of the file, and a total number of entries in the file. I still cant get it into an array as of yet, and the script issues a logout at the end for some reason and disconnects my ssh session.

Bash has a special syntax for reading an entire file:
Code:
IFS='
'
file=( $( < FILENAME ) )
 
1 members found this post helpful.
Old 04-17-2007, 08:20 AM   #7
TheMeeks
LQ Newbie
 
Registered: Apr 2007
Location: Waterloo
Distribution: Fedora
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks guys, that helped alot. Ill do my best to get through the rest of the script myself. Appreciate the help.
 
  


Reply


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
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
Bash script - how to reassemble file from array sixerjman Programming 11 08-25-2006 11:45 AM
script to read file into array STAGED Programming 4 01-27-2006 08:06 PM


All times are GMT -5. The time now is 04:00 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration