bash - reading user variable into bash script grep

8,703

Solution 1

-c means that you want to know the number of times this user is in /etc/passwd, while $? is the exit code. Those are differents, since the number of times is printed on stdout. Use $() for getting stdout into a variable

Second problem: all your variables, like $uzer will not be substituted with their values when in single quotes. Use double quotes.

number=$(grep -c "^${uzer}:" /etc/passwd)
if [ $number -gt 0 ]; then
  echo "User does exist :)"
else
  echo "No such user"
fi

Solution 2

You need grep -q. If you don't distinguish between more exit codes than "0" and "other" there is no need to seperate grep and if:

if grep -q "^${uzer}:" /etc/passwd; then
  echo "User does exist :)"
else
  echo "No such user"
fi
Share:
8,703

Related videos on Youtube

user3779606
Author by

user3779606

Updated on September 18, 2022

Comments

  • user3779606
    user3779606 almost 2 years

    I've tried every possible combination to get this bash script working. It's part of a larger script, and it basically prompts for a username (to check if it exists) and returns the appropriate response:

    #! /bin/bash
    # Script to see if User exists
    
    clear
    echo -n "Enter user to check: "
    read $uzer
    
    grep -c '^${uzer}:' /etc/passwd
    
    if [ $? -eq 0 ]; then
      echo "User does exist :)"
    else
      echo "No such user"
    fi
    

    In terminal the following works fine:

    grep -c '^devuser1:' /etc/passwd
    RETURNS: 1
    grep -c '^devuser1234:' /etc/passwd
    RETURNS: 0
    

    I've tried many combinations of passing the read variable into '^${uzer}:' with no joy. Any ideas what else I can try?

    • Angel Todorov
      Angel Todorov about 11 years
      read $uzer should be read uzer
  • user3779606
    user3779606 about 11 years
    Thanks for your reply! However, I get the following error: Enter user to check: devuser1 ./userexist.sh: line 8: =0: command not found ./userexist.sh: line 10: [: -eq: unary operator expected No such user
  • user3779606
    user3779606 about 11 years
    sorry, can't do newlines in comments!
  • eppesuig
    eppesuig about 11 years
    right. I already corrected my answer. Please use number instead of $number in the assignment.
  • Uwe
    Uwe about 11 years
    The single quotes around ^${uzer}: must be replaced by double quotes, otherwise the variable ${uzer} is not expanded.
  • user3779606
    user3779606 about 11 years
    Sweet! I didn't even notice the $ at the beginning. No errors now, but it returns "No such user" even for a user that I know exists. I feel it has something to do with the way $uzer is used in the regex
  • Uwe
    Uwe about 11 years
    there is an error in the read command: it must be read uzer rather than read $uzer.
  • Hauke Laging
    Hauke Laging about 11 years
    @Uwe How embarrassing. I just copied the code and was looking at the other problem only...
  • user3779606
    user3779606 about 11 years
    You're a STAR!! Thank you! This is my first dive into bash scripting so still getting used to the syntax :)
  • user3779606
    user3779606 about 11 years
    Hi Hauke. Thanks anyway for your response. The answer from eppesuig above worked like a charm. I did something similar to this before, and the error I got was "...too many arguments..."
  • Uwe
    Uwe about 11 years
    Bourne shell and bash distinguish between a variable and its current value. If you want the current value of variable A, it's $A; if you want the container rather than what's inside (i.e., in assignments A=... and read A) the $ is missing.
  • Stéphane Chazelas
    Stéphane Chazelas about 11 years
    That would still think that the r.ot and root:0 users exist though. Using grep here is wrong. It must be IFS= read -r uzer, not read uzer let alone read $uzer