1

I'm trying to write a script that does something when the read line does not include a * symbol in the line.

I've tried the following with no success:

  1. if [ grep -v -q '*' ] which results in a too many arguments error

  2. if [ "$(grep -v -q '*')" -eq '0' ]; which results in a unary operator expected error.

What am I doing wrong?

2

An if statement will accept anything that produces exit status, so either a shell builtin like [ or test - or a disk file like grep. You can use both:

if [ "$(grep -c '*' file)" = 0 ]
then
  echo 'star symbol not found'
fi

but its really not needed:

if ! grep -q '*' file
then
  echo 'star symbol not found'
fi

Or:

while read x
do
  if ! grep -q '*' <<eof
$x
eof
  then
    printf '%s does not contain star symbol\n' "$x"
  fi  
done < file

Or:

awk '!/\*/ {print $0, "does not contain star symbol"}' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.