LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-06-2011, 10:26 AM   #1
casperpache
LQ Newbie
 
Registered: Oct 2009
Location: Aberystwyth
Distribution: Redhat 5
Posts: 23

Rep: Reputation: 0
Smile script help (Case & IF statement syntax), easy to answer if you know shell scripting.


Hi all,
Im a bit stuck with a simple script that im practicing with... im trying to get my head around some simple scripting.

Basically i want to write a script that will work as below:
It will ask the user to enter their userid.
If its correct it will say 'Correct'
If its the wrong id (But the ID exists in /etc/passwd) then it will say 'incorrect'
If its not a valid userid it will say 'doesnt exist'.

What i have so far is:

-------------------------------------------------------------
#!/bin/bash
#
#This is used to test user input using a case statement.
#
echo "Enter ID:";

read loginid

case $loginid in

# If the user enters their own userid they will get a comment saying 'correct'
$LOGNAME) echo "Correct";;

# If they enter another users ID (That exists in the /etc/passwd), then they get a message to say 'incorrect userid used'.
Im not sure what i need to do here to get it to check the /etc/passwd to see if the input exists in there.... im guessing some kind of simple 'if' statement to say ---- "if 'input' exists in /etc/passwd then echo "incorrect userid used"

# If its entered incorrectly and not a valid ID then they will get this comment.
Again, im guessing this part would be similar to te above option.....
*) echo "not a valid ID";;

esac

---------------------------------------------------------

Ive managed to work out how the basic Case staements work, but to add a conditional statement based on the input, i just cant figure out.
I have tried looking on google, and found some help but it doesnt really explain how i check the input against the /etc/passwd.

Any help and explanation would be appreciated.

Thanks in advance

Dave
 
Old 05-06-2011, 10:54 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,877

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
You could probably do it with minimum need to resort to reading /etc/passwd. The users ID is stored as the environment variable UID. If you give the command "echo $UID" you will get your user id in return.

So,
Code:
echo "Enter ID:";
read MYID
 case 1 if $MYID = $UID then "That's correct"
 case 2 if $MYID !e then "That user doesn't exist" *read 
/etc/passwd to see if MYID is there. 
it's the third field in the record. 
You may have to resort to using awk 
to parse the record for the third 
field, or cut, or probably other means.*
 case 3 if $MYID != $UID then "That's the wrong ID"
 esac
That a basic outline (as in pseudocode). The syntax is not correct for a shell script. Notice that cases 1 and 3 don't need to read /etc/passwd. You can download the Advanced Bash-scripting Guide and the Bash Beginners Guide for more info the fill in the code.

Last edited by bigrigdriver; 05-06-2011 at 10:58 AM.
 
1 members found this post helpful.
Old 05-06-2011, 11:03 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Well I am not sure you are using the correct construct. Case requires an expression equivalence for each option whereas you wish to search a file.

As an example of possible solution in an if, imagine I enter my userid as 'grail' and wish to search the /etc/passwd file to see if it exists:
Code:
if grep -q $loginid /etc/passwd
If grep finds something you perform the if and if not you exit or go to the else.

So it looks to be more of an if, elif, else scenario - although someone else might have an idea on how to get it in a case.
 
1 members found this post helpful.
Old 05-06-2011, 11:48 AM   #4
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
I understand that this is a simple project to learn bash scripting, but there's a fundamental flaw in the logic involving this script... there's no such thing as a case where the userid matches but doesn't exist. The user input will be compared against $USER (not $UID, as stated above, that returns the UID in number form, not the username), and obviously that value exists in /etc/passwd, or the user would have never gotten to that point.

Also, CASE is typically used to take different actions based on different user input. For instance, you might make one script that does a start, stop, or restart of a subsystem, and build your CASE constructs around which option is entered at the command line.

This exercise here is best served by a simple IF - ELSE construct.
 
Old 05-06-2011, 01:52 PM   #5
casperpache
LQ Newbie
 
Registered: Oct 2009
Location: Aberystwyth
Distribution: Redhat 5
Posts: 23

Original Poster
Rep: Reputation: 0
Thanks for the reply guys,

This initially started out as a play around with a case statement to start learning scripts and it eventually grew into a few other things.
Thought i was being clever trying to create it from scratch but i failed and needed help.

The actual script ive created is a little bigger than what ive posted but its got all kinds of crazy comments so i narrowed it down to the point i needed to get working.

If this scenario cant be scripted then ill try to think of some other scenario to play with.

Thanks again

Dave
 
Old 05-06-2011, 02:00 PM   #6
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
It's not that this scenario can't be scripted, it's just that CASE isn't the right way to handle it.

This is all you need:

Code:
#!/bin/bash
#
echo "Enter ID:"
read MYID
if [ $MYID = $USER ]; then echo "Correct"
   else echo "Incorrect"
fi

Last edited by SL00b; 05-06-2011 at 02:01 PM.
 
1 members found this post helpful.
Old 05-06-2011, 05:43 PM   #7
casperpache
LQ Newbie
 
Registered: Oct 2009
Location: Aberystwyth
Distribution: Redhat 5
Posts: 23

Original Poster
Rep: Reputation: 0
aah ok, i had read through all replies and must not have took notice of the last part of your comment.... I will give that a try and see how i get on.

I appreciate the time you have taken to look at this.
Your way looks a lot easier than what ive done, but doesnt completely achieve what i was trying to achieve but is still a better way for a simpler result.

Ill post my script on monday so you can have a laugh and see what i was trying to achieve overall.
 
Old 05-07-2011, 02:11 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
You can still have the 3 options you were looking for as well:
Code:
if [[ $MYID == $USER ]]
then
    echo Correct
elif grep -q "^$MYID:" /etc/passwd
then
    echo This is not your ID
else
    echo The ID entered does not exist
fi
 
1 members found this post helpful.
Old 05-07-2011, 03:52 PM   #9
casperpache
LQ Newbie
 
Registered: Oct 2009
Location: Aberystwyth
Distribution: Redhat 5
Posts: 23

Original Poster
Rep: Reputation: 0
Smile

ok great, thanks for that.... I havent got as far as 'elif' yet.... Im a complete newbie startin from scratch.
Ill update on monday to how it goes.
 
Old 05-09-2011, 02:09 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Links for the docs mentioned in post#2
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Also recommended
http://rute.2038bug.com/index.html.gz


Enjoy
 
1 members found this post helpful.
Old 05-09-2011, 07:09 AM   #11
casperpache
LQ Newbie
 
Registered: Oct 2009
Location: Aberystwyth
Distribution: Redhat 5
Posts: 23

Original Poster
Rep: Reputation: 0
All, thank you for your help with this, its been a good learning curve and appreciate all of your replies.

I just didnt know where to start looking really but will save the links provided and use them for my ongoing learning.

Just to show you what i was actually doing, here is the jokey script ive been playing with...... its not perfect but its my first script so im pleased with it.

----------------------------

#!/bin/bash
#
# This is used to test user input using a CASE statement.... and also IF, ELIF and ELSE statements.
#
# Get the user to enter their login id, if true then be nice and give an option to check the date or login info,
# If not then give bad message and allow different options.
#

echo "Enter your own Login ID... this will give you access to a personal area setup just for you."
echo
echo "Do not enter anyone else's Login ID as that would give you access to their secret files"
echo
echo "Enter ID:";
echo
read loginid
echo

if [[ $loginid == $USER ]]
then
# If the user enters their own userid they will get this comment:-
echo "Well done, you can now choose one of the below options."
echo

# Then it checks the /etc/passwd to see if the ID exists.
elif grep -q "^$loginid:" /etc/passwd

echo

then
# If they enter another users ID, then they get the below message.:-
echo " You sneaky little git, you really didnt think id allow you to access another users secret files did you?"

else
echo

# If its entered incorrectly then they will get this comment:-
echo "You fool!!! The input doesnt even match an ID, go back and try again, but this time take your time."

fi


if [[ $loginid == $USER ]]

then

echo "1. Date"
echo "2. Login Info"

read option

case $option in
1) echo $(date);;
2) finger $(logname);;
*) echo "You accessed the script so atleast choose a bloody option. ";;
esac
else

echo "1. What are you?"
echo "2. My finger slipped and i want to try again."

read option1

case $option1 in
1) echo "I am a fool and i dont know my arse from my elbow." ;;
2) ./case_script.sh ;;
*) echo "You are worse than first envisioned and cant even choose between option 1 and 2.... go home!!" ;;
esac
fi

---------------------

Thanks again, have a good week.

Dave
 
Old 05-09-2011, 08:16 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Glad you are progressing Dave. Please use [code][/code] tags around your code to keep formatting and make it easier to read.
 
Old 05-09-2011, 08:18 AM   #13
casperpache
LQ Newbie
 
Registered: Oct 2009
Location: Aberystwyth
Distribution: Redhat 5
Posts: 23

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by grail View Post
Glad you are progressing Dave. Please use [code][/code] tags around your code to keep formatting and make it easier to read.
Thanks, and i will do in the future.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ignore case in if statement shell scripting saiteju Programming 2 02-27-2010 02:49 AM
Korn Shell Scripting case statement help!! wfdeng117 Programming 3 07-22-2008 11:51 AM
How do I use an If statement within a case in a Shell script?? crazygyhrous Programming 7 01-03-2006 07:41 AM
Case Statement With Aix Ksh Scripting ']['HeBroken AIX 2 02-09-2005 01:44 PM
KSH Scripting case statement..... ']['HeBroken Programming 1 12-10-2004 11:38 AM


All times are GMT -5. The time now is 02:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration