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?
[is the name of a command, not part of theifstatement's syntax.ls.[], useif ls /some/dir | grep -q pattern; then. But really, you should use a glob for this:shopt -s nullglob; f=(*txt*); if (( ${#f[@]} > 0 )); thenlsorgrep.