-1

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

  1. Then display grep "$userid" /etc/passwd | cut -d: -f5 | sort | sed 's/^\(.*\), \(.*\)$/\2 \1/' AND echo is NOT currently logged on then end script with a code of 1.

If user is logged in (will return exit code 0): who | grep $userid

  1. Then display grep "$userid" /etc/passwd | cut -d: -f5 | sort | sed 's/^\(.*\), \(.*\)$/\2 \1/' AND echo is currently logged on then 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

  1. 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.

7
  • 1
    In what way is this different from your last question? Commented Apr 9, 2020 at 20:09
  • 2
    @Derek Do the best you can to show us what code you can manage to write, it doesn't matter how bad it could be, without mention of the homework guide you where assigned. I tell you this because this question is also gonna be closed, unless you edit it showing specific problems, from actual code. Commented Apr 9, 2020 at 20:27
  • 1
    I honestly don't know where to start or the setup that I need. I tried my best to explain what I need done into words but I don't know how to translate that into code. I am just learning this. I'll try right now. I have the if statement for the arguments which I posted above. If user doesn't give an argument when executing the script, then it will ask the user what username. Then the else will 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? Commented Apr 9, 2020 at 20:48
  • 1
    I don't know how to differentiate and make the correct output appear. Am I suppose to use one big if statement? How do I tell it to appear is not a valid user if its not a valid user, if the user is logged on, if it's not logged on. Commented Apr 9, 2020 at 20:57
  • 2
    @Derek Go step by step. You don't need to code the whole thing at once. Write down every problem you need to solve. Then try to solve problem by problem. Use the terminal to test code, you don't need to write it everytime in a file and run it. Use google with key words: "bash command find user id". You will have tons of results (the bests from stackexchange). Test the commands in terminal, see what the output is, try 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. Commented Apr 9, 2020 at 21:16

1 Answer 1

1

As this looks like an assignment, I will only give you the tools and some hints:

  1. If no argument was given to the script, then $1 (the first argument to the script) will be empty. You can test this with [[ -z $1 ]]. If you want to display an error for too many arguments, test the number of arguments with [[ $# -eq 1 ]] (will be true for a single argument).

  2. The gentent passwd "$username" command can be used to get the passwd entry for the user $username. The command will fail with a non-zero exit status if the user does not exist. This means that you can use if ! getent passwd "$username" >/dev/null; then ...; fi to react to a non-existing user $username.

  3. The 5th field of the passwd entry for a user usually contains the user's full name (there is really no guarantee that it will be in "firstname lastname" form). The entry may have extra commas at the end that you have to delete. You can parse the output of getent passwd "$username" using cut -d: -f 5 to get at the fifth field, and then delete from the first comma with name=${name%%,*}.

    On some systems, the full name of the user may contain a & character. On those systems, this & character should be replaced by the username (so that the name System & for the user operator becomes System operator). I'm not sure Linux generally does this, but that could be done with name=${name/&/$username}.

  4. The who utility outputs a list of users that are currently logged on. To match $username against that, use grep -e "^$username\>". The \> matches the end of a word so that e.g. antony is not matched when looking or anton. grep -q is useful in an if statement.

This follows the code structure:

  1. Check and handle invalid arguments (missing: read; too many: exit or just use first). One if statement.
  2. Check and handle invalid user name. One if statement.
  3. Get full name of user. Variable assignment(s) with command substitution.
  4. Check if user is logged on (and output). One if statement, possibly with an else branch.

I don't see a loop in this script, so it will be a number of if-statements and variable assignments.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.