Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

if string is there then do nothing

Posted on 2013-01-11
7
Medium Priority
?
325 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 336 total points
ID: 38768661
Use

if ! grep ...

in the first line.
0
 
LVL 7

Assisted Solution

by:amitnepal
amitnepal earned 332 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 664 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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 668 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 664 total points
ID: 38769586
@ahoffmann:
grep -q 'string' somefile || echo writing into the > file
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 668 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article will show, step by step, how to integrate R code into a R Sweave document
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

688 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