Results 1 to 8 of 8

Thread: Combine if and grep

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Austria / Central Europe
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default Combine if and grep

    Hello, nixcraft forums!

    Is there a way to combine if and grep, so that if executes a command if grep finds a matching string in a file? If yes, can you show me the syntax?

  2. #2
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    486
    Thanks
    10
    Thanked 50 Times in 47 Posts
    Rep Power
    8

    Default

    grep command return exit status 0 if match found if not then exit status 1
    you can use grep and if
    ex.
    HTML Code:
    #!/bin/bash
    read -p "Please enter Username to check user exist or not:- " USERNAME
    grep $USERNAME /etc/passwd 2>&1 >/dev/null
    if [ $? -eq 0 ]; then
    echo "$USERNAME available on this system"
    else
    echo "$USERNAME not found "
    fi

  3. The Following User Says Thank You to Rahul.Patil For This Useful Post:

    Numpad (13th April 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Location
    Austria / Central Europe
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Thank you!

  5. #4
    Senior Member
    Join Date
    Aug 2011
    Posts
    453
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    hi,

    to be precise
    Code:
    #!/bin/bash
    
    read -p "Please enter Username to check user exist or not:- " USERNAME
    grep -q $USERNAME /etc/passwd >/dev/null 2>&1 && echo "$USERNAME available on this system" || echo "$USERNAME not found "
    theses simple tests don't need if statements.
    beware, redirection has to be this, not rahul's; see bash man page about redirections.

  6. The Following User Says Thank You to Watael For This Useful Post:

    Numpad (13th April 2012)

  7. #5
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    486
    Thanks
    10
    Thanked 50 Times in 47 Posts
    Rep Power
    8

    Default

    Hi Watael,

    beware, redirection has to be this, not rahul's; see bash man page about redirections.
    but why this also works "/etc/passwd 2>&1 >/dev/null"

  8. #6
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    486
    Thanks
    10
    Thanked 50 Times in 47 Posts
    Rep Power
    8

    Default

    Originally posted by Watael
    don't need if statements.
    we knew it we can use "&& ||"
    but Numbad Question related to combine if and grep
    thats the reason i post that code.

  9. #7
    Senior Member
    Join Date
    Aug 2011
    Posts
    453
    Thanks
    0
    Thanked 62 Times in 58 Posts
    Rep Power
    10

    Default

    I understand. sometimes I do it too

  10. #8
    Senior Member Rahul.Patil's Avatar
    Join Date
    Feb 2012
    Location
    Mumbai india
    Posts
    486
    Thanks
    10
    Thanked 50 Times in 47 Posts
    Rep Power
    8

    Default

    Hi Watael,

    Thanks for correcting me
    i am wrong , i checked all scripts under "/etc/init.d/" , in all scripts i found this code ">/dev/null 2>&1"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. grep / cat
    By opnine in forum Linux software
    Replies: 2
    Last Post: 2nd March 2014, 07:54 PM
  2. [Solved] Grep one of two words from the file ( grep OR example )
    By chinalinix in forum Shell scripting
    Replies: 4
    Last Post: 8th November 2011, 04:44 PM
  3. Script to combine contents of files
    By linuxgeek in forum Shell scripting
    Replies: 2
    Last Post: 14th April 2011, 04:57 PM
  4. [Solved] Sed removal of :newline and combine two new lines
    By hemso in forum Shell scripting
    Replies: 3
    Last Post: 28th September 2010, 02:12 AM
  5. Replies: 1
    Last Post: 26th July 2007, 05:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •