I need to write a script that prompts the user to enter a username and then if that username exists, lists all of the user's information from the password file and a list of all commands they are currently running. So far I have this:
#!/bin/bash
echo "Please enter username"
read username
if [ "$username" = cat /etc/passwd | cut -d: -f1 ]
then
//list all user info from passwd file
id $username
//list all commands they are running
i dont know how to code this
fi
If you guys can help me fill in or change anything that will be great, thanks!
cat
command was used with a command substitution like"$(cat … | cut …)"
, you would compare$username
against the list of all usernames and this would very likely fail. Instead you could usegrep
withif grep -q "^${username}:" /etc/passwd; then …
or test if the username exists withif id "$username" >/dev/null 2>&1; then …
discarding the output of stdout and stderr. – Freddy Nov 06 '19 at 04:03