11. ตัวดำเนินการ (operators) และคำสั่งน่าสนใจ

11.1 ตัวดำเนินการเปรียบเทียบตัวอักษร (String comparison operators)

  • [ "$s1" = "$s2" ] หรือ [ "$s1" == "$s2" ] เป็นจริง ถ้า s1 เท่ากับ s2
  • [ "$s1" != "$s2" ] เป็นจริง ถ้า s1 ไม่เท่ากับ s2
  • [[ "$s1" < "$s2" ]] หรือ [ "$s1" \< "$s2" ] เป็นจริง ถ้า s1 น้อยกว่า s2
  • [[ "$s1" > "$s2" ]] หรือ [ "$s1" \> "$s2" ] เป็นจริง ถ้า s1 มากกว่า s2
  • [ -n "$s1" ] เป็นจริง ถ้า s1 มีค่าใด ๆ
  • [ -z "$s1" ] เป็นจริง ถ้า s1 ไม่มีค่า

11.2 ตัวอย่างการเปรียบเทียบอักษร

#!/bin/bash
S1='string'
S2='String'
if [ "$S1" = "$S2" ]; then
    echo "S1('$S1') is not equal to S2('$S2')"
fi
if [ "$S1" = "$S1" ]; then
    echo "S1('$S1') is equal to S1('$S1')"
fi

11.3 ตัวดำเนินการทางคณิตศาลตร์ (Arithmetic operators)

  • + การบวก
  • - การลบ
  • * การคูณ
  • / การหาร
  • % การหาเศษจากตัวหาร (remainder)

11.4 ตัวเปรียบเทียบทางคณิตศาตร์ (Arithmetic relational operators

  • -lt น้อยกว่า (<)
  • -gt มากกว่า (>)
  • -le น้อยกว่าหรือเท่ากับ (<=)
  • -ge มากกว่าหรือเท่ากับ (>=)
  • -eq เท่ากับ (==)
  • -ne ไม่เท่ากับ (!=)

11.5 คำสั่งควรรู้

sed (stream editor)
sed เป็นเอดิเตอร์แบบบรรทัดคำสั่ง มีการใช้งานที่พลิกแพลงหลากหลายมาก ตัวอย่าง

$ sed 's/old/new/g' /tmp/dummy

นำเอาเนื้อไฟล์ /tmp/dummy มาแทนที่ old ด้วย new และแสดงออกทางจอภาพ

$ sed 12,18d /tmp/dummy

นำเอาเนื้อไฟล์ /tmp/dummy มาแสดงทางจอภาพ โดยเว้นไม่แสดงบรรทัดที่ 12 ถึงบรรทัดที่ 18

ดูรายละเอียดเพิ่มเติมได้ที่ gentoo: Sed by example

awk (manipulation of datafiles, text retrieval and processing)
awk เป็นทั้งโปรแกรมและภาษาในการค้นหาข้อความในไฟล์จากรูปแบบที่เรากำหนดให้

สมมุติว่าไฟล์ /tmp/dummy มีเนื้อไฟล์คือ

test123
test
tteesstt

ตัวอย่างการใช้งานคือ

$ awk '/test/ {print}' /tmp/dummy
test123
test 

ดูรายละเอียดเพิ่มเติมได้ที่ gentoo: Awk by example

grep (print lines matching a search pattern)
grep เป็นโปรแกรมที่ใช้บ่อยในการค้นข้อความในไฟล์ และยังมีความสามารถในการสรุปผลการนับข้อความด้วย

ตัวอย่าง

$ man grep | grep "standard" -c
8

เป็นการค้นคำว่า standard ในการแสดงผลของคำสั่ง man grep ว่ามีอยู่กี่คำ คำตอบคือ 8

ดูตัวอย่างเพิ่มเติมที่ tdlp: Examples using grep

wc (counts lines, words and bytes)
wc ใช้ในการนับคำ, นับบรรทัด และนับจำนวนหน่วยความจำที่ถูกใช้ในไฟล์ เป็นไบต์

ตัวอย่าง

$ wc --words --lines --bytes /tmp/dummy
 3  3 22 /tmp/dummy
sort (sort lines of text files)
sort ใช้จัดเรียงข้อมูล
สมมุติว่าไฟล์ /tmp/dummy มีเนื้อว่า

b
c
a

ตัวอย่างคำสั่งคือ

$ sort /tmp/dummy
a
b
c

คือการนำเอาเนื้อไฟล์ /tmp/dummy มาจัดเรียง และแสดงผลออกทางจอภาพ

bc (a calculator programming language)
bc เป็นเครื่องคิดเลขแบบใช้บรรทัดคำสั่ง
ตัวอย่างเช่น

$ echo 1+1
1+1
$ echo 1+1 | bc
2

หรือใช้แบบโต้ตอบ

$ bc -q
1 == 5
0

0.05 == 0.05
1

5 != 5
0

2 ^ 8
256

sqrt(9)
3

while (i != 9) {
i = i + 1;
print i
}
123456789

quit
tput (initialize a terminal or query terminfo database)
tput ใช้ในการตั้งค่าหรือแสดงค่าต่าง ๆ ของเทอร์มินัล เช่น

$ tput cup 10 4

เลื่อนเคอร์เซอร์ไปยังบรรทัดที่ 10 สดมภ์ที่ 4

$ tput reset

ล้างจอภาพ มีค่าเท่ากับคำสั่ง clear

$ tput cols

แสดงจำนวนสดมภ์ (ความกว้าง) ของจอเทอร์มินัล

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <blockquote> <img> <h3> <h4> <h5>
  • Lines and paragraphs break automatically.
  • E-Mail addresses are hidden with reCAPTCHA Mailhide.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.