I'm trying to use process substitution with grep as follows:
#!/bin/bash
if [grep -c "*" <(cut -d"," -f5 input_q1.csv) ]
then
echo "Yes";
else
echo "No";
fi
But I'm getting the error:
line 2: [grep: command not found
What am I missing?
I'm trying to use process substitution with grep as follows:
#!/bin/bash
if [grep -c "*" <(cut -d"," -f5 input_q1.csv) ]
then
echo "Yes";
else
echo "No";
fi
But I'm getting the error:
line 2: [grep: command not found
What am I missing?
[ ... ]
is not part of the syntax of an if-statement; rather, [
(also called test
) is a Bash built-in command that is very commonly used as the test-command in an if-statement.
In your case, your test-command is grep
rather than [
, so you would write:
if grep -c "*" <(cut -d"," -f5 input_q1.csv) ; then
Additionally, there's no real reason to use process substitution here. The overall result of a pipeline is the result of the final command in the pipeline; and grep supports reading from standard input. So you can write:
if cut -d"," -f5 input_q1.csv | grep -c "*" ; then
to support a wider range of systems.
(Incidentally, the quotation marks around the ,
don't do anything: -d","
means -d,
means "-d,"
. If your goal is to "highlight" the delimiter, or set it off in some way, then it might make more sense to split it into a separate argument: -d ,
. But that's a matter of preference.)
if cut -d"," -f5 input_q1.csv | grep -c "*"; then
... and you'll need to change *
into a valid regex, because *
most certainly isn't.
As I've explained in Why doesn't my if statement with backticks work properly?,
if
in shells
takes a command.
[
is a way to invoke the test
command -- it's NOT like the parentheses that if
takes in other languages.