เมื่อใดที่ฉันควรใช้-eqvs =vs==
เช่น
[[ $num -eq 0 ]]
[[ $num = 'zzz' ]]
ฉันสังเกตรูปแบบการใช้-eq(และ-neอื่น ๆ ) สำหรับตัวเลขและ=สตริง มีเหตุผลสำหรับสิ่งนี้และเมื่อใดที่ฉันควรใช้==
เมื่อใดที่ฉันควรใช้-eqvs =vs==
เช่น
[[ $num -eq 0 ]]
[[ $num = 'zzz' ]]
ฉันสังเกตรูปแบบการใช้-eq(และ-neอื่น ๆ ) สำหรับตัวเลขและ=สตริง มีเหตุผลสำหรับสิ่งนี้และเมื่อใดที่ฉันควรใช้==
คำตอบ:
เพราะนั่นคือคำจำกัดความสำหรับตัวถูกดำเนินการเหล่านั้น จากเอกสารการทดสอบ POSIX ส่วน OPERANDS :
s1 = s2
เป็นจริงถ้าสตริง s1 และ s2 เหมือนกัน มิฉะนั้นเป็นเท็จ
...
n1 -eq n2
เป็นจริงถ้าจำนวนเต็ม n1 และ n2 เท่ากับพีชคณิต มิฉะนั้นเป็นเท็จ
==ไม่ได้กำหนดโดย POSIX ก็เป็นส่วนหนึ่งของที่ได้มาจากbash kshคุณไม่ควรใช้==เมื่อคุณต้องการพกพา จากเอกสาร bash - การแสดงออกตามเงื่อนไข Bash :
string1 == string2
string1 = string2
จริงถ้าสตริงเท่ากัน ควรใช้ '=' กับคำสั่งทดสอบเพื่อให้สอดคล้องกับ POSIX
ในวิธีที่ละเอียดยิ่งขึ้นการ
ติดตามลำดับสามารถช่วยได้:
gnu:~$ [ sam -eq sam ]
bash: [: sam: integer expression expected
gnu:~$ echo "Exit status of \"[ sam -eq sam ]\" is $?."
Exit status of "[ sam -eq sam ]" is 2.
gnu:~$ [ 5 -eq 5 ]
gnu:~$ echo "Exit status of \"[ 5 -eq 5 ]\" is $?."
Exit status of "[ 5 -eq 5 ]" is 0.
gnu:~$ [ 5 = 5 ]
gnu:~$ echo "Exit status of \"[ 5 = 5 ]\" is $?."
Exit status of "[ 5 = 5 ]" is 0.
gnu:~$ [ sam = sam ]
gnu:~$ echo "Exit status of \"[ sam = sam ]\" is $?."
Exit status of "[ sam = sam ]" is 0.
gnu:~$ [ 5 == 5 ]
gnu:~$ echo "Exit status of \"[ 5 == 5 ]\" is $?."
Exit status of "[ 5 == 5 ]" is 0.
gnu:~$ [ sam == sam ]
gnu:~$ echo "Exit status of \"[ sam == sam ]\" is $?."
Exit status of "[ sam == sam ]" is 0.
gnu:~$ (( 5 == 5 ))
gnu:~$ echo "Exit status of \"(( 5 == 5 ))\" is $?."
Exit status of "(( 5 == 5 ))" is 0.
gnu:~$ (( sam == sam ))
gnu:~$ echo "Exit status of \"(( sam == sam ))\" is $?."
Exit status of "(( sam == sam ))" is 0.
gnu:~$ ( sam = sam )
The program 'sam' is currently not installed. You can install it by typing:
sudo apt-get install simon
gnu:~$ echo "Exit status of \"( sam = sam )\" is $?."
Exit status of "( sam = sam )" is 127.
gnu:~$ ( 5 = 5 )
5: command not found
gnu:~$ echo "Exit status of \"( 5 = 5 )\" is $?."
Exit status of "( 5 = 5 )" is 127.
gnu:~$ ( sam == sam )
The program 'sam' is currently not installed. You can install it by typing:
sudo apt-get install simon
gnu:~$ echo "Exit status of \"( sam == sam )\" is $?."
Exit status of "( sam == sam )" is 127.
gnu:~$ ( 5 == 5 )
5: command not found
gnu:~$ echo "Exit status of \"( 5 == 5 )\" is $?."
Exit status of "( 5 == 5 )" is 127.
จากman test:
-eq ฯลฯ
ถ่ายทอดไปสู่การทดสอบทางคณิตศาสตร์ อาร์กิวเมนต์ต้องเป็นตัวเลขทั้งหมด (อาจเป็นค่าลบ) หรือนิพจน์พิเศษ `-l STRING 'ซึ่งประเมินความยาวของ STRING
STRING1 = STRING2
True if the strings are equal.
STRING1 == STRING2
True if the strings are equal (synonym for =).
ดังนั้น=และ==คำพ้องความหมาย
สัญลักษณ์=ใช้สำหรับการเปรียบเทียบสตริงในขณะที่-eqสำหรับการเปรียบเทียบจำนวนเต็ม ทั้งสองทำงานร่วมกับและtest [...]หากคุณกำลังใช้bashกับไวยากรณ์[[...]]คุณสามารถใช้==สำหรับการเปรียบเทียบสตริง นอกจากนี้ในการทุบตี=และ==มี[[...]]ผลงานpatternsมากเกินไป [[ $x == y* ]](ตัวอย่างเช่น