Please enter a username: student
Now enter your membership: root
Membership is valid!
ubuntu@user:~$ ./membership.sh
Please enter a username: doesnotexist
Now enter your membership: alsodoesnotexist
Both are not found - why are you even asking me this?
ubuntu@user:~$ ./membership.sh
Please enter a username: student
Now enter your membership: doesnotexist
One exists, one does not. You figure out which.
ubuntu@user:~$ ./membership.sh
Please enter a username: student
Now enter your membership: sudo
Membership invalid but available to join.
I need to convert the conversation above to a bash script. This is what I come up with:
banner="Welcome to the sexy hackers Club"
now="$(date)"
computer_name="$(hostname)"
echo "Date: $now"
echo "$banner: $computer_name"
echo " "
read -p "Please enter a user name: " USER
read -p "Now enter a group: " MEMBERSHIP
if [[ $USER == 'student' && $MEMBERSHIP == 'root' ]];
then
echo "Membership valid!"
else
echo "Both are not found"
if [[ $USER == 'student' || $MEMBERSHIP == 'root' ]];
then
echo "One exist, one does not"
if [[ $USER == 'student' || $MEMBERSHIP == 'sudo' ]];
then
echo "Membership invalid but available to join."
fi
fi
fi
The first, and second steps work perfectly but the problem is with the 3rd and 4th
conversations. if I add user student
and a wrong membership it should say One exists, one does not
and in the 4th conversation if I add student
but sudo as membership, it should say Membership invalid but available to join.
The problem with my scrip is that the last two conversation gets mixed, I can’t find a way to make them unique. I need to create a short Bash script that will validate a user’s membership in a specified group. This script should not take any arguments and, instead, should prompt the user to enter a username
and a membership
. This script should first check to see if the username
and membership
are found on our system to get Membership valid!. If BOTH ARE NOT FOUND, the script should respond Both are not found but If ONLY ONE IS FOUND, it should respond One exists, one does not. If BOTH ARE FOUND, it should also check to see if the user is a member and If the USER IS A MEMBER OF THE GROUP, the script should respond Membership valid!
otherwise, it should respond Membership invalid but available to join
. To be clear, the script should initially prompt twice for user input
(the prompt does not matter) and then only respond once with one of the four specified responses.
3
Answers
This is the correct answer to the script. Four different answers to four different actions within the script.
Whitespace would make your code a lot easier to understand. This is what you have with indentation added:
The output mixes because in the
else
case you always print "Both are not found" but then you could additionally print the other statements as well because they’re all part of the same conditional branch.Instead of
else
, you should be usingelif
.Continuing from my comment, you can’t just check that the user and group exists on the system and conclude the membership is valid, you have to validate the user is a member of the entered group. There are several ways to do this, but the easiest is just to validate the user and group exist on the system by checking the return of
getent
called onpasswd
andgroup
databases.The to determine membership, just
grep $user /etc/group
to generate a list of the groups the member belongs to, and parse the output withawk
to verify that one of the groups returned matches the membership (group) entered.Don’t use UPPERCASE variables names, those are generally reserved for environment variables or internal shell variables. Lowercase variable names are fine.
Putting it altogether, you could do something similar to the following:
Example Use/Output
Check if I am a member of the
wheel
group (I am):Check if
foo
is a member ofwheel
(there is no userfoo
on system):Check if I am a member of the
foo
group (there is nofoo
group on the system):Check if I’m a member of the
audio
group (I’m not, but it is a valid group):That covers all four possible cases (1) valid member; (2) user doesn’t exist; (3) group doesn’t exist; and (4) user is not a member of valid group.