LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 03-17-2006, 03:16 PM   #1
Harry Seldon
Member
 
Registered: Feb 2004
Distribution: SLES 9
Posts: 112

Rep: Reputation: 15
Trying to read a file by line for a script


I'm having trouble with a script file I'm writing to disable services in the /etc/inetd.conf file. What I'm trying to do is back the file up, then read each line, look at the first word of the line, and add a # to the beginning of the line to comment it out. Here's the script:

#! /bin/ksh -x
#
# This script finds and disables services per the Unix configuration
# guidelines for Linux servers. - NH
#
cp /etc/inetd.conf /etc/inetd.conf.old
echo > /etc/inetd.conf.test
for line in `cat /etc/inetd.conf`
do
read first rest < $line
case "$first"
in
telnet) newline="\# $line";;
ftp) newline="\# $line";;
shell) newline="\# $line";;
login) newline="\# $line";;
klogin) newline="\# $line";;
exec) newline="\# $line";;
comsat) newline="\# $line";;
uucp) newline="\# $line";;
bootps) newline="\# $line";;
finger) newline="\# $line";;
systat) newline="\# $line";;
netstat) newline="\# $line";;
tfp) newline="\# $line";;
talk) newline="\# $line";;
ntalk) newline="\# $line";;
sadmind) newline="\# $line";;
uucpd) newline="\# $line";;
lpd) newline="\# $line";;
kpropd) newline="\# $line";;
stfsloader) newline="\# $line";;
*) newline="$line";;
esac
echo $newline >> /etc/inetd.conf.test
done

For some reason, it looks like it's getting each work of each line and placing it in the new file. I'm getting errors like this when the script runs:

+ ./config.sh[9]: root: cannot open
+ newline=root
+ echo root
+ 1>> /etc/inetd.conf.test
+ read first rest
+ 0< /usr/sbin/in.uucpd
+ newline=/usr/sbin/in.uucpd
+ echo /usr/sbin/in.uucpd
+ 1>> /etc/inetd.conf.test
+ read first rest
+ ./config.sh[9]: in.uucpd: cannot open
+ newline=in.uucpd
+ echo in.uucpd
+ 1>> /etc/inetd.conf.test

As far as I can tell, my read command is set up correctly but that seems to be the problem. Does anyone see my mistake?
 
Old 03-18-2006, 09:39 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try
Code:
while line=$(line)             # was: for line in `cat /etc/inetd.conf`
do
echo "$line" | read first rest # was read first rest < $line
case "$first"
...
done < /etc/inetd.conf         # was: done
 
Old 03-18-2006, 03:01 PM   #3
Harry Seldon
Member
 
Registered: Feb 2004
Distribution: SLES 9
Posts: 112

Original Poster
Rep: Reputation: 15
Perfect! Thank you!
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
C: fread to read a file line by line until the end Blue_muppet Programming 2 09-19-2008 09:42 AM
Script to read line by line from a file kushalkoolwal Programming 20 01-27-2006 04:17 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 05:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:06 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration