bash - reading user variable into bash script grep
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
Related videos on Youtube
user3779606
Updated on September 18, 2022Comments
-
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 about 11 years
read $uzer
should beread uzer
-
-
user3779606 about 11 yearsThanks 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 about 11 yearssorry, can't do newlines in comments!
-
eppesuig about 11 yearsright. I already corrected my answer. Please use
number
instead of$number
in the assignment. -
Uwe about 11 yearsThe single quotes around
^${uzer}:
must be replaced by double quotes, otherwise the variable${uzer}
is not expanded. -
user3779606 about 11 yearsSweet! 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 about 11 yearsthere is an error in the
read
command: it must beread uzer
rather thanread $uzer
. -
Hauke Laging about 11 years@Uwe How embarrassing. I just copied the code and was looking at the other problem only...
-
user3779606 about 11 yearsYou're a STAR!! Thank you! This is my first dive into bash scripting so still getting used to the syntax :)
-
user3779606 about 11 yearsHi 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 about 11 yearsBourne 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 assignmentsA=...
andread A
) the$
is missing. -
Stéphane Chazelas about 11 yearsThat would still think that the
r.ot
androot:0
users exist though. Usinggrep
here is wrong. It must beIFS= read -r uzer
, notread uzer
let aloneread $uzer