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 06: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
LR_ENVIRONMENT.IN(1)					  LogReport's Lire Documentation				      LR_ENVIRONMENT.IN(1)

NAME
lr_environment - Export Lire configuration in shell script form SYNOPSIS
eval `lr_environment` DESCRIPTION
The lr_environment command is used to import the Lire configuration in Lire shell scripts. All of Lire configuration variables will be written in a format that can be evaled by the shell. Shell scripts don't usually have to use that command, since it is done by the defaults file sourced by each command. The old names used by when the configuration was done in shell script are also exported by this script for backward compatibility. AUTHOR
Francis J. Lacoste <flacoste@logreport.org> VERSION
$Id: lr_environment.in,v 1.12 2006/07/23 13:16:33 vanbaal Exp $ COPYRIGHT
Copyright (C) 2003 Stichting LogReport Foundation LogReport@LogReport.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program (see COPYING); if not, check with http://www.gnu.org/copyleft/gpl.html. Lire 2.1.1 2006-07-23 LR_ENVIRONMENT.IN(1)
All times are GMT -4. The time now is 05:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy