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 |
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:"; |
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 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. |
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. |
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 |
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 |
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. |
You can still have the 3 options you were looking for as well:
Code:
if [[ $MYID == $USER ]] |
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. |
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 |
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 |
Glad you are progressing Dave. Please use [code][/code] tags around your code to keep formatting and make it easier to read.
|
Quote:
|
All times are GMT -5. The time now is 02:19 AM. |