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.
For some reason i cant get this code to work perfectly can anyone help..
its bash programming
Code:
echo "please enter second passwd";
pass="******************"
read pass
if ["$pass" == "pass"];
then
echo "passwd correct you may procced"
else
if ["pass" -eq "$pass"]; then
echo "pass incorrect"
fi
fi
well it's a bit of a mess really. -eq is an integer comparison, and you are using strings, so that won't work. and if it did work then it would be the same as the first if statement. you have a bizarre half nested if block.. by the else statement you already know it is not matching, yet you check it again? and of course you will get a syntax error as there is no space between [ and ] and the quotes. [ is a program, not just a bracket. i'd strongly suggesting buying a bash book and taking a bit more time.
also though... you don't need all those semi-colons, that's just to continue on the same line, not like c / java. and you don't need to put quotes around your echo statements. I also get the impression that you're going to hardcode a password into a plain text bash file? i *seriously* hope this is just to learn bash....
Last edited by acid_kewpie; 10-31-2002 at 06:26 PM.
No that is ok harsh is good sometimes.. but im a newbie at shell scripting.. i really dont have money to get a book yet.. but if you can help me fix this... or if you can fix my code.. with were i have the errors.. then i can understand more.. if ok with you
it's normally against my religion to solve peoples code for them, but...
Code:
echo please enter second passwd
read pass
if [ $pass == pass ]
then
echo passwd correct you may procced
# DO SOMETHING
else
echo pass incorrect
# DO SOMETHING ELSE
fi
Hhmm that is weard ... as far as i know.. from the tuts that i have read.. i see
if [ "blah" =="blah" ] but since i removed them and added another space in [] now it works.. but why... when i read a tut in some site...(will find site again) it says that it is the correct way?
Uhm, not to intrude here, but may I warn agains unquoted variables since the shell usually separates everything thrown at it by space?: if [ "$pass" = "pass" ]; then
And you won't need a double equal sign.
well its fine this way.. just one more prob.. it keeps saying pass is not correct.. for some reason.. errr its really anoying check it out
Code:
echo "please enter second passwd"
pass="***********"
read pass
if [ $pass == pass ]
then
echo "passwd correct you may procced"
elif [ $pass != pass ]
then
echo "pass incorrect"
else
echo "Please enter pass"
fi
There is no difference between the last two if branches. $pass is either equal to pass or not, it can't also be something different. If you wanted to check for an empty $pass then you should do something like:
if [ -z "$pass" ]
then
echo "Please enter pass"
fi
If you had listened to my advice and properly quoted variables you would have ended up with "if [ "$pass" = "pass" ]" , which in turn would translate in bashspeak to
'if' '[' '' '=' 'pass' ']'. By not quoting and then providing an empty value for pass you end up with one part where "=" (thats binary operator) cannot operate on.
Like Mik suggested you should test for empty string.
I like to do it like this:
if [ "${pass:?"not_defined"}" = "pass" ]; then echo "passwd correct you may procced"; else echo "Please enter pass"; fi
This way it always gets assigned a (bogoid) value indicating zomesing's vrronkk.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.