LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2002, 05:47 PM   #1
xlord
Member
 
Registered: Mar 2002
Posts: 75

Rep: Reputation: 15
shell script


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
 
Old 10-31-2002, 06:18 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 10-31-2002, 06:20 PM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
hmm, that read a bit harsh ehh? sorry!

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.
 
Old 10-31-2002, 06:23 PM   #4
xlord
Member
 
Registered: Mar 2002
Posts: 75

Original Poster
Rep: Reputation: 15
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
 
Old 10-31-2002, 06:31 PM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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
 
Old 10-31-2002, 06:36 PM   #6
xlord
Member
 
Registered: Mar 2002
Posts: 75

Original Poster
Rep: Reputation: 15
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?

btw thanx for your help!
 
Old 10-31-2002, 06:41 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
run " ls -l `which [` " you wouldn't expect something like
Code:
 if testblah == blah
to work now would you?
 
Old 10-31-2002, 08:36 PM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 10-31-2002, 09:05 PM   #9
xlord
Member
 
Registered: Mar 2002
Posts: 75

Original Poster
Rep: Reputation: 15
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
i cant see to understand why
 
Old 10-31-2002, 09:17 PM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
execute as "sh -x <scriptname>"
 
Old 10-31-2002, 09:21 PM   #11
xlord
Member
 
Registered: Mar 2002
Posts: 75

Original Poster
Rep: Reputation: 15
ok i did that and this is what i got..

Code:
[xlordt@local xlordt]$ sh -x test
+ echo 'please enter second passwd'
please enter second passwd
+ pass=*********
+ read pass

+ '[' pass == ']'
test: [: pass: unary operator expected
+ '[' pass '!=' ']'
test: [: pass: unary operator expected
+ echo 'Please enter pass'
Please enter pass
 
Old 11-01-2002, 05:07 AM   #12
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
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

Last edited by Mik; 11-01-2002 at 05:11 AM.
 
Old 11-01-2002, 07:53 AM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.

If you haven't got money to buy a book, at least use the ABS: http://www.tldp.org/LDP/abs/html/
 
Old 11-01-2002, 01:33 PM   #14
xlord
Member
 
Registered: Mar 2002
Posts: 75

Original Poster
Rep: Reputation: 15
ahh yes sorry i was abit waisted last night.. but thanx i finaly got it working all =)
 
Old 11-01-2002, 01:49 PM   #15
dogmeat
Member
 
Registered: Jul 2002
Location: Wyoming
Distribution: Mandrake9.0
Posts: 48

Rep: Reputation: 15
wasted on what
 
  


Reply



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
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:55 AM.

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
Open Source Consulting | Domain Registration