shell variable with multiple values

shell variable with multiple values

this probably is very easy to do but I just can't get my head around it

I've got a script that grep some values from a file (file1)

sort of

var1=$(cat file1 | grep foo | grep bar)

if I echoed $var1 it would be a simple word string like string1

997 out of 1000 times, var1 would be only 1 string, but then, there's a really small number of cases in which it has more than 1 string

string1 string2

how can I check if var1 has more than 1 string?

I've tried with if [[ $var1 -gt "1" ]] but obviously this is for integers

答案1

May be this will help you to get the expected result

var="$(grep 'foo' A.txt ) $(grep 'bar' A.txt)"
echo $var

count=`echo $var | wc -w`  
 
if [ $count -eq 1 ]
then
    echo "Contains only one string"
else
    echo "Contains more then one string"
fi   

相关内容