ProgrammingThis 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.
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.
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.
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.
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
#!/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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.