Solved

if string is there then do nothing

Posted on 2013-01-11
7
320 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

776 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