I'm a beginner at Linux/Unix. Here is my assignment, it is too long to post everything here: https://i.sstatic.net/v2AJR.jpg
Here is the logic that I have:
This code will check if the user entered an argument or not. I do not know what to put under else. We never talked about the which statement which I learned from another user here so it is not expected to be used. Just basic commands this isn't a super advanced assignment.
if [ $# -ne 1 ]; then
echo Please enter a single, valid user id:
read userid
else
userid=$1
**what goes here?**
fi
To display a user's full name (i.e. Ben Franklin) this is the code that I have written and tested:
grep "$userid" /etc/passwd | cut -d: -f5 | sort | sed 's/^\(.*\), \(.*\)$/\2 \1/'
Determine whether the user is logged in or not:
If user is not logged in (will return exit code 1): who | grep $userid
- Then display
grep "$userid" /etc/passwd | cut -d: -f5 | sort | sed 's/^\(.*\), \(.*\)$/\2 \1/'ANDecho is NOT currently logged onthen end script with a code of 1.
If user is logged in (will return exit code 0): who | grep $userid
- Then display
grep "$userid" /etc/passwd | cut -d: -f5 | sort | sed 's/^\(.*\), \(.*\)$/\2 \1/'ANDecho is currently logged onthen end script with a code of 0.
Determining whether a user is valid:
If user is invalid (will return exit code 1): grep $userid /etc/passwd
- Then it should display
echo "The user you entered, $userid is not a valid user on this system."
If user is valid (will return exit code 0): grep $userid /etc/passwd
How do I write this script? What's the full structure? I am stuck on the structure/the setup.
elsewill go through the checks of whether or not the user exists, whether they are logged in, etc. (like the code examples I wrote above) I guess I am stuck on the checks part and implementing that?sed,grep,cut... etc to parse the that output. Think on what control structure could you use:if,for,while, etc. Have in mind that coding needs practice.