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

Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux

Marcel เป็น Shell ใหม่ คล้าย Traditional Shells หลายทาง แต่ทำบางอย่างต่างออกไป:

  • Piping: ทุก Shells ใช้ Pipes ส่ง Text จาก Output Command หนึ่งไป Input อีก Command Marcel Pipe Structured Data แทน Strings
  • Python: Marcel Implement ด้วย Python และ Expose Python หลายทาง หากต้องการ Logic น้อยๆ ใน Commands Marcel ให้ Express ด้วย Python
  • Scripting: Marcel เข้าถึง Scripting แบบ Unusual เขียน Sequence ของ Marcel Commands ใน Text File Execute ได้ แต่ Marcel ยังให้ API เป็น Python Module Import Module นี้ทำ Python Scripting ได้สะดวกกว่า Plain Python มาก

Marcel Licensed Under GPLv3

ติดตั้ง Marcel Modern Shell ใน Linux

Marcel ต้องการ Python 3.6 หรือใหม่กว่า พัฒนาและทดสอบบน Linux ทำงานได้ส่วนใหญ่บน macOS (หากช่วย Port ไป Windows หรือ Fix macOS Deficiencies ติดต่อมาได้)

ติดตั้ง marcel สำหรับการใช้งานส่วนตัว:

# python3 -m pip install marcel

หากต้องการติดตั้งสำหรับ All Users (เช่น /usr/local):

$ sudo python3 -m pip install --prefix /usr/local marcel

ติดตั้ง marcel เสร็จ ตรวจสอบการทำงานรันคำสั่ง marcel แล้วที่ Prompt รัน version Command:

$ marcel
Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux

Customization ของ Marcel Shell

Customize marcel ในไฟล์ ~/.marcel.py ซึ่งอ่านตอน Startup (และอ่านใหม่เมื่อ Modified) จากชื่อไฟล์ Customization ของ Marcel ทำด้วย Python

สิ่งที่คุณอาจอยากทำคือ Customize Prompt กำหนด List ให้ Variable PROMPT ตัวอย่าง หากอยาก Prompt เป็น Current Directory สีเขียว ตามด้วย > สีน้ำเงิน:

PROMPT = [ Color(0, 4, 0), lambda: PWD, Color(0, 2, 5), '> ' ]

Prompt ที่ได้จะแบบนี้:

Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux

แทนที่ PS1 Configuration ที่น่าปวดหัวใน Bash Color(0, 4, 0) ระบุ Green (Arguments เป็น RGB Values Range 0-5) PWD เป็น Environment Variable แทน Current Directory Prefix ด้วย lambda: สร้าง Function Evaluate ทุกครั้งที่ Prompt แสดง

~/.marcel.py สามารถ Import Python Modules ได้ เช่น ต้องการใช้ Functions ของ Math Module ใน Marcel Commands:

from math import *

หลังทำแล้ว เรียก Symbols จาก Module ได้ เช่น pi:

Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux

สังเกต pi อยู่ใน Parentheses โดยทั่วไป Marcel ใช้ Parentheses Delimit Python Expressions ดังนั้น (pi) Evaluate Python Expression ดึงค่า Variable pi เข้าถึง Traditional Environment Variables แบบนี้ได้ เช่น (USER) และ (HOME) หรือ Python Expression ใดๆ ที่ Valid ใน Marcel's Namespace

และ Define Symbols เองได้ เช่น หากใส่ Function Definition นี้ใน ~/.marcel.py:

def factorial(n):
  f = 1
  for i in range(1, n + 1):
    f *= i
  return f

ใช้ Factorial Function บน Command Line ได้:

Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux

Marcel Shell Examples

เรียนรู้ตัวอย่าง Commands ใน Marcel Shell

หา File Sizes ตาม Extension

Explore Current Directory Recursively Group Files โดย Extension (เช่น .txt, .py ฯลฯ) คำนวณ Total File Size ต่อ Group

ใน Marcel ทำได้ดังนี้:

Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux

LS Operator ผลิต Stream ของ File Objects (-fr หมายถึง Visit Directories Recursively Return เฉพาะ Files)

File Objects Piped ไป Command ถัดไป Map Map ระบุ Python Function ใน Parentheses ภายนอก Map แต่ละ File เป็น Tuple มี Extension และ Size (Marcel อนุญาตให้ละ Keyword Lambda ได้)

Red (Reduce) Operator Group โดยส่วนแรกของ Tuple (Extension) แล้ว Sum Sizes ภายในแต่ละ Group ผลลัพธ์ Sorted By Extension

Host Executables และ Marcel Pipeline

Pipelines อาจประกอบด้วย Mixture ของ Marcel Operators และ Host Executables Operators Pipe Objects แต่ที่ Operator/Executable Boundaries Marcel Pipe Strings แทน

ตัวอย่าง Command นี้รวม Operators และ Executables แสดง Usernames ของ Users ที่ Shell เป็น /bin/bash

$ cat /etc/passwd \
 | map (line: line.split(':')) \
 | select (*line: line[-1] == '/bin/bash') \
 | map (*line: line[0]) \
 | xargs echo
Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux Marcel: Shell สมัยใหม่ ขับเคลื่อนด้วย Python สำหรับ Linux

Cat เป็น Linux Executable อ่าน /etc/passwd Marcel Pipe เนื้อหา Downstream ไป Marcel Operator Map

Parenthesized Argument ของ Map เป็น Python Function Split Lines ที่ Colon Separators ให้ 7-Tuples Select เป็น Marcel Operator Argument เป็น Python Function Identify Tuples ที่ Last Field เป็น /bin/bash

Operator ถัดไป Map อีกตัว Keep Username Field ของแต่ละ Input Tuple สุดท้าย xargs echo รวม Usernames เข้า Single Line พิมพ์ Stdout

Scripting ใน Marcel Shell

Python บางครั้งถือว่าเป็น Scripting Language แต่จริงๆ ไม่ทำงานดีเพื่อ Purpose นั้น ปัญหาคือ Run Shell Commands และ Executables อื่นๆ จาก Python ค่อนข้าง Cumbersome ใช้ os.system() เรียบง่ายแต่ Inadequate สำหรับ Stdin, Stdout, Stderr subprocess.Popen() Powerful แต่ Complex

Marcel Approach ให้ Module รวม Marcel Operators กับ Python Language Features Revisit ตัวอย่างก่อน Python Code สำหรับ Sum File Sizes By Extension:

from marcel.api import *
for ext, size in (ls(file=True, recursive=True) | map(lambda f: (f.suffix, f.size)) | red('.', '+')):
  print(f'{ext}: {size})

Shell Commands เหมือนเดิม แตกต่าง Syntactic Conventions ดังนั้น ls -fr กลายเป็น ls(file=True, recursive=True) Map และ Red Operators อยู่เช่นเดิม เชื่อมด้วย Pipes เหมือน Shell Version ทั้ง Shell Command (ls ... red) ให้ Python Iterator ใช้กับ For Loop ของ Python ได้

Database Access ด้วย Marcel Shell

Integrate Database Access กับ Marcel Pipelines ได้ ก่อน Config Database Access ใน Config File ~/.marcel.py เช่น:

define_db(name='jao', driver='psycopg2', dbname='acme', user='jao')
DB_DEFAULT = 'jao'

Config Access ไป Postgres Database ชื่อ acme ใช้ psycopg2 Driver Connections จาก Marcel ทำด้วย User jao Database Profile ชื่อ jao (DB_DEFAULT ระบุ Profile jao เป็น Default หากไม่ระบุ Profile) ด้วย Config นี้ Database Query ได้ด้วย SQL Operator เช่น:

sql 'select part_name, quantity from part where quantity < 10' \
 | out --csv --file ~/reorder.csv

Command นี้ Query Table part Dump Query Result ไป File ~/reorder.csv Format CSV

Remote Access ด้วย Marcel Shell

เหมือน Database Access Remote Access Config ใน ~/.marcel.py ได้ ตัวอย่าง Config 4-Node Cluster:

define_remote(name='lab', user='frankenstein', identity='/home/frankenstein/.ssh/id_rsa', host=['10.0.0.100', '10.0.0.101', '10.0.0.102', '10.0.0.103'])

Cluster ระบุเป็น lab ใน Marcel Commands User และ Identity Params ระบุ Login Info Host Param ระบุ IP Addresses ของ Nodes บน Cluster

Config Cluster เสร็จ Operate All Nodes พร้อมกันได้ เช่น ดู Process PIDs และ Command Lines ทั่ว Cluster:

@lab [ps | map (proc: (proc.pid, proc.commandline))]

Return Stream ของ (IP Address, PID, Command Line) Tuples

ข้อมูลเพิ่มเติม:

Marcel ยังใหม่และพัฒนาอยู่ ติดต่อมาได้หากอยากช่วย