Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ออเปอเรเตอร์พื้นฐานใน Shell Scripting


Shell เป็นอินเทอร์เฟซที่โปรแกรมเมอร์ใช้เพื่อรันคำสั่งและโต้ตอบกับระบบปฏิบัติการโดยตรง Shell scripting คือการให้คำสั่งที่ shell สามารถรันได้

ใน shell ก็มีตัวแปรและออเปอเรเตอร์ที่ใช้จัดการตัวแปรเหล่านี้ มีออเปอเรเตอร์พื้นฐาน 5 ประเภทใน shell scripting

  • ออเปอเรเตอร์คณิตศาสตร์ (Arithmetic Operators)
  • ออเปอเรเตอร์เชิงสัมพันธ์ (Relational Operators)
  • ออเปอเรเตอร์บูลีน (Boolean Operators)
  • ออเปอเรเตอร์บิตเวส (Bitwise Operators)
  • ออเปอเรเตอร์ทดสอบไฟล์ (File Test Operators)

ออเปอเรเตอร์คณิตศาสตร์

ออเปอเรเตอร์คณิตศาสตร์ใน shell scripting ใช้สำหรับดำเนินการคณิตศาสตร์/คณิตศาสตร์ทั่วไป มีออเปอเรเตอร์คณิตศาสตร์ที่ถูกต้อง 7 ตัวใน shell scripting −

  • บวก (+) ใช้บวกสองโอเปอรานด์ (ตัวแปร)

  • ลบ (-) ใช้ลบสองตัวแปร (โอเปอรานด์) ใน shell scripting

  • คูณ (*) ใช้คูณสองตัวแปร (โอเปอรานด์) ใน shell scripting

  • หาร (/) ใช้หารสองตัวแปร (โอเปอรานด์) ใน shell scripting

  • มอดุลัส (%) ใช้หาเศษจากการหารของโอเปอรานด์ใน shell scripting

  • ออเปอเรเตอร์เพิ่ม (++) ใช้บวก 1 เข้ากับค่าปัจจุบันของออเปอเรเตอร์

  • ออเปอเรเตอร์ลด (--) ใช้ลบ 1 จากค่าปัจจุบันของออเปอเรเตอร์

ตัวอย่าง

เพื่อแสดงการนำไปใช้ของออเปอเรเตอร์คณิตศาสตร์ใน shell scripting −

a = 32 b = 23    add = $((a + b))    echo sum of a and b is $add    sub = $((a - b))    echo Subtraction of a and b is $sub    mul = $((a * b))    echo product of a and b is $mul    div = $((a / b))    echo division of a and b is $div mod = $((a % b))    echo remainder when a is divided b is $mod ((++a))    echo Increment operator when applied on "a" results into a = $a ((--b)) echo Decrement operator when applied on "b" results into b = $b

ผลลัพธ์

sum of a and b is 55 Subtraction of a and b is 9 product of a and b is 736 division of a and b is 1 remainder when a is divided b is 9 Increment operator when applied on a results into a = 33 Decrement operator when applied on b results into b = 24

ออเปอเรเตอร์เชิงสัมพันธ์

ออเปอเรเตอร์เชิงสัมพันธ์ใน shell scripting นิยามความสัมพันธ์ระหว่างโอเปอรานด์ ค่าที่คืนกลับเป็น true หรือ false ขึ้นอยู่กับออเปอเรเตอร์และโอเปอรานด์ มีออเปอเรเตอร์เชิงสัมพันธ์ที่ถูกต้อง 6 ประเภทใน shell scripting −

  • == operator คือออเปอเรเตอร์ที่เทียบค่าของสองโอเปอรานด์ คืนค่า true ถ้าค่าเท่ากัน และ false ถ้าไม่เท่ากัน

  • != operator คือออเปอเรเตอร์ที่เทียบค่าของสองโอเปอรานด์และตรวจสอบความไม่เท่ากัน คืนค่า true ถ้าค่าไม่เท่ากัน และ false ถ้าเท่ากัน

  • < operator คือออเปอเรเตอร์น้อยกว่า เปรียบเทียบค่าของสองโอเปอรานด์ ถ้าค่าโอเปอรานด์แรกน้อยกว่าตัวที่สอง ออเปอเรเตอร์คืน true มิฉะนั้นคืน false

  • <= operator คือออเปอเรเตอร์น้อยกว่าหรือเท่ากับ ที่เปรียบเทียบค่าของสองโอเปอรานด์ ถ้าค่าโอเปอรานด์แรกน้อยกว่าหรือเท่ากับตัวที่สอง ออเปอเรเตอร์คืน true มิฉะนั้นคืน false

  • > operator คือออเปอเรเตอร์มากกว่า เปรียบเทียบค่าของสองโอเปอรานด์ ถ้าค่าโอเปอรานด์แรกมากกว่าตัวที่สอง ออเปอเรเตอร์คืน true มิฉะนั้นคืน false

  • >= operator คือออเปอเรเตอร์มากกว่าหรือเท่ากับ ที่เปรียบเทียบค่าของสองโอเปอรานด์ ถ้าค่าโอเปอรานด์แรกมากกว่าหรือเท่ากับตัวที่สอง ออเปอเรเตอร์คืน true มิฉะนั้นคืน false

ตัวอย่าง

a = 32 b = 67 if(( $a==$b )) then    echo a is equal to b. else    echo a is not equal to b. fi if(( $a!=$b )) then    echo a is not equal to b. else    echo a is equal to b. fi if(( $a<$b )) then    echo a is less than b. else    echo a is not less than b. fi if(( $a<=$b )) then    echo a is less than or equal to b. else    echo a is not less than or equal to b. fi if(( $a>$b )) then    echo a is greater than b. else    echo a is not greater than b. fi if(( $a>=$b )) then    echo a is greater than or equal to b. else    echo a is not greater than or equal to b. fi

ผลลัพธ์

a is not equal to b. a is not equal to b. a is less than b. a is less than or equal to b. a is not greater than b. a is not greater than or equal to b.

ออเปอเรเตอร์บูลีน

ออเปอเรเตอร์บูลีน หรือที่รู้จักในชื่อ logical operators ใช้สำหรับดำเนินการตรรกะใน shell scripting มีออเปอเรเตอร์ตรรกะที่ถูกต้อง 3 ประเภทใน shell scripting −

  • Logical AND (&&) คำนวณตรรกะ AND ของค่าที่เป็นบูลีน คืนค่า true ถ้าทั้งสองโอเปอรานด์เป็น true มิฉะนั้น false

  • Logical OR (||) คำนวณการดำเนินการ OR แบบตรรกะของโอเปอรานด์บูลีน คืนค่า false ถ้าทั้งสองโอเปอรานด์เป็น false มิฉะนั้น true

  • Logical Not Equal to (!) คำนวณการปฏิเสธของโอเปอรานด์เดี่ยวที่ส่งมา ถ้าค่าของโอเปอรานด์เป็น true คืนค่า false มิฉะนั้น true

ตัวอย่าง

a = true b = false if(($a == "true" & $b == "true" )) then    echo Both are true. else    echo Both are not true. fi if(($a == "true" || $b == "true" )) then    echo Atleast one of them is true. else    echo None of them is true. fi if(( ! $a == "true" )) then    echo "a" was intially false. else    echo "a" was intially true. fi

ผลลัพธ์

Both are not true. Atleast one of them is true a was intially true.

ออเปอเรเตอร์บิตเวส คือออเปอเรเตอร์ที่ดำเนินการบิตเวสบนตัวแปรบิต มีออเปอเรเตอร์บิตเวส 6 ประเภทใน shell scripting −

  • Bitwise AND (&) คือออเปอเรเตอร์ที่ทำการดำเนินการ AND แบบไบนารีบนบิตของโอเปอรานด์ คือ แต่ละบิตของตัวแปรแรกจะถูกดำเนินการกับบิตที่สอดคล้องของตัวแปรที่สอง

  • Bitwise OR (|) คือออเปอเรเตอร์ที่ทำการดำเนินการ OR แบบไบนารีบนบิตของโอเปอรานด์

  • Bitwise XOR (^) คือออเปอเรเตอร์ที่ทำการดำเนินการ XOR แบบไบนารีบนบิตของโอเปอรานด์

  • Bitwise complement (~) คือออเปอเรเตอร์ที่ทำการดำเนินการ NOT แบบไบนารีบนบิตของโอเปอรานด์

  • Bitwise Left Shift (<<) คือออเปอเรเตอร์ที่เลื่อนบิตของโอเปอรานด์ไปทางซ้าย n ครั้ง ตามที่ระบุทางขวาของออเปอเรเตอร์

  • Bitwise Right Shift (>>) คือออเปอเรเตอร์ที่เลื่อนบิตของโอเปอรานด์ไปทางขวา n ครั้ง ตามที่ระบุทางขวาของออเปอเรเตอร์

ตัวอย่าง

a = 14 b = 67 bitwiseAND=$(( a&b )) echo Bitwise AND of a and b is $bitwiseAND bitwiseOR=$(( a|b )) echo Bitwise OR of a and b is $bitwiseOR bitwiseXOR=$(( a^b )) echo Bitwise XOR of a and b is $bitwiseXOR bitiwiseComplement=$(( ~a )) echo Bitwise Compliment of a is $bitiwiseComplement leftshift=$(( a<<1 )) echo Left Shift of a is $leftshift rightshift=$(( b>>1 )) echo Right Shift of b is $rightshift

ผลลัพธ์

Bitwise AND of a and b is 2 Bitwise OR of a and b is 79 Bitwise XOR of a and b is 77 Bitwise Compliment of a is -15 Left Shift of a is 28 Right Shift of b is 33

ออเปอเรเตอร์ทดสอบไฟล์

ออเปอเรเตอร์ทดสอบไฟล์ ใช้ทดสอบคุณสมบัติเฉพาะของไฟล์ บางออเปอเรเตอร์ทดสอบไฟล์มีดังนี้ :

  • -b operator ใช้ตรวจสอบว่าไฟล์ที่ระบุเป็น block special file หรือไม่ ถ้าเป็น block special file ฟังก์ชันคืนค่า true มิฉะนั้น false

  • -s operator คือออเปอเรเตอร์ที่ใช้ตรวจสอบขนาดของไฟล์ที่กำหนด ถ้าขนาดไฟล์มากกว่า 0 คืนค่า true มิฉะนั้น false

  • -r operator คือออเปอเรเตอร์ที่ตรวจสอบว่าการเข้าถึงอ่านเนื้อหาไฟล์ได้รับอนุญาตหรือไม่ ถ้าได้รับอนุญาตอ่าน คืนค่า true มิฉะนั้น false

  • -w operator คือออเปอเรเตอร์ที่ตรวจสอบว่าการเข้าถึงเขียนไฟล์ได้รับอนุญาตหรือไม่ ถ้าได้รับอนุญาตเขียน คืนค่า true มิฉะนั้น false

  • -x operator คือออเปอเรเตอร์ที่ตรวจสอบว่าการเข้าถึงรันไฟล์ได้รับอนุญาตหรือไม่ ถ้าได้รับอนุญาตรัน คืนค่า true มิฉะนั้น false