انجمنهای فارسی اوبونتو
کمک و پشتیبانی => برنامهسازی => نویسنده: Geek در 07 تیر 1392، 04:05 بظ
-
سلام دوستان
در tutor بش اومده که کی ورد ]] تفکیک کلمه رو برای آرگومان هاش اجازه نمیده اما اگر بنویسم:
$ a=geek
$ b=geek
$ if [[ my name is $a = my name is $b ]] ; then echo yes ; fi
خطا می گیره؟
اگر کسی برام تبیین کنه ممنون میشم.
-
$ a=geek
$ b=geek
$ if [[ "my name is $a" = "my name is $b" ]] ; then echo yes ; fi
-
$ a=geek
$ b=geek
$ if [[ "my name is $a" = "my name is $b" ]] ; then echo yes ; fi
اول ممنون... ;D
در واقع با دابل کوتیشن تفکیک کلمه انجام دادیم که اینو می دونستم...
اما در راهنما ذکر شده که :
Another feature of [[ helps us in dealing with parameter expansions:
$ [[ $me = $you ]] # Fine.
$ [[ I am $me = I am $you ]] # Not fine!
-bash: conditional binary operator expected
-bash: syntax error near `am'
This time, $me and $you did not need quotes. Since [[ isn't a normal command (like [ is), but a shell keyword, it has special magical powers. It parses its arguments before they are expanded by Bash and does the expansion itself, taking the result as a single argument, even if that result contains whitespace. (In other words, [[ does not allow word-splitting of its arguments.) However, be aware that simple strings still have to be quoted properly. [[ can't know whether your literal whitespace in the statement is intentional or not; so it splits it up just like Bash normally would. Let's fix our last example:
$ [[ "I am $me" = "I am $you" ]]
من نمی تونم خوب بفهمم...اول میگه که خودش میتونه آرگومان هاش رو تجزیه و تحلیل کنه قبل اینکه بش بتونه اونا رو تفکیک کنه واونا رو به عنوان یه آرگومان تکی قبول میکنه اما بعد میگه که جملات ساده همچنان باید در دابل کوتیشن قرار بگیرن!!!....آخه چطوری؟!!!!........
-
درسته، آرگومانها رو میتونه تجزیه و تحلیل کنه، ولی یک متن تنها یک آرگومان محصوب میشه و برای اینکه مشخص کنیم متن از کجا شروع میشه و کجا تموم میشه، باید با " اول و آخرش رو مشخص کنیم.
-
پس با این تفسیر عبارت زیر در حکم یه جمله هست درسته؟
I am $me = I am $you
-
یک String باید همیشه در بین دو " قرار بگیره.