lundi 1 février 2021

If statement gives "[: too many arguments" when grep pattern is found [duplicate]

I'm trying to make a bash script where it searches for a pattern in different files (they start all with the same word).

The script works but gives an error when the pattern is found ("[: too many arguments") and after the error it gives the result that I'm looking for. When the pattern is not found, the error does not show up. So how do I get rid of the error when the pattern is found?

#!/bin/bash

#Look for sample with their barcode

echo "Wich sample are you looking for?"
read barcode

pattern=$(grep $barcode samples*)

if [ -z $pattern ]

then echo "$barcode did not yet arrive."
else echo "$pattern"

fi

Aucun commentaire:

Enregistrer un commentaire