Bash Shell Expansion
หนึ่งใน Operation ของ Shell เมื่อ Analyze Input คือ Shell Expansion Bash ให้ Expansion ประเภทต่างๆ ในบทความนี้มาทบทวน Expansion สำคัญ – "Brace Expansion"
บทความนี้เป็นส่วนหนึ่งของชุด Bash Tutorial Series ต่อเนื่อง
Brace Expansion
Brace Expansion ใช้ Generate Arbitrary Strings Brace Expansion อนุญาตให้สร้าง Multiple Modified Command Line Arguments จาก Single Argument Strings ที่ระบุใช้ Generate All Possible Combinations กับ Optional Surrounding Preambles และ Postscripts Preamble ถูก Prefix ให้แต่ละ String ใน Braces และ Postscript ถูก Append ให้แต่ละ Resulting String Expand จากซ้ายไปขวา
$ echo last{mce,boot,xorg}.log
lastmce.log lastboot.log lastxorg.log
where last is Preamble and .log is the postscript
Echo Statement ข้างต้นหลีกเลี่ยงการ Specify 3 Log Files แยกกัน ถ้าอยากดู Content ของ Last Boot Log, MCE Log และ Xorg Log ใช้ Brace Expansion ตาม Echo Statement ข้างต้น
1. Example สำหรับ Backup ใช้ Brace Expansion
$ cat bkup.sh
set -x # expand the commands
da=`date +%F`
cp $da.log{,.bak}
$ ./bkup.sh
++ date +%F
+ da=2010-05-28
+ cp 2010-05-28.log 2010-05-28.log.bak
ใน Backup Script ข้างต้น Copy Current Date Log File ด้วย Extension .bak Element แรกใน Braces ว่าง ดังนั้น Element แรกจะมีแค่ Preamble
2. Example สำหรับ Restore ใช้ Brace Expansion
$ cat restore.sh
set -x # expand the commands
da=`date +%F`
cp $da.log{.bak,}
$ ./restore.sh
++ date +%F
+ da=2010-05-28
+ cp 2010-05-28.log.bak 2010-05-28.log
ใน Restore Script Element แรกใน Parameter คือ .bak ในขณะที่ Element ที่สองว่าง
อ่านเพิ่มเติม Bash Shell Functions ก่อนหน้า
3. Example สำหรับ Brace Expansion โดยไม่มี Preamble และ Postscript
ถ้าไม่มี Preamble และ Postscript มันแค่ Expand Elements ที่ให้ใน Braces
$ cat expand.sh
echo {oct,hex,dec,bin}
$ ./expand.sh
oct hex dec bin
โดยไม่มี Optional Preamble และ Postscript Strings Result คือแค่ Space Separated List ของ Strings ที่ให้มา
Brace Expansion สำหรับ Ranges
Brace Expansion Expand Sequences ด้วย Sequences อาจเป็น Integers หรือ Characters
4. Example สำหรับ Integer และ Character Sequences
$ cat sequence.sh
cat /var/log/messages.{1..3}
echo {a..f}{1..9}.txt
$ ./sequence.sh
May 9 01:18:29 x3 ntpd[2413]: time reset -0.132703 s
May 9 01:22:38 x3 ntpd[2413]: synchronized to LOCAL(0), stratum 10
May 9 01:23:44 x3 ntpd[2413]: synchronized to
May 9 01:47:48 x3 dhclient: DHCPREQUEST on eth0
May 9 01:47:48 x3 dhclient: DHCPACK from 23.42.38.201
..
..
a1.txt a2.txt a3.txt a4.txt b1.txt b2.txt b3.txt b4.txt c1.txt c2.txt c3.txt c4.txt
Cat Command แรก Expand Messages.1, Messages.2 และ Messages.3 แสดง Content และ Echo Statement ถัดไป Character และ Integer Sequences รวมกันใช้
Sequences ด้วย Increment Value
ใน Kshell Brace Expansion สามารถใช้ Increment Value Generate Sequences ได้
Syntax:.. ..
Incr เป็น Numeric สามารถใช้ Negative Integer ได้ แต่ Sign ที่ถูกต้อง Deduced จาก Order ของ Start และ End
5. Example สำหรับใช้ Increment ใน Sequences
$ ksh
$ echo /var/log/messages.{1..7..2}
/var/log/messages.1
/var/log/messages.3
/var/log/messages.5
/var/log/messages.7
$
ใช้วิธีนี้ดู Alternate Days Logfiles ได้
Pitfall ใน Brace Expansion (หลุมที่ต้องระวัง)
Brace Expansion ไม่ Expand Bash Variables เพราะ Brace Expansion เป็น Very First Step ของ Shell Expansion Variable จะ Expand ภายหลัง
6. Example สำหรับ Variables ใน Expansion
ถ้าดู Output ของ For Statement 2 ตัวต่อไปนี้ จะเห็น Pitfall ข้างต้น
$ cat var_seq.sh
# Print 1 to 4 using sequences.
for i in {1..4}
do
echo $i
done
start=1
end=4
# Print 1 to 4 using through variables
echo "Sequences expressed using variables"
for i in {$start..$end}
do
echo $i
done
$ ./var_seq.sh
1
2
3
4
Sequences expressed using variables
{1..4}