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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-13-2007, 02:19 PM
|
#1
|
|
LQ Newbie
Registered: Apr 2007
Location: Waterloo
Distribution: Fedora
Posts: 3
Rep:
|
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 
|
|
|
|
04-13-2007, 02:53 PM
|
#2
|
|
Member
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Rep:
|
How does your bash script look so far?
|
|
|
|
04-13-2007, 03:28 PM
|
#3
|
|
LQ Newbie
Registered: Apr 2007
Location: Waterloo
Distribution: Fedora
Posts: 3
Original Poster
Rep:
|
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
|
|
|
|
04-13-2007, 04:39 PM
|
#4
|
|
Member
Registered: Feb 2007
Location: $HOME
Distribution: Hardened Gentoo
Posts: 66
Rep:
|
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.
|
04-13-2007, 05:39 PM
|
#5
|
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 713
Rep:
|
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.
|
04-13-2007, 07:03 PM
|
#6
|
|
Member
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mandriva, Ubuntu, LFS, gNewSense
Posts: 221
Rep:
|
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.
|
04-17-2007, 08:20 AM
|
#7
|
|
LQ Newbie
Registered: Apr 2007
Location: Waterloo
Distribution: Fedora
Posts: 3
Original Poster
Rep:
|
Thanks guys, that helped alot. Ill do my best to get through the rest of the script myself. Appreciate the help.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:38 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|