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

เชี่ยวชาญการนำทาง Bash: ใช้ pushd และ popd สลับ Directory ได้อย่างมีประสิทธิภาพ

คำสั่ง pushd และ popd เป็น Built-in Features ของ Bash Shell ช่วย "Bookmark" ไดเรกทอรีเพื่อนำทางเร็วระหว่างตำแหน่งต่างๆ บนฮาร์ดไดรฟ์ คุณอาจรู้สึกว่า Terminal เร็วมากแล้ว ไปไหนก็ได้ในไม่กี่ Keystrokes แต่ความเร็วนั้นอาจพังเมื่อต้องไป-มาระหว่าง Directory บ่อย หรือ "หลงทาง" ใน Filesystem นั่นคือจุดที่ pushd และ popd ช่วยแก้ได้

pushd

พื้นฐานแล้ว pushd คล้าย cd เลย มันพาคุณจาก Directory หนึ่งไปอีกหนึ่ง สมมติมี Directory one มี Subdirectory two มี three ถ้า Current Working Directory เป็น one สามารถไป two หรือ three ด้วย cd:

$ pwd
one
$ cd two/three
$ pwd
three

ทำได้ด้วย pushd เช่นกัน:

$ pwd
one
$ pushd two/three
~/one/two/three ~/one
$ pwd
three

ผลลัพธ์สุดท้ายเหมือน cd แต่มีผลกลาง: pushd Echo ไดเรกทอรีปลายทางและต้นทาง นี่คือ Directory Stack สิ่งที่ทำให้ pushd โดดเด่น

Stacks

Stack ในคอมพิวเตอร์ หมายถึง Collection ของ Elements ในบริบทนี้ Elements คือ Directories ที่เยี่ยมชมด้วย pushd คิดได้ว่าเป็น History หรือ Breadcrumb Trail

เคลื่อนที่ทั่ว Filesystem ด้วย pushd ทุกครั้ง ตำแหน่งเก่าและใหม่จะเพิ่มเข้า Stack:

$ pushd four
~/one/two/three/four ~/one/two/three ~/one
$ pushd five
~/one/two/three/four/five ~/one/two/three/four ~/one/two/three ~/one

เมื่อสร้าง Stack แล้ว ใช้เป็น Bookmarks หรือ Fast-travel Waypoints สมมติทำงานมากใน ~/one/two/three/four/five รู้ว่าเคยไป one แต่ไม่จำตำแหน่งใน Stack ดู Stack ได้ด้วย Argument +0 (Plus sign ตามด้วย Zero) บอก pushd อย่าเปลี่ยน Directory แต่ Echo Stack ปัจจุบัน:

$ pushd +0
~/one/two/three/four ~/one/two/three ~/one ~/one/two/three/four/five

หรือใช้คำสั่ง dirs พร้อม Option -v ดู Index ของแต่ละ Directory:

$ dirs -v
0 ~/one/two/three/four
1 ~/one/two/three
2 ~/one
3 ~/one/two/three/four/five

Entry แรกคือตำแหน่งปัจจุบัน ยืนยันได้ด้วย pwd:

$ pwd
~/one/two/three/four

เริ่มที่ 0 (ตำแหน่งปัจจุบันและ Entry แรก) Element ที่ 2 คือ ~/one ตำแหน่งเป้าหมาย ย้ายไปข้างหน้าใน Stack ด้วย Option +2:

$ pushd +2
~/one ~/one/two/three/four/five ~/one/two/three/four ~/one/two/three
$ pwd
~/one

เปลี่ยน Working Directory เป็น ~/one และ Shift Stack ให้ตำแหน่งใหม่อยู่หน้า

ย้ายถอยหลังใน Stack ได้เช่นกัน เช่น ไป ~/one/two/three จากตัวอย่าง Output ใช้ -0 (ย้อนหลัง 1 โดยเริ่มที่ 0):

$ pushd -0
~/one/two/three ~/one ~/one/two/three/four/five ~/one/two/three/four

เพิ่ม Directory ใน Stack

นำทาง Stack ต่อได้ และจะคงเป็น Static Listing ของ Directory ที่เยี่ยมชมล่าสุด หากต้องการเพิ่ม Directory ให้ Path ของ Directory นั้น หาก Directory ใหม่ จะเพิ่มใน List ตามปกติ:

$ pushd /tmp
/tmp ~/one/two/three ~/one ~/one/two/three/four/five ~/one/two/three/four

แต่ถ้ามีอยู่ใน Stack แล้ว จะเพิ่มซ้ำ:

$ pushd ~/one
~/one /tmp ~/one/two/three ~/one ~/one/two/three/four/five ~/one/two/three/four

Stack มักใช้เป็น List ของ Directory ที่ต้องการเข้าถึงเร็ว แต่จริงๆ เป็น History จริงของที่เคยไป หากไม่ต้องการเพิ่มซ้ำ ต้องใช้ Notation +N และ -N

ลบ Directory ออกจาก Stack

Stack ไม่ Immutable เพิ่มได้ด้วย pushd ลบได้ด้วย popd

สมมติเพิ่ง pushd เพิ่ม ~/one ทำให้เป็น Current Working Directory ลบ Element แรก (Zeroeth):

$ pwd
~/one
$ popd +0
/tmp ~/one/two/three ~/one ~/one/two/three/four/five ~/one/two/three/four
$ pwd
~/one

ลบ Element ไหนก็ได้ เริ่มนับที่ 0:

$ pwd ~/one
$ popd +2
/tmp ~/one/two/three ~/one/two/three/four/five ~/one/two/three/four
$ pwd ~/one

ใช้ popd จากหลัง Stack ได้ เริ่มที่ 0 เช่น ลบ Directory ตัวสุดท้าย:

$ popd -0
/tmp ~/one/two/three ~/one/two/three/four/five

ใช้แบบนี้ popd ไม่เปลี่ยน Working Directory แค่จัดการ Stack

Default Behavior ของ popd โดยไม่มี Argument คือ ลบ Item แรก (Zeroeth) ออกจาก Stack และทำให้ Item ถัดไปเป็น Current Working Directory

มีประโยชน์เป็น Quick-change Command เมื่อทำงานใน 2 Directory ต่างกัน ต้อง "หลบ" ไปที่อื่นสักพัก ไม่ต้องคิดถึง Stack หากไม่ต้องการ History ซับซ้อน:

$ pwd
~/one
$ pushd ~/one/two/three/four/five
$ popd
$ pwd
~/one

ไม่จำเป็นต้องใช้ pushd และ popd ติดกัน หาก pushd ไปที่อื่น แล้วถูกเบื่อ 3 ชั่วโมง Debug Bug หรือ Research จะพบ Stack รออยู่ (เว้นแต่จบ Terminal Session):

$ pwd ~/one
$ pushd /tmp
$ cd {/etc,/var,/usr}; sleep 2001
[...]
$ popd
$ pwd
~/one

Pushd และ Popd ในโลกจริง

คำสั่ง pushd และ popd มีประโยชน์มากกว่าที่คิด จะหาข้อดีใช้งานได้ และคุ้นเคยกับ Directory Stack การคุ้นเคย pushd ช่วยเข้าใจ git stash ซึ่งไม่เกี่ยวข้องแต่คล้ายกันในแนวคิด

ใช้ใน Shell Scripts น่าสนใจ แต่โดยทั่วไปควรหลีกเลี่ยย เพราะไม่ Portable นอก Bash และ Zsh และอาจเข้าใจยากเมื่ออ่าน Script กลับ (pushd +3 ไม่ชัดเจนเท่า cd $HOME/$DIR/$TMP)

นอกจากข้อควรระวัง หากคุณ User Bash หรือ Zsh ปกติ ควรลอง pushd และ popd