LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-27-2011, 08:39 PM   #1
jnreddy
Member
 
Registered: May 2008
Location: INDIA
Distribution: RHEL
Posts: 170

Rep: Reputation: 15
String replace in shell script


Hi Friends and Guru's

I need to replace a string in my script..

my sample file :

root# cat passwd
################################
This is script generated file

tftp = off
dns = off

my script:
#!/bin/sh
cat /nir/passwd | grep tftp >/dev/null
RC=$?
if [ "$RC" = 0 ] ; then
echo "Tftp is exist"
RM=`cat /nir/passwd | grep tftp | awk '{print$3}'`

if [ $RM = off ] ; then


echo "Fail: TFTP is : $RM: Do you want to make it : ON"
else
echo " TFTP is | ON......exiting"
fi
fi

in my sample its showing tftp = off and it should to modified to tftp = on.

any help is appreciateable

Thanks In Advance
JNReddy
 
Old 05-27-2011, 11:12 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Here's one solution... the answer you seek is in bold text.

Code:
#!/bin/bash

FILE=passwd

state=`grep tftp $FILE | awk '{print $3}'`

if [ "$state" == "" ]; then
        echo "Error; TFTP not defined in $FILE"
        exit 1

elif [ "$state" == "off" ]; then
        echo -n "Do you want to enable TFTP (y/n)? [y] "
        read ans

        if [ "$ans" == "" -o "$ans" == "y" ]; then
                sed -e "s/tftp = off/tftp = on/" -i $FILE
                echo "TFTP has been turned ON."
        else
                echo "TFTP will be left OFF... exiting"
        fi

elif [ "$state" == "on" ]; then
        echo -n "Do you want to disable TFTP (y/n)? [y] "
        read ans

        if [ "$ans" == "" -o "$ans" == "y" ]; then
                sed -e "s/tftp = on/tftp = off/" -i $FILE
                echo "TFTP has been turned OFF."
        else
                echo "TFTP will be left ON... exiting"
        fi

else
        echo "TFTP is neither ON or OFF... exiting"
        exit 1
fi

exit 0
 
1 members found this post helpful.
Old 05-28-2011, 02:20 AM   #3
jnreddy
Member
 
Registered: May 2008
Location: INDIA
Distribution: RHEL
Posts: 170

Original Poster
Rep: Reputation: 15
Thumbs up Hats off

Hi dwhitney67,


Thanks a Lot it's an excellent script, it solved my task....

Thanks a lot...


Thanks
JNReddy
 
Old 05-28-2011, 03:07 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Please mark as SOLVED once you have a solution
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell script, recursive rename/string replace? lrall Linux - General 1 07-27-2010 11:12 PM
Need assistance with shell script - replace a string with a start tag and end tag SupermanInNY Programming 18 01-02-2010 06:44 PM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 04:07 AM

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

All times are GMT -5. The time now is 06:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration