0

I am new to bash shell scripting and I'm getting that error. I looked on similar questions but didn't find my answer.

#!/bin/bash
user=$1
if [ who -u | grep -q "^$user" ]; then
    fullname=getent passwd $user | cut -d: -f5
    echo "User $fullname is logged in"
else
    echo "User $user is not logged in"
fi

Does anyone have any idea?

andra
  • 97
  • 6
  • My shell says: ``line 3: [: missing `]'`` – axiac Mar 19 '19 at 15:50
  • 1
    http://shellcheck.net/ is your friend here; many separate bugs it'll detect. – Charles Duffy Mar 19 '19 at 15:51
  • If you still get errors after shellcheck gives your code a clean bill of health (meaning you've removed the inappropriate `[ ]` syntax and made it `fullname=$( ... )`), then you're probably saving your script as a DOS text file with CRLF newlines; see #1 in the "before asking about problematic code" section of https://stackoverflow.com/tags/bash/info – Charles Duffy Mar 19 '19 at 15:55
  • wow, thank you so much!! had to make the changes you suggested and delete DOS carriage return characters. you rock! – andra Mar 19 '19 at 20:15

0 Answers0