0

This is more directed to learning about BASH rather than creating a specific code.

---Problem: Write a Bash script that takes a list of login names on your computer system as command line arguments, and displays these login names, full names and user-ids of the users who own these logins (as contained in the /etc/passwd file), one per line. If a login name is invalid (not found in the /etc/passwd file), display the login name and an appropriate error message. ---

If I needed to create a code to fulfill this problem could I do it using a "choice" list like this:

 read -p "Enter choice:  " ch
        if [ "$ch" = "1" ]; then
        function_1
        else
        if [ "$ch" = "2" ]; then
        function_2
        else
        if [ "$ch" = "3" ]; then
        function_3
        else
        if [ "$ch" = "4" ]; then
        function_4
        else
        if [ "$ch" = "5" ]; then
        function_5
fi x5

or would it have to be completed using a grep and test method where by the user read input must be taken into variables Name1 Name2.... NameN and are tested to the ect/passwd file via grep command.

#!/bin/bash
# pgroup -- first version
# Fetch the GID from /etc/group
gid=$(grep "$̂1:" /etc/group | cut -d: -f3)
# Search users with that GID in /etc/passwd
grep "^[^:]*:[^:]*:[^:]*:$gid:" /etc/passwd | cut -d: -f1`enter code here`

Please explain the best you can with some generic code. I am still learning the basics and testing different ideas. I apologize if these are very vague concepts, I am still getting the hang of BASH.

Rep
  • 29
  • 4

2 Answers2

0

You would:

  1. Accept (read) from the user the username,

  2. Check if the username exists by retrieving the record using grep,

  3. If positive result (i.e. you got data), display it as needed,

  4. Otherwise, display an error message (or whatever you need).

You are almost there (got how to get input, how to search using grep). What you need is to get the result of the grep into a variable that you can check. You may try something like:

Result=$(grep....)

and then check the contents of Result.

FDavidov
  • 3,202
  • 5
  • 19
  • 48
  • Ok so check input to -> variable name1 (which equals grep test that user input) But how do I set the variable to the user id before hand, do I just make a variable for however many user ids I could need? What if I ended up needing to check 30 users, would i need 30 variables? – Rep Nov 15 '16 at 13:56
  • Your `read` command assigns the list of users to your variable `ch`. You can then use [this](http://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash) example on how to split your string and loop through the returned values. – FDavidov Nov 15 '16 at 14:07
  • One addition: If the list of users is provided within quotes, it is received as a single parameter and hence you need to split it. If no quotation characters are used, you can loop through using the example [here](http://stackoverflow.com/questions/255898/how-to-iterate-over-arguments-in-bash-script). – FDavidov Nov 15 '16 at 14:10
0

To me it looks like you're missing an important part of the problem: the names are passed as arguments to the script:

./script.sh name1 name2 name3

If this is the case, then a loop would be the most appropriate thing to use:

for login; do
    printf '%s: %s\n' "$login" "$(grep "^$login" /etc/passwd)"
done

Note that a for loop iterates over the list of arguments passed to the script $@ by default.

Tom Fenech
  • 65,210
  • 10
  • 85
  • 122