I read strings from stdin and I want to display the users that match the strings. Problem is, if the user inputs the character '[', or a string containing it.
grep -F
does not work because the line has to start with the string (^ - which is a simple character with -F). Also, getent $user
won't be good because I need only the username not id as well.
if [[ "$user" == *"["* ]]; then
echo -e "Invalid username.\n"
continue
fi
if ! getent passwd | grep "^$user:"; then
echo -e "Invalid username.\n"
continue
fi
This is the workaround for '[', is there another way?
awk
will do the job most probably, but I have no knowledge of it yet, I'm interested in grep.
\[{^$.*
– Gilles 'SO- stop being evil' Apr 27 '17 at 22:43