Solved

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

Posted on 2016-11-05
5
40 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 77

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
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…

809 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