Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7859485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:55:08+00:00 2026-06-02T21:55:08+00:00

I am trying to do an if/then statement, where if there is non-empty output

  • 0

I am trying to do an if/then statement, where if there is non-empty output from a ls | grep something command then I want to execute some statements. I am do not know the syntax I should be using. I have tried several variations of this:

if [[ `ls | grep log ` ]]; then echo "there are files of type log";
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:55:09+00:00Added an answer on June 2, 2026 at 9:55 pm

    Well, that’s close, but you need to finish the if with fi.

    Also, if just runs a command and executes the conditional code if the command succeeds (exits with status code 0), which grep does only if it finds at least one match. So you don’t need to check the output:

    if ls | grep -q log; then echo "there are files of type log"; fi
    

    If you’re on a system with an older or non-GNU version of grep that doesn’t support the -q ("quiet") option, you can achieve the same result by redirecting its output to /dev/null:

    if ls | grep log >/dev/null; then echo "there are files of type log"; fi
    

    In this particular case, you can do without grep entirely, since ls returns nonzero if it doesn’t find a specified filename, as in D.Shawley’s answer:

    if ls *log* >&/dev/null; then echo "there are files of type log"; fi
    

    But in Zsh, or Bash with the failglob option set, such a command will error out if the wildcard doesn’t match anything, without ever actually running ls. You can take advantage of that behavior to check without needing ls at all:

    # Zsh, or Bash with failglob set
    if (echo *log*) >&/dev/null; then
      echo "there are files of type log"
    fi
    

    Or you could set nullglob instead:

    # with 'nullglob' set, in either Bash or Zsh
    for f in *log*; do
      echo "There are files of type log"
      break
    done
    

    In Ksh, or Bash without either option set, it’s a little more work:

    # Ksh or Bash without failglob or nullglob
    for f in *log*; do 
      # even if there are no matching files, the body of this loop will run once
      # with $f set to the literal string "*log*", so make sure there's really
      # a file there:
      if [ -e "$f" ]; then 
        echo "there are files of type log"
        break
      fi
    done 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run multiple ddl statements within one Execute Immediate statement. i thought
Im loading an image from a SQL CE db and then trying to load
rebuilding a mac from scratch. Installed xcode and rvm then trying to install rubies
i have a simple template that i populate with some data then im trying
I'm trying to load page and then remove some elements in it. While doing
I'm having some problems trying to format an If/Then condition in a Wordpress widget.
I'm trying to execute a script from within my java code which looks like:
Trying to set a sprite filename with an if statement, then load the proper
I have some Fortran 77 source files that I'm trying to convert from a
I am currently using sql.row(statement) and storing to a list. I then am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.