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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With