3

I'm writing a program that prints the username and the number of times that the user has logged in, or prints "Unknown user" otherwise.

My code is the following:

iden=$1
c='last | grep -w -c $iden'
if (( $c > 1 ))
then
    echo "$iden $c"
else
    echo "Unknown user"
fi

And I keep getting this error:

-bash: ((: last | grep -w -c 123: expression recursion level exceeded (error token is "c 123")

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Pedro
  • 43
  • 1
  • 4

2 Answers2

5

To store the output of a command in a variable you need to say var=$(command). Hence, use:

c=$(last | grep -w -c "$iden")  # always good to quote the variables

instead of

c='last | grep -w -c $iden'

If you are learning Bash scripting, it is always handy to paste your code in ShellCheck to see the problems you may have.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

You can use this too:

c=`last | grep -w -c "$iden"`
Jahid
  • 21,542
  • 10
  • 90
  • 108
  • 1
    Backticks and `$()` are equivalent and in, in fact, `$()` is prefered since you can nest them. See [Shell Programming: What's the difference between $(command) and `command`](http://stackoverflow.com/q/4708549/1983854). – fedorqui Apr 19 '15 at 10:54