Solved

if string is there then do nothing

Posted on 2013-01-11
7
319 Views
Last Modified: 2013-01-15
I am trying to create an if statement that searches for the string and if its there don't do anything my script is still writing into the file I want it to not write if the string is there and if its not there then write the string into the file this is what I have.


if grep -q "string" /etc/syslog.conf
then
sed -i -e '/^authpriv.info/ a\
authpriv.none\t\t\t\t\t\t\t/var/log/secure' /etc/syslog.conf
else
     if grep -q "string" /etc/syslog.conf
     then
     echo "authprive.none exists"

     fi
fi
0
Comment
Question by:atom_jelly
7 Comments
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 84 total points
ID: 38768661
Use

if ! grep ...

in the first line.
0
 
LVL 7

Assisted Solution

by:amitnepal
amitnepal earned 83 total points
ID: 38768707
grep -q "string" /etc/syslog.conf
if [ $? -eq 0 ];then
      sed -i -e '/^authpriv.info/ a\
      authpriv.none\t\t\t\t\t\t\t/var/log/secure' /etc/syslog.conf
else
      grep -q "string" /etc/syslog.conf
      if [ $? -eq 0 ];then
            echo "authpriv.none exists"
      fi
fi
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 166 total points
ID: 38769015
You could try writing your script in 'pseudo' code first and then replace 'pseudo' code with real code. Like this:

if 'string is not there'
then 'do nothing'
else 'write string to file'

would translate into:

if ! grep -q "string" /etc/syslog.conf
then
  echo "authprive.none exists"
else
  sed -i -e '/^authpriv.info/ a\
  authpriv.none\t\t\t\t\t\t\t/var/log/secure' /etc/syslog.conf
fi
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 167 total points
ID: 38769562
grep -q 'string' || echo writing into the > file
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen
Gerwin Jansen earned 166 total points
ID: 38769586
@ahoffmann:
grep -q 'string' somefile || echo writing into the > file
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 167 total points
ID: 38769597
(only if you want it to be completed in measurable time :-)
thanks
0
 

Author Closing Comment

by:atom_jelly
ID: 38777957
Thank you all.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Fine Tune your automatic Updates for Ubuntu / Debian
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

23 Experts available now in Live!

Get 1:1 Help Now