Solved

Error in getting the exit status of grep inside shell script?

Posted on 2016-11-05
5
41 Views
Last Modified: 2016-11-11
hi,

When i am running the script with below...it is adding everytime a  same line as shown below help me to avoid this please?
My aim is to add line in /etc/hosts if it is not there?
the line is "Local_IP  Hostname" ?

[ansible@ansible2 outline]$ sudo ./add_hosts_entry.sh
 the grep command inside script not all exiting and everytime it is throwing wrong exit status only. Could you please

[ansible@ansible2 outline]$ cat add_hosts_entry.sh
#! /bin/bash

HOSTNAME=`hostname`
HOSTIP=`hostname -I`
HOSTENTRY="${HOSTIP} ${HOSTNAME}"
if grep -Fxq "$HOSTENTRY" /etc/hosts
then
echo "success" >> /tmp/hostentry.log
exit
else
echo $HOSTENTRY >> /etc/hosts
fi
[ansible@ansible2 outline]$ sudo ./add_hosts_entry.sh
[ansible@ansible2 outline]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain ansible2.bharathkumarraju.com ansible2
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.1.73 db2.bharathkumarraju.com db2
10.0.1.72  web2.bharathkumarraju.com web2
10.0.1.71 ansible2.bharathkumarraju.com
10.0.1.71 ansible2.bharathkumarraju.com
10.0.1.71 ansible2.bharathkumarraju.com
[ansible@ansible2 outline]$

Open in new window

0
Comment
5 Comments
 
LVL 3

Assisted Solution

by:jessbruffett
jessbruffett earned 167 total points
ID: 41875855
use echo -e "127.0.0.1\t$HOSTSENTRY" >> /etc/hosts (the 127.0.0.1 is just an example of course same ans the $HOSTENTRY) to put the line in, as for the search option, as for the grep try just using grep -e. i just wrote a quick test script similar to yours and it worked just fine.
0
 
LVL 78

Assisted Solution

by:arnold
arnold earned 167 total points
ID: 41875872
When you grep, the exit status is either true when the pattern is found or false.
You should always redirect the output from grep to >>/dev/null

If test grep .. >> /dev/null
   Then
echo found
Else
Echo no match
Fi

To confirm the behavior is as expected.
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 166 total points
ID: 41876046
Couple of things to do. One is to put double quotes round $HOSTENTRY in the "echo" line. That's what your grep is using, so that's what you echo should use.

Another is to run the script yourself - only when it works when run manually is there any chance of it running in ansible. Try "bash -x add_hosts_entry.sh" to see what the test is doing. Try taking off the -x option (you still want the test to succeed even if there is extra text after the hostname), and the -F (again, you don't need it).
0
 
LVL 1

Accepted Solution

by:
BharathKumarRaju DasaraRaju earned 0 total points
ID: 41876078
Hi,

Thanks a lot for your quick responses here the code that worked.

#! /bin/bash

HOSTNAME=`hostname`
HOSTIP=`hostname -I`
HOSTENTRY="${HOSTIP}${HOSTNAME}"
count=`grep "$HOSTENTRY" /etc/hosts | wc -l`

if [[ $count != 0 ]]
then
echo "success" >> /tmp/hostentry.log
exit
else
echo $HOSTENTRY >> /etc/hosts
fi

Open in new window

0
 
LVL 1

Author Closing Comment

by:BharathKumarRaju DasaraRaju
ID: 41883581
Thank you all
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question