0

I'm new here and I'm trying to learn some Bash. I need to write a script that will print all of users from etc/passwd in seperated lines from one, selected group defined as a $1 parameter.

When group does not exist, the proper message is displayed. Same for wrongly entered numbers. I manage to do this, which do all the job with messages, but I just can't find the way to write users from one group.

Example:

myscript.sh 1000

Output:

User1

User2

User3

Here's the bash file:

#!/bin/bash
awk -F':' '{ print $3 }' /etc/passwd | sort > list.txt
re='^[0-9]+$'

if grep -Fxq "$1" list.txt
then
**echo "don't know what to do here"** 
else

if ! [[ $1 =~ $re ]] ; then
   echo "Wrong number"
else
    echo "Group does not exist"
fi
fi
baduker
  • 19,152
  • 9
  • 33
  • 56
Cassy_1
  • 73
  • 1
  • 7
  • you could do all of this with `awk` alone: `awk -F: -v group=$1 '$3==group' /etc/passwd` to print all those users with the given group, etc. – fedorqui Feb 01 '15 at 23:08
  • I think it doesnt work properly or I just can't use it properly in my script. I think it only print one user with selected user ID, not all the users from one group with the given Group Id. – Cassy_1 Feb 01 '15 at 23:46

0 Answers0