1

I'm trying to use an if [grep] using the exit value to trigger it

to test, from the command line

$ ls ~/dir | grep txt
$ echo $?
0

However when i use it in an if statement in a script, i.e.

if [ ls /some/dir | grep -q pattern ]; then
    echo y
fi

It says

line 1: [ missing `]' (the line the if statement was written on)

Why is this happening and is there a way to fix it?

4
  • 4
    [ is the name of a command, not part of the if statement's syntax. Commented Feb 5, 2020 at 19:58
  • 5
    Never parse ls. Commented Feb 5, 2020 at 19:59
  • 1
    To get what you're trying to work: drop the [], use if ls /some/dir | grep -q pattern; then. But really, you should use a glob for this: shopt -s nullglob; f=(*txt*); if (( ${#f[@]} > 0 )); then Commented Feb 5, 2020 at 20:01
  • 1
    BTW, what you're trying to do here in a larger sense (check whether any files match a pattern) is the subject of BashFAQ #4; the best-practice approach it teaches does not involve using ls or grep. Commented Feb 5, 2020 at 20:45

1 Answer 1

1

That is because [ is and conditional expression, check the details.

You should use () instead:

if (ls /some/dir | grep -q pattern); then
    echo y
fi
Sign up to request clarification or add additional context in comments.

3 Comments

Do not use the parens either. They make your code slower / less efficient than just leaving them out would be, as in if ls /some/dir | grep -q pattern; then
(the performance hit is because in shell, parens aren't just grouping; they tell the shell to fork off an additional subprocess and run the enclosed code inside it. Sure, pipes also require forking and so does execution of external commands, so it's not a fork-vs-no-fork decision here, but you're potentially creating one more fork than there would be otherwise, depending on how efficient the particular shell executing this code happens to be).
Moreover, the best practices for checking whether any files in a directory have names patching a pattern -- as taught in BashFAQ #4 -- do not involve either ls or grep; details of why parsing ls output is an antipattern are discussed in ParsingLs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.