LinuxQuestions.org
Help answer threads with 0 replies.
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 03-03-2014, 02:46 AM   #1
mirage1993
Member
 
Registered: Feb 2014
Location: China
Distribution: CentOS6.4
Posts: 51

Rep: Reputation: Disabled
Why leave a blank character at the both sides of the "="


Code:
#!/bin/sh
echo "enter password"
read pw
while [ "$pw" != "123" ]
do
        echo "enter password"
        read pw
done

exit 0
this is a simple example to enter password.
but ,why should leave a blank character at the both sides of the
Code:
!=
if change like this
Code:
while [ "$pw"!="123" ]
,it will go wrong.

but I can't understand why?
 
Old 03-03-2014, 02:49 AM   #2
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
The space is considered the delimiter between the three "parameters".
 
Old 03-03-2014, 03:38 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,508

Rep: Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890Reputation: 2890
Maybe if you consider just the equals sign it will be clearer:
Code:
x=5              # this is an assignment of the value 5 to x

[[ $x = "5" ]]   # this is a test to see if the variable x is to the value 5

[[ $x="5" ]]     # if we ignore the issue of having the $ on the left side which will cause issues, you are still telling bash to assign a value due to no whitespace around '='
 
Old 03-03-2014, 03:58 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
The main issue is that bash cannot distinguish between string concatenation and test operators if not forced to separate items by means of something (IFS comes in handy). The test
Code:
[[ "$pw"!="123" ]]
is always true since not empty strings are evaluated as such. But what chances have you to tell bash about the following test, if items are not properly separated?
Code:
[[ "$pw"=="123" =~ == ]]
 
Old 03-04-2014, 08:19 AM   #5
mirage1993
Member
 
Registered: Feb 2014
Location: China
Distribution: CentOS6.4
Posts: 51

Original Poster
Rep: Reputation: Disabled
thanks all the people!!!
 
  


Reply

Tags
shell script


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
"Zero" drive with character other than "\x00"? fmillion Linux - General 7 04-12-2013 01:06 PM
Perl: how to save an e-mail attachment on disk keeping the "&" character (no "%26"!!) d1s4st3r Programming 5 09-29-2010 09:30 PM
"copy -urv": any way to leave files out? newbiesforever Linux - General 1 11-16-2009 07:50 PM
Mandrake 10 prints blank page with "a" character on startup with HP LaserJet1200 mandrake_2k4 Mandriva 2 04-12-2004 05:06 PM
Mandrake 10 prints blank page with "a" character on startup with HP LaserJet1200 mandrake_2k4 Linux - Hardware 0 04-10-2004 11:19 AM


All times are GMT -5. The time now is 09:00 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