[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2016-11-05
5
Medium Priority
?
79 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 80

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 20

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

656 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