ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello,
I'm new to bash scripting, I have a need to write a script which will take user name and password as input validate the inputs and check whether the username/password combination can really login to the system and if authentication is successful then it has to run a set of other commands roughly about 200 different scripts but if authentication fails then it has ask for correct user name and password again and do the above steps again.
If some one can help me with It would be very much helpful.
Could you explain the "actual" end goal (i.e. what you are actually trying to achieve)? It sounds like you're trying to reinvent the wheel, and you actually want the user to ssh into the system, or something like that.
this will be done by the system automatically, just execute su - <username>. It will ask for pw and based on ~/.profile or ~/.bashrc can execute some programs...
Hi Snark,
I have a load file which has about 200 commandline entries asking for user name and password, I figured out a way to input user name and password to those script file but instead of asking 200 times I want the user to enter username and password only once.
The catch is we have different servers and user authentication is done via ldap, and I don't have access to do an ldap.
So instead I want to my script to check whether the given combo of username and password works in the given system.
If yes I would take those inputs to run my load file and execute if the authentication fails then I want the script to echo error and ask for valid user name/password combo.
A simple conditional statement is what I'm looking for.
Any way thanks for the response.
Right... I'm still not 100% convinced this is the best way to do it, but meh, this should work, and it's not immediately obvious you're doing something silly:
Code:
#!/usr/bin/env bash
success=0
while [[ $success -eq 0 ]]; do
echo -n "username:"
read uname
su $uname -c "/path/to/file_with_commands.bash"
if [[ $? -eq 0 ]]; then
success=1
fi
done
'su' returns exit code 125 for incorrect password, but su itself pauses for a second or two and prints an error message so it didn't seem necessary to test for that in the script itself.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.