A
A
Anton2015-12-23 23:51:32
linux
Anton, 2015-12-23 23:51:32

What is the error (let: =: syntax error: operand expected (error token is "="))?

Hello!
There is a script:

#!/bin/bash

echo "Введите своё имя:"

read user_name

let count = `grep $user_name /etc/passwd | wc -l`

if [ $count -gt 0 ];
  then
    echo "В файле /etc/passwd найдено $count совпадений!"
  else
    echo "Нет совпадений!"
fi

Swears:
./scrypt.sh: line 7: let: =: syntax error: operand expected (error token is "=")
./scrypt.sh: line 9: [: -gt: unary operator expected
What's the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2015-12-24
@hummingbird

You have a space between count between = and between the actual value being assigned, resulting in count being empty.
And in the test statement you have [ $count -gt 0] with an empty variable, $count stands for [ -gt 0 ], which is what the syntax error reports.
When using the test statement, always quote variables, i.e.
[ "$count" -gt 0 ] or use the advanced test - , which is even better.
Be careful with spaces - where they are needed and where they are not needed.

M
Mark Doe, 2015-12-23
@mourr

Are you sure you understand how to use let? Documentation says it is used for arithmetic EXPRESSION
stackoverflow.com/questions/18704857/bash-let-stat...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question