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

เชี่ยวชาญ Bash Tilde Expansion: 5 ตัวอย่างปฏิบัติ

Bash Shell ให้ Variables บางตัวที่ Prefix ด้วย '~' (เรียกว่า Tilde) ซึ่งเรียกว่า Tilde Expansions

มันคือ Synonyms สำหรับ Contents ของ Variables อื่นๆ ภายใน Shell ของคุณ

Tilde Expansion คือ Process ของการ Convert Abbreviations เหล่านี้เป็น Directory Names ที่มันแทน ในบทความนี้ มาทบทวน Tilde Expansion Feature พร้อมตัวอย่าง

Tilde Expansion ใช้กับ '~' Plus Characters รวมถึง +, - และ N (ซึ่งเป็น Integer) จนถึง Whitespace หรือ Slash

Tilde Expansion ใช้ Expand ไปยัง Pathnames เฉพาะหลายอย่าง:

  • Home Directories
  • Current/Previous Working Directory
  • Directories จาก Directory Stack

Home Directories

Tilde Expansion ให้วิธี Expand Home Directory ของ Current User หรือ Home Directory ของ Username ที่กำหนด

Syntax
~       Expand ไปยัง Variable $HOME หรือ Home Directory ของ Current User
~USER   Expand ไปยัง Home Directory ของ Username ที่กำหนด

ตัวอย่าง 1. Current User's Home

Tilde(~) เป็น Word แยกต่างหาก จะ Expand ไป $HOME ถ้ามีการ Define ถ้า $HOME ไม่ได้ Define จะ Expand ด้วย Home Directory ของ Current User

ตอนนี้ค่า HOME Variable คือ /home/oracle ดังนั้น cd ~ เปลี่ยน Current Directory ไปค่า $HOME

## Login เข้า Oracle User ที่ Home Directory คือ /home/oracle
# su oracle
[tmp]$ pwd
/tmp
[tmp]$ echo $HOME
/home/oracle
[tmp]$ cd ~
[~]$ pwd
/home/oracle

HOME เปลี่ยนเป็น /sbin และ cd ~ ใช้แค่ $HOME ไม่ใช่ Home Directory ของ User หลัง Unset ค่า $HOME cd ~ เปลี่ยน Directory ไปค่า Home Directory ที่ Set ให้ Oracle User ใน /etc/passwd สำหรับ Tilde Expansion HOME Override Real Home Directory

[~]$ export HOME=/sbin
[oracle]$ cd ~
[~]$ pwd
/sbin
[~]$ unset HOME
[sbin]$ cd ~
[oracle]$ pwd
/home/oracle

ตัวอย่าง 2. Home Directory ของ User ที่กำหนด

Script ต่อไปนี้ Backup Log File ที่มี Current Date ในชื่อ มันยัง Log Starting Time และ End Time ลงไฟล์ backup.log ใน Home Directory ของ Oracle User

#! /bin/bash
echo "Initiating the backup at `date`" >> ~oracle/backup.log
da=`date +%F`
cp $da.log{,.bak}
echo "END BACKUP at `date`" >> ~oracle/backup.log
$ ls -l /home/oracle/
total 8
-rw-r--r-- 1 root root 99 Jun 4 14:23 backup.log

ถ้า Username ที่ให้มาไม่มีอยู่จริง มันจะไม่ Expand ไปที่ไหน ในตัวอย่างต่อไปนี้ ไม่มี User ชื่อ ora ดังนั้น ~ora จะไม่ Expand ไป /home/ora

$ echo ~ora
~ora

อ่านบทความก่อนหน้าเข้าใจ Brace Expansion ใน Bash คือการใช้ { } ใน Bash ยังไง

Working Directories (Working Directories)

Tilde ด้วย + และ - ใช้แทน Working Directories

  • ~+ Expand ไปค่า PWD Variable ที่เก็บ Current Working Directory
  • ~- Expand ไปค่า OLPDWD Variable ที่เก็บ Previous Working Directory ถ้า OLDPWD Unset ~- จะไม่ Expand

ตัวอย่าง 3. Expansion ของ Old/Current Working Directories

ตัวอย่างต่อไปนี้จะ Compare File ใน Current Directory และ Previous Working Directory

$ cat comp.sh
#! /bin/bash
set -x
cd /var/opt/gg
if [ -f gg.c ]
then
  echo "File1 exists"
fi
cd /var/opt1/gg
if [ -f gg.c ]
then
  echo "File2 exists"
  cmp ~+/gg.c ~-/gg.c
fi
$ ./comp.sh
+ cd /var/opt/gg
+ '[' -f gg.c ']'
+ echo 'File1 exists'
File1 exists
+ cd /var/opt1/gg
+ '[' -f gg.c ']'
+ echo 'File2 exists'
File2 exists
+ cmp /var/opt1/gg/gg.c /var/opt/gg/gg.c
cmp: EOF on /var/opt1/gg/gg.c
$

ใน Execution ข้างต้น:

  • ~+/gg.c Expand ไป /var/opt1/gg/gg.c
  • ~-/gg.c Expand ไป /var/opt/gg/gg.c

บทความนี้เป็นส่วนหนึ่งของชุด Bash Tutorial Series ต่อเนื่อง

Expansion สำหรับ Directories ใน Stack

แต่ละ Bash Process มี Stack Object ที่ใช้ Track Directories ที่ Script Visit ขณะ Process Data ของ Directory Contents

เป็น Mechanism ง่ายๆ สำหรับ Reference Directories หรือ Change กลับไป Directories ที่ Visit มาก่อน Tilde Expansion ยังให้ Expansion ไป Directories ใน Directory Stack

  • ~+N Expand Nth Directory ใน Directory Stack (นับจากซ้ายของ List ที่ dirs พิมพ์เมื่อ Invoke โดยไม่มี Options) เริ่มจากศูนย์
  • ~-N Expand Nth Directory ใน Directory Stack (นับจากขวาของ List ที่ dirs พิมพ์เมื่อ Invoke โดยไม่มี Options) เริ่มจากศูนย์

อ่านบทความก่อนหน้าเข้าใจ Dir, Pushd และ Popd Commands เพื่อจัดการ Directory Stack

ตัวอย่าง 4. แสดง Nth Directory จากซ้ายด้วย ~+

ในตัวอย่าง Directory Stack มี 4 Directories ~+2 ให้ Directory Path ในตำแหน่งที่ 2 จากซ้าย เริ่มจากศูนย์

$ dirs -v
0 /sbin
1 /var/opt/midas
2 /var/opt/GG/bin
3 /root
$ cd ~+2
$ pwd
/var/opt/GG/bin

แต่ Top of Stack (Zero Position) จะมี Current Directory เสมอ ดังนั้นหลัง Execution ข้างต้น Directory Stack จะแสดง:

$ dirs -v
0 /var/opt/GG/bin
1 /var/opt/midas
2 /var/opt/GG/bin
3 /root

ตัวอย่าง 5. แสดง Nth Directory จากขวาด้วย ~-

คล้ายตัวอย่างข้างต้น แต่ Consider Directories จาก Bottom ของ Stack เพราะ ~-

$ dirs -v
0 /var/opt/GG/bin
1 /var/opt/midas
2 /var/opt/GG/bin
3 /root
$ cd ~-2
$ pwd
/var/opt/midas
$ dirs -v
0 /var/opt/midas
1 /var/opt/midas
2 /var/opt/GG/bin
3 /root