Solved

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

Posted on 2016-11-05
5
32 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
Comment Utility
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 76

Assisted Solution

by:arnold
arnold earned 167 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you all
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Utilizing an array to gracefully append to a list of EmailAddresses
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.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now