Solved

if string is there then do nothing

Posted on 2013-01-11
7
322 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
[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
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 38

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Assisted Solution

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

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
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.:
The viewer will learn how to count occurrences of each item in an array.

739 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