ผมมีความสัมพันธ์ Love-Hate กับ Bash ใช้เวลามากใน Terminal และ Bash เป็น "ภาษาโปรแกรมมิ่ง" หลัก บางครั้งบอกคนว่า "find, grep, xargs คือสิ่งที่รัน Infrastructure" ทุกคนหัวเราะ — จนกว่าจะรู้ว่าผมจริงจัง
Python เป็นเลือกที่สมบูรณ์แบบสำหรับ System Admins — และทุกคนที่ทำงาน Terminal แต่ไม่ชอบ Bash หรือมีงานซับซ้อนเกินกว่า Bash จะรับมือได้
เมื่องานเกินระดับนี้:
find $(pwd) -name "*.txt" | xargs -I {} echo "do stuff with {}"
→ ถึงเวลาใช้ Python!
ทำไม Python ถึงดีกว่า Bash สำหรับ CLI
- Libraries อุดมสมบูรณ์: จัดการ System Ops, อ่านไฟล์, List Directories, Loops, Exit Codes, ทุกอย่างมี Library รองรับ
- Autocomplete ด้วย IDEs: ไม่ต้องจดจำ Syntax — VS Code, PyCharm, Atom ทำให้เขียนเร็วและถูกต้อง
- Testing Framework แข็งแกร่ง: pytest ทดสอบโค้ดได้จริง (ถ้ายังไม่ทดสอบ เริ่มเลยครับ)
- iPython Console: I LOVE IT. Interactive, Autocomplete, Help, Debug ได้ทุกอย่าง
- Cross-platform: Python มีเกือบทุกระบบ ไม่มีก็ติดตั้ง Miniconda ได้
- Error Handling: Try/Except Blocks จัดการ Error ได้ชัดเจนกว่า Bash มาก
- OS Abstraction: Libraries จัดการความแตกต่าง Windows/Linux/macOS ให้เอง
- Learning Curve ต่ำ: แม้ไม่มีพื้นฐานโปรแกรมมิ่ง เริ่ม Python ได้ง่าย
เริ่มต้นใช้งาน Python
ตรวจสอบ Python และ iPython
which python
which ipython
ถ้าทั้งคู่มี → พร้อมใช้งาน! ถ้ามี Python แตไม่มี iPython → ติดตั้งผ่าน Miniconda (แนะนำ)
ติดตั้ง Miniconda
- ดาวน์โหลด Installer สำหรับ OS ของคุณ (Python 3)
- รัน:
bash Miniconda3-latest-Linux-x86_64.sh - Follow Prompts → ติดตั้งเสร็จ
- Update:
conda update conda && conda config --add channels conda-forge && conda update -y --all && conda install -y ipython
Troubleshooting
conda clean --allแล้วลองใหม่- ตรวจสอบ Channel ให้ถูกต้อง
conda update -y --all- อย่าติดตั้งมากไปใน Global — สร้าง Environments แยกต่างหาก
สร้าง Environments ด้วย Conda
conda create -n my-project ipython package1 package2
Tip: ติดตั้ง iPython ในทุก Environment!
Python Libraries สำหรับ System Administration
- os — List dirs, check file/symlink exists, mkdir, run commands, env vars, etc. (Go-to package)
- subprocess — รัน System Commands ที่ไม่มี Library สำเร็จรูป
- shutil — File Operations ที่ไม่อยู่ใน os (copy tree, move, etc.)
- pprint — Pretty-print โครงสร้างข้อมูลซับซ้อน
- pytest — Testing Framework
รันโค้ด Python อย่างไร?
- เข้า iPython Console โดยตรง → พิมพ์ทดสอบได้เลย
- เขียน Script → รัน
python script.py - Editor แนะนำ: PyCharm (จ่าย), VS Code (ฟรี), Atom (ฟรี)
- Autocomplete ใน iPython: พิมพ์ฟังก์ชันบางส่วน → Tab → เลือกได้เลย
Get Help ใน iPython
รู้ชื่อ Library/Function → เรียก Help ใน iPython ได้เลย:
>>> import os
>>> help(os.path.join)
IDE/Editor ทั่วไปก็มี Help Built-in
ตัวอย่างการใช้งานจริง
Import Packages
import os
import subprocess
import shutil
from pprint import pprint
File & Directory Operations
# Current Working Directory
my_cwd = os.getcwd()
print(my_cwd)
# List Directory Contents
dir_list = os.listdir()
for item in dir_list:
print(item)
# Absolute Path
os.path.abspath('some-file')
# Basename (filename only)
os.path.basename('/path/to/file')
# Split Path (Platform Independent)
os.path.split(os.getcwd()) # ('/Users', 'jillian')
# Check Path Exists
os.path.exists('/path/on/filesystem')
# Check Symlink
os.path.islink('/path/to/check')
Move/Copy Files & Directories
# Copy Directory (cp -rf)
shutil.copytree('src', 'dest')
# Copy File (cp)
shutil.copyfile('file1', 'file2')
# Move (mv)
shutil.move('src', 'dest')
Run System Commands
command = "echo 'hello'"
result = subprocess.run(command.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout)
print(result.stderr)
Write Files
# Write (Create/Overwrite) - echo "hello" > hello.txt
with open("hello.txt", "w+") as f:
f.write("hello!")
# Append - echo "hello" >> hello.txt
with open("hello.txt", "a+") as f:
f.write("hello again!")
เขียน Tests ด้วย pytest
def test_system_command():
"""Test exit code of a system command"""
command = "echo 'hello'"
result = subprocess.run(command.split(' '), stdout=subprocess.PIPE)
assert result.returncode == 0
บันทึกเป็น test_my_code.py → รัน pytest test_my_code.py
สรุป
ครั้งหน้าที่ต้องเขียน Loop ใน Bash — ลองเปิด iPython Console ดูสิ! จะพบว่า Python ทำได้ง่ายกว่า อ่านง่ายกว่า และรักษาได้ดีกว่ามาก
Python ไม่ได้แทนที่ Bash ทั้งหมด — แต่สำหรับงานซับซ้อน Python เป็นเครื่องมือที่เหมาะสมกว่าอย่างชัดเจน