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

เพิ่มประสิทธิภาพการทำงานด้วย 10 เคล็ดลับ Bash Command Line ที่จำเป็น

การเรียนรู้ command line จำเป็นสำหรับนักพัฒนาทุกคน

และเพื่อรันคำสั่งบน command line คุณต้องมี shell

Bash shell นิยมใน Unix-like operating systems เช่น Mac และ Linux จริงๆ แล้วใน most Linux distros Bash เป็น default shell

คุณยังใช้ Bash ใน Windows ผ่าน WSL (Windows Subsystem for Linux) ได้

หลังเรียน basic Bash commands แล้ว ถึงเวลา level up

บทความนี้ไม่ใช่สำหรับ absolute beginners แต่หวังว่า novices และ advanced users จะได้ประโยชน์

นี่คือ 10 Bash commands ที่ช่วยทำงานเร็วขึ้นบน terminal

1. ใช้ Control + L ล้างหน้าจอ และ Control + D เพื่อออก

ล้างหน้าจอ terminal พิมพ์ clear บน command line

ออกพิมพ์ exit

แต่กด Ctrl + l (⌘ + l) ล้างหน้าจอ และ Ctrl + d (⌘ + d) ปิด terminal เร็วกว่า

2. ใช้คำสั่ง nohup สร้าง processes ที่ไม่จบเมื่อ terminal session จบ

โปรแกรมโหลดเข้า memory แล้วเรียกว่า processes

บางครั้งผมเปิด Firefox จาก command line:
firefox https://freecodecamp.org.

แต่ทันทีที่ปิด terminal Firefox ก็ crash ตามไป

ป้องกันได้ด้วยคำสั่ง nohup (no hang up): nohup firefox https://freecodecamp.org.

ตอนนี้ปิด terminal Firefox ไม่ crash แต่มันแท็บ crash

แก้: ทำ Firefox เป็น background process โดยเติมสัญลักษณ์ &

nohup firefox https://freecodecamp.org &

ตอนนี้แม้ quit terminal tabs ก็ยังคงสมบูรณ์

3. ใช้ pkill kill processes โดยพิมพ์แค่ส่วนของชื่อ

ด้วยคำสั่ง killall kill process ด้วยชื่อได้:

killall firefox

ดีกว่า ใช้ pkill terminate โดยพิมพ์แค่ส่วนของชื่อ

pkill fire*

4. วางคำสั่ง time นำหน้า เพื่อรู้ว่า execute ใช้เวลาเท่าไหร่

อยากรู้ว่าอะไร execute ใช้เวลานานแค่ไหนใน shell?

แค่วาง time นำหน้าคำสั่ง: time gcc -g *.c.

5. บน Linux ใช้ cat /etc/*rel* ดูชื่อ distro

พิมพ์ uname -a แสดง system information

อยาก double check ว่ารัน distro อะไรอยู่?

พิมพ์ cat /etc/*rel* บน shell แล้ว enter

6. ใช้คำสั่ง sed ใน text files find และ replace

อยาก replace multiple occurrences ของคำใน text file?

ใช้คำสั่ง sed.

sed s'/apples/oranges/g' myfile.txt

ที่นี่ occurrences ทั้งหมดของคำ 'apples' กลายเป็น 'oranges'

ถ้าต้องการ replace แค่ occurrence แรกในแต่ละบรรทัด เอา 'g' suffix ท้ายออก: sed s'/apples/oranges/' myfile.txt.

'g' คือ global.

Forward slash / คือ delimiter จริงๆ ใช้ delimiter อะไรก็ได้

ลองใช้ underscore เดียว _ เป็น delimiter: sed s'_apples_oranges_'g ` myfile.txt.

แค่ sed แค่ replace บน standard output ไฟล์ต้นฉบับไม่เปลี่ยน

เปลี่ยนไฟล์ 'in place' ใช้ flag -i: sed -i s'_apples_oranges_g' myfile.txt.

7. รู้ public IP address ของคอมพิวเตอร์ด้วย curl

มี IP addresses 2 ประเภท: private และ public

System มอบ internal IP addresses เช็คได้ด้วยคำสั่ง ifconfig

แต่อยากรู้ public IP ของคอมพิวเตอร์ – IP address ที่ ISP มอบให้ interface?

ขณะ online ใช้ curl ifconfig.me ; echo หรือ curl ifconfig.co ; echo บน command line

กด 'up' arrow key แสดงคำสั่งล่าสุดที่พิมพ์

พิมพ์ history แสดงคำสั่งทั้งหมดที่พิมพ์เก็บใน bash history

ดีกว่า พิมพ์ Ctrl + r (⌘ + r) บน shell เริ่มพิมพ์คำสั่ง

ขณะพิมพ์ shell autocompletes จาก history กด 'enter' ทันทีเจอ match

ถ้าจำได้แค่หนึ่งอย่างจาก tutorial นี้ จำ key combination นี้: Ctrl + r (⌘ + r).

มันจะช่วยประหยัดเวลามาก มั่นใจได้!

9. ใช้ shell ทำคณิตศาสตร์

สำหรับ simple calculations ที่ไม่ input/output fractions ใช้ได้เลย:

:~$ echo $((19*34)) :~$ 646 

สำหรับ calculations ที่เกี่ยว fractions แค่ echo expression แล้ว pipe เข้า bc command

:~$ echo "scale=2; 9*3/((2*2)+1)" | bc :~$ 5.40 

ที่นี่ 'scale' คือ precision ที่ต้องการ ระบุ 2 decimal points

10. ใช้ brace expansion สร้างไฟล์จำนวนมาก

สร้าง 100 ไฟล์ในโฟลเดอร์อย่างไร?

file1.txt, file2.txt, file3.txt ... file100.txt

ใช้ brace expansion: touch file{1..100}.txt.

ต้องสร้าง 3 ไฟล์สำหรับโปรเจกต์: app.html, app.css, app.js

แทนที่สร้างทีละไฟล์ ใช้ brace expansion สร้างทั้งหมดทีเดียว

:~$ touch app.{html,css,js} :~$ ls app.html app.css app.js :~$ 

หรือในโฟลเดอร์โปรเจกต์ ต้องสร้าง 5 directories: images, css, src, templates, scripts

ใช้:

:~$ mkdir {images,css,src,templates,scripts} :~$ ls images css src templates scripts :~$ 

ข้อระวังเดียว: ให้แน่ใจว่าไม่มี spaces ระหว่างคำภายใน braces

Wrapping up

รายการ 10 Bash command line tips ที่ช่วยทำงานเร็วขึ้นบน terminal

เรียนรู้ Bash commands เหล่านี้ จะช่วยคุณมากใน programming journey

Happy Coding!