0
if(grep "9" ls ); then echo "hello"; else echo "hi"; fi

when I execute the above command output is the below one

grep: ls: No such file or directory

hi

how can I just get hi(only condition) as output

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • please check you code at hottps://shellcheck.net . And use the `{}` tool from the Edit menu on mouse-selected text to format as `code/data/requiredOutput/ExactErrMsgs`. Good luck. – shellter Jan 29 '21 at 13:43
  • 1
    Do you understand that the "template" for an `grep` call is `grep "Search Target" file [file2 file....]` ? Do you have a file named `ls` in your current directory? (probably not). Good luck! – shellter Jan 29 '21 at 13:45
  • And, that using `( parens_surrounded_cmd )` means, "create a sub-shell and run included command".? Usually this is written as `if grep srchTarg file ; then found srchTarg ; fi`. Good luck. – shellter Jan 29 '21 at 13:47
  • If you are looking for a file that has a `9` in its name, you are probably thinking of `ls | grep 9`, but that [isn't recommended](http://mywiki.wooledge.org/ParsingLs). – chepner Jan 29 '21 at 15:17
  • And you could find a recommended alternative in [this question](https://stackoverflow.com/questions/2937407/test-whether-a-glob-has-any-matches-in-bash), using `*9*` as a glob. The accepted answer would be a good bash-only solution and the second most upvoted one would proposes a good portable solution. – Aaron Jan 29 '21 at 15:38

2 Answers2

0
grep -q "9" ls 2>/dev/null || echo "hi"

-q will not print grep output itself

2>/dev/null is not necessary, but suppresses possible error messages making the output 'more clean' (e.g. no grep: ls: No such file or directory)

Or even better solution on the recommendation of @Aaron:

grep -qs "9" ls || echo "hi"
Alex Zhu
  • 69
  • 3
  • 1
    Since you're already using `-q`, you could also replace the redirection of the output error by the `-s` flag. If I were you I'd present two alternatives, one where redirection is used for the two output streams and another one where `-sq` is used to suppress the outputs in the first place – Aaron Jan 29 '21 at 13:59
  • This doesn't address the issue of `ls` not existing. – chepner Jan 29 '21 at 15:18
  • @chepner that's most definitely the problem the question's author should focus on, but to be fair that's not the problem he asked about. – Aaron Jan 29 '21 at 15:33
  • It's a critical component, though; `grep -q "9" ls` is *still* going to fail, leaving his original question unanswered. – chepner Jan 29 '21 at 15:39
  • @chepner except if OP sometimes has a file named `ls` in his current directory and actually knows what he's doing. That seems improbable, but until he has clarified by answering to the comments on his question I'd rather not assume he isn't asking the question he should be – Aaron Jan 29 '21 at 15:43
  • @chepner I guess my initial response to your comment is more in response to the downvotes on those answers (which I wouldn't assume are yours) than to your comment itself, which I find helpful. They seem unfair to me as these answers actually answer the question as it is written. While the answers' authors might miss the big picture and not be as useful as they could be, it's not something I would expect from low-rep users, but I wouldn't want to discourage them from answering either. – Aaron Jan 29 '21 at 16:05
0

TL; DR

if( grep -q "9" ls 2>/dev/null); then echo "hello"; else echo "hi"; fi

OR

if( grep -sq 9 ls); then echo "hello"; else echo "hi"; fi


Explanation

You can use man grep command in terminal to see how -q suppresses the output. Refer this link to see why 2>/dev/null is used so that STDERR is not shown.

As OP's question shows 2 conditions. If and else, I suggested this solution. But, if you just want to have else part, you can refer to Alex's answer

ajinzrathod
  • 925
  • 10
  • 28