#!/bin/bash
while echo -n "Player's name?"
read name
[ $name != 'ZZZ' ]
do
searchresult=$(grep [$name] playername)
if [ $searchresult=0 ]
then
echo -n "if See target (T/t) or team name (M/m)?"
read team
read target
while [ [ $target!=T ] || [ $team!=M ] ]
do
echo "Please enter only T or M."
done
if $target=T
then
grep [ $name ] targetselected
else
grep [ $name ] teamselected
fi
else
echo 'no such player'
fi
done
echo You are now exited search
2 Answers
The two lines:
searchresult=$(grep [$name] playername)
if [ $searchresult=0 ]
are very wrong. If you are trying to determine if the grep succeeds, it is best to do:
if grep -q "$name" playername; then ...
Putting grep in $() and assigning to searchresult has you comparing the output of grep rather than checking its exit status. And the spaces that are missing around the = in the second line are very wrong. Assuming searchresult is a string that contains no whitespace and only alphanumeric characters, the line if [ $searchresult=0 ] will always evaluate to true because the string "$searchresult=0" is always non-empty. (If searchresult is the empty string then $searchresult=0 is the string =0 which is non-empty, so the test succeeds.) It is highly unlikely that you intend to be testing whether or not that string is empty. The exact error that you are seeing indicates that the grep is assigning a value to searchresult which contains whitespace. In that case [ $searchresult=0 ] (without double quotes around $searchresult) is being parsed by [ as multiple arguments (split on the whitespace in $searchresult), and the error is telling you that there are too many arguments.
readinside thewhile, otherwise yourwhilecould never return false, since it does the same thing everytime?