1

I am trying to work on a bash script that checks for a username in the argument of the script and then outputs the relevant lines from the /etc/passwd and /etc/group files (not the /etc/shadow file). Currently, I am utilizing a if then else loop to check the contents of the /etc/* directory and output the relevant information. My intention was to output simple text line if a match user is not found in the two files, thus a null value. However, it is outputting information that is totally incorrect for what I am looking for.As a new user to BASH, and linux is general, I am sure there are some glaring issues right away. However, I am trying to learn.

Any help with the code of my script or a point in the right direction would be greatly appreciated. Thank you.

#! /bin/bash                                                              


USERLOOK='grep -h $USERID ~/etc/* | grep :x:'                       

grep $1 ~/etc/*                                                     
if [ -z $1 ]; then                                                        
    echo "User not found."                                            
else                                                                      
    echo "$USERLOOK"                                                  
fi                                                                        
exit 0
4
  • Question not clear? You wish to enter the username as an argument to script say ./script user_to_check? Commented Jul 17, 2016 at 19:31
  • You should read up on quoting and command substitution. Commented Jul 17, 2016 at 19:31
  • @sjsam Yes, I want to have the script function where I input ./script user_to_check. If the username is found, I want to output all lines where it was found, that was my reasoning for using grep. However, if the username was not found, I wanted to echo that. Commented Jul 17, 2016 at 19:47
  • @Cyrus Thanks for the link. I have tried spellcheck a couple of times with various iterations of this script. While some return no errors, the output was never really what I was looking for. Thanks for the suggestion though. Commented Jul 17, 2016 at 19:57

3 Answers 3

2

Finds any lines in /etc/passwd or /etc/group that contain the inputted username:

#!/bin/bash

USERLOOK=$(grep -h "$1" /etc/passwd /etc/group)

if [ -z "$1" ] || [ -z "${USERLOOK}" ]; then
    echo "User not found."
else
    echo "$USERLOOK"
fi
Sign up to request clarification or add additional context in comments.

6 Comments

the :x: won't work for /etc/shadow, and anyway you should anchor the search to the beginning of the line, e.g. "^$1:"
You may wish to replace the legacy backticks with $()
Thank you for the response. This definitely works better that what I initially have, however, I was trying to return all lines from both files that contain the username that was input in the script arguments. Also, I do see some syntax errors that I made based on this script, so thank you for that as well.
Also, am I correct in thinking that the first line of the IF statement is just looking for a blank argument, or would that function the same if the username is not found in either of the two files?
Okay, I saw the :x: in your question and figured you were looking for only those lines, I've updated the script to include all lines and also check for empty input.
|
1

I want to have the script function where I input ./script user_to_check. If the username is found, I want to output all lines where it was found... However, if the username was not found, I wanted to echo that.

It can be as simple as

#!/bin/bash
grep "^${1}:" /etc/passwd   /etc/group
[ $? -ne 0 ] && echo "User : ${1} not found"

As the user name appears in the beginning in both /etc/passwd & /etc/group we placed a ^ in grep to match stuff at beginning and by tradition a : appears just after the username.

Run the script as

./script 'username'

7 Comments

use "^$1:" else you could get a user foobar when calling ./script foo
Thank you for the response. This does return the first username value. However, I am trying to return all lines the contain the username that was input, thus my initial reasoning for utilizing grep.
@mona_sax Once again, thanks for the help. I just typed out that script with a username that I know is present in the file, but it still returns a not found. In another response, I clarified a little more with what I was trying to do with this script: " I want to have the script function where I input ./script user_to_check. If the username is found, I want to output all lines where it was found, that was my reasoning for using grep. However, if the username was not found, I wanted to echo that."
@NewBashUser226 : See the update please. Pls gimme n update if this helped?
@mona_sax Thanks for the help. Your script actually helped me research a couple more topics and I will look into your methodology versus mine as well. However, I believe the script that jchavannes posted answers my question and utilizes code that is closer to my new user skill level.
|
0

Stop, you're thinking about this all wrong.

A UNIX shell is an environment from which to call UNIX tools with a language to sequence those calls, that is all. The general purpose UNIX tool to manipulate text is awk. So if you need to look for text in a file and have control logic to do anything with it, that should be an awk script, not a shell script. Shells role is to just call awk, something like this:

awk -v user="$1" '
$0 ~ user { line = $0 }
END { print (line != "" ? line : "User not found") }
' /etc/passwd /etc/group

but note that it's trivial with awk to focus the search on just one field, even a different field for each file, unlike how difficult that is in general with grep.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.