?
Solved

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

Posted on 2016-11-05
5
Medium Priority
?
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Assisted Solution

by:jessbruffett
jessbruffett earned 668 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 79

Assisted Solution

by:arnold
arnold earned 668 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 664 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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