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

เชี่ยวชาญ Linux Terminal: คู่มือเชี่ยวชาญ Piping และ Redirection

ข้อกำหนดเบื้องต้น

เพื่อเข้าใจคู่มือนี้ คุณควรมี:

  • ความเข้าใจพื้นฐานของระบบปฏิบัติการ Linux
  • ประสบการณ์พื้นฐานกับ command line ของ Linux
  • เข้าถึง Linux command line เพื่อลองคำสั่งได้

Piping ใน Linux คืออะไร?

Piping คือการนำ output ของคำสั่ง Linux ตัวหนึ่งไปเป็น input ของคำสั่งอีกตัวหนึ่ง สามารถนำ standard output หรือ standard error จากคำสั่งหนึ่งไปยังอีกคำสั่งได้ โดยใช้ metacharacter pipe (|)

Metacharacters คืออักขระที่มีความหมายพิเศษใน command line เช่น <, >, &

พื้นฐานของ Piping

Syntax พื้นฐาน:

command1 | command2 | command3 | ... | commandN

Terminal จะ execute คำสั่งจากซ้ายไปขวา เริ่มจาก command1 output จะเป็น input ของ command2 ต่อไป output ของ command2 เป็น input ของ command3 และต่อไปเรื่อยๆ สามารถ chain คำสั่งได้ไม่จำกัด

ตัวอย่าง Piping

1. นับจำนวนไฟล์และไดเรกทอรี

ls -l | wc -l

ls -l แสดงรายการไฟล์/ไดเรกทอรีแบบ long format output จะถูก pipe ไป wc -l นับจำนวนบรรทัด

2. เรียงลำดับรายการไฟล์และไดเรกทอรี

ls | sort

ls output pipe ไป sort เรียงตามตัวอักษร

3. เรียงและแสดงคำที่ไม่ซ้ำจากไฟล์

cat words.txt | sort -r | uniq

cat words.txt อ่านไฟล์ sort -r เรียงย้อนกลับ uniq กรองคำซ้ำ

เมื่อไหร่ควรใช้ Command Piping?

Piping ไม่ใช่สำหรับ chain คำสั่งที่ไม่เกี่ยวข้องเพื่อรัน sequential หากต้องการรัน sequential ใช้ semicolon (;):

command1 ; command2 ; ... ; commandN

Redirection ใน Linux คืออะไร?

Redirection คือการกำหนดว่า input หรือ output ของคำสั่งจะไปที่ไหน โดย default คำสั่งรับข้อมูลจาก standard input และ output ไป standard output

Redirection มีประโยชน์มากเมื่อทำงานกับคำสั่งและไฟล์ เช่น redirect output ไปไฟล์แทนการพิมพ์บน terminal หรือระบุไฟล์เป็น input ให้คำสั่ง

อักขระ Redirection สำคัญ

  • > - นำ output ของคำสั่งไปไฟล์ที่กำหนด (overwrite)
  • < - นำเนื้อหาไฟล์ไปเป็น input ของคำสั่ง
  • >> - นำ output ไปไฟล์ (append ถ้าไฟล์มีอยู่)
  • 2> - นำ error messages ไปไฟล์
  • 2>> - นำ error messages ไปไฟล์ (append)
  • &> - นำ standard output และ error ไปไฟล์พร้อมกัน
  • &>> - นำ standard output และ error ไปไฟล์ (append)

1. Output Redirection ด้วย >

command > file

ตัวอย่าง: ls -a > contents.txt เขียนรายการไฟล์ (รวม hidden) ไป contents.txt ไม่แสดงบน terminal

2. Output Redirection ด้วย >>

command >> file

Append output ไปไฟล์ ถ้าไฟล์มีอยู่ จะไม่ overwrite

3. Input Redirection ด้วย <

command < file

ตัวอย่าง: wc -w < files.txt นำ files.txt เป็น input ให้ wc -w นับคำ (แต่คำสั่งส่วนใหญ่รับไฟล์เป็น argument ได้โดยตรง)

4. Error Redirection ด้วย 2> และ 2>>

ls nonexistent 2> error.txt

Error จะถูกเก็บใน error.txt แทนการพิมพ์บน console ใช้ 2>> สำหรับ append

5. Output และ Error Redirection ด้วย &> และ &>>

command &> output.txt

Redirect ทั้ง output และ error ไปไฟล์เดียว &>> สำหรับ append

รวม Piping และ Redirection เพื่อปลดล็อกพลัง Command Line

การรวมทั้งสองทำให้ทำ operation ซับซ้อนได้โดยไม่ต้องเขียนโค้ดมาก

ls | grep ".txt" > text_files.txt

ls แสดงไฟล์ pipe ไป grep กรองเฉพาะ .txt แล้ว redirect ไป text_files.txt

สรุป

ในบทช่วยสอนนี้ คุณเรียนรู้พื้นฐาน piping และ redirection พร้อมตัวอย่าง การใช้ทั้งสองร่วมกันจะเพิ่มพลังของ command line ขึ้นอีกระดับ