Golfreeze.packetlove.com: Life style of Golfreeze Canon400D Family kammtan.com Jazz Freebsd Unix Linux System Admin guitar Music

All about unix linux freebsd and FAQ for Packetlove.com Web hosting , Mail hosting , VoIP + IP PBX server => command , shell script ,tool , crontab => Topic started by: golfreeze on เมษายน 29, 2016, 11:00:02 PM

Title: ความแตกต่างระหว่าง && และ ; ในการรัน shell script
Post by: golfreeze on เมษายน 29, 2016, 11:00:02 PM
In the shell, && and ; are similar in that they both can be used to terminate commands.
The difference is && is also a conditional operator. With ; the following command is always executed, but with && the later command is only executed if the first succeeds.

false; echo "yes"   # prints "yes"
true; echo "yes"    # prints "yes"
false && echo "yes" # does not echo
true && echo "yes"  # prints "yes"
Newlines are interchangeable with ; when terminating commands.

สรุปคือ ถ้าใช้ && ขั้น คือต้องรัน command แรกเสร็จแล้วค่อยรันอีก command ต่อ เช่น
true && echo "yes"  # prints "yes"

แต่ถ้า ; ก็คือไม่สนใจว่า ประโยคข้างหน้าจะจริงหรือเปล่าให้ รัน command ที่สองเลย