How to run "finger" command in an if statement?

I have been trying to run the finger command in a if statement but its giving me a bunch of errors.

gidlistTemp="g274gG;g2759C;g28320;g2885G;g2A276;g23338;g2A5h5;g2A307"
for i in $(echo $gidlistTemp| tr ';' ' \n')
do
        tst=(finger $i | wc -l)
        if [$tst != 0]
        then
                gidlist=$gidlist$i
        fi
done

How do I fix this?

tst=$(finger $i | wc -l)

Doesn't work, the $ makes in front of finger makes it think that finger is a variable.

No, it sees the $( ... ) . A Posix-compatible shell treats it like the traditional ` ... `
Further, make sure there a spaces around the [ ]

 if [ $tst != 0 ]

Expanding a bit on what MadeInGermany already said... the code rdrtx1 suggested:

tst=$(finger $i | wc -l)

does not have a dollar sign in front of finger ; it has a dollar sign in front of an opening parenthesis. What rdrtx1 suggested was to execute the pipeline finger $i | wc -l and store the output written to its standard output in the variable named tst .

Your code:

tst=(finger $i | wc -l)

(making the wild assumption that you are using a shell that has array variables) attempts to make tst an array of values that arise from splitting the string finger current_value_of_variable_i | wc -l but gets a syntax error because a pipeline has higher precedence than assigning an array to a variable. And, if you get past that syntax error (and ignore the probable bug in the command substitution:

$(echo $gidlistTemp| tr ';' ' \n')

which will be treated exactly the same as the common substitution:

$(echo $gidlistTemp| tr ';' ' ')

when I'm not sure whether you wanted to alternate between <space> and <newline> or just want to replace all occurrences of <semicolon> with <space> or to replace all occurrences of <semicolon> with <newline>), we then get to the syntax errors in your if statement:

        if [$tst != 0]

which (assuming that you are using a shell based on Bourne shell syntax) requires a space between [ and $tst and between 0 and ] and for a numeric comparison should use -ne instead of != . Then we also have the problem that we do not know what system you're using and what output its version of finger produces when a user is looked in and when a user is not logged in. On OS X (using a BSD based finger utility) finger produces five lines of output for a user that is not logged in and 5 + n lines of output for a user that is currently logged in n times.

So, if we remove all of your syntax errors, it appears that what you are trying to do is remove all of the <semicolon> characters from the string stored in the variable gidlistTemp and store the result of that conversion in the variable named gidlist .

But, since you have never told us what you are trying to do, what operating system you're using, what shell you're using, nor the exact "bunch of errors" your script is producing; we are left to make wild guesses at how to correct your script. If you would be willing to share all of this information with us, we might be able to help you correct your script to do what you want to do.