Solved

if string is there then do nothing

Posted on 2013-01-11
7
318 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

744 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

10 Experts available now in Live!

Get 1:1 Help Now