Sponsored Content
Top Forums Shell Programming and Scripting Shell script to read multiple options from file, line by line Post 302653625 by haggismn on Saturday 9th of June 2012 03:45:09 PM
Old 06-09-2012
Shell script to read multiple options from file, line by line

Hi all
I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis.

The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here is an example

Code:
$ cat /tmp/servers
172.18.1.1

172.18.50.1 tcp SSH SSL=8443

The script should ignore any empty lines, obviously. For the line with server 172.18.1.1, default settings should be used as nothing else is specified (Default is UDP mode port 500). For the line 172.18.50.1, the specified settings are that tcp mode is to be used, and SSH and SSL on port 8443 options are also to be used.

This is what I have created, as an example. The final product will obviously do much more, it is the reading in of the lines and variables that I need help with.

Code:
#!/bin/sh
cat /tmp/servers | while read SRV ; do
IP=$(grep -o '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $SRV)
if [ ! -z $IP ] ; then
if grep -i "tcp" $SRV ; then TCP=1 ; fi
if grep -i "udp" $SRV ; then UDP=1 ; fi
if ! grep -i "tcp" $SRV ; then UDP=1 ; fi
SSL=$(grep -i -o 'ssl=[0-9]\{1,5\}' $SRV | cut -d = -f2) ; if [ -z $SSL] ; then if grep -i "ssl" $SRV ; then SSL=443 ; fi ; fi
SSH=$(grep -i -o 'ssh=[0-9]\{1,5\}' $SRV | cut -d = -f2) ; if [ -z $SSH] ; then if grep -i "ssh" $SRV ; then SSH=22 ; fi ; fi
HTTP=$(grep -i -o 'http=[0-9]\{1,5\}' $SRV | cut -d = -f2) ; if [ -z $HTTP] ; then if grep -i "http" $SRV ; then HTTP=80 ; fi ; fi 
echo "$IP $SSL $SSH $HTTP" 
fi ; done

The idea here is that each line will be read in. grep will attempt to find a valid IP address in that line, and if it does ( if [ ! -z $IP ] ) then the line will be checked for all possible options. Otherwise if there is no valid IP address on the line it will go to the next line.

I know that I am going wrong somewhere. I cannot use the $SRV string to get any information, although this pasted script simply gives no response. I also believe that using
Code:
if [ ! -z $SRV ]

will not work with the servers file where the line contains spaces. Is this true?

Can anyone advise on the commands I should be using to read the lines and gain these variables. Could I use a for loop instead?

Thanks for any help

PS also, is the usage of grep ok? I had used awk before but this seems to work ok and be shorter. Is there any advantage of one over the other? Thanks

Last edited by haggismn; 06-09-2012 at 07:05 PM.. Reason: fixed ssl,ssh,http if -z statements
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

2. UNIX for Dummies Questions & Answers

shell script to read file line by line

Hi, I need to read a text file from shell script line by line and copy the feilds of each line. Below is the complete requirement. I've text file which contains ... pgm1 file11 file12 file13 pgm2 file21 file22 pgm3 file31 file32 file33 I'll give input as... (4 Replies)
Discussion started by: ani12345
4 Replies

3. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

4. Shell Programming and Scripting

how can u read a file line by line in shell script ?

hello , plz help for below script req:- how can we read a file line by line in shell script ? (4 Replies)
Discussion started by: abhigrkist
4 Replies

5. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

6. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

7. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

8. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

9. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

10. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies
ZGREP(1)						      General Commands Manual							  ZGREP(1)

NAME
zgrep - search possibly compressed files for a regular expression SYNOPSIS
zgrep [ grep_options ] [ -e ] pattern filename... DESCRIPTION
Zgrep invokes grep on compressed or gzipped files. These grep options will cause zgrep to terminate with an error code: (-[drRzZ]|--di*|--exc*|--inc*|--rec*|--nu*). All other options specified are passed directly to grep. If no file is specified, then the standard input is decompressed if necessary and fed to grep. Otherwise the given files are uncompressed if necessary and fed to grep. If the GREP environment variable is set, zgrep uses it as the grep program to be invoked. EXIT CODE
2 - An option that is not supported was specified. AUTHOR
Charles Levert (charles@comm.polymtl.ca) SEE ALSO
grep(1), gzexe(1), gzip(1), zdiff(1), zforce(1), zmore(1), znew(1) ZGREP(1)
All times are GMT -4. The time now is 02:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy