01204223/filename expansion

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
เอกสารนี้ประกอบการสอนวิชา 01204223

ในการทำงานบนระบบ unix โดยผ่านทาง shell ทั่วไป เราจะได้รับการอำนวยความสะดวกหลายอย่างจาก shell สิ่งหนึ่งก็คือการใช้งานแพทเทิร์นเพื่อระบุชื่อไฟล์ ทั้งหมดนี้อยู่ในสิ่งที่เราเรียกว่า word expansion (หรือ word substitution

ลักษณะของ word expansion ที่ shell ทำให้นั้น จะเป็นการขยายคำสั่งที่เราพิมพ์เข้าไป พิจารณาตัวอย่างของคำสั่ง echo เมื่อเรียกในไดเร็กทอรีที่มีไฟล์ test.c และ hello.c

$ echo hello world
hello world
$ echo hello world *.c
hello world test.c hello.c

shell จะขยายแพทเทิร์น *.c ด้วยไฟล์ที่ตรงกับที่แพทเทิร์นนั้นระบุ

แพทเทิร์น

เราสามารถใช้อักขระพิเศษต่อไปนี้ในการเขียนแพทเทิร์นได้

  • * --- จะแทนสตริงใด ๆ รวมถึงสตริงว่าง
  • ? --- จะแทนตัวอักษรตัวเดียว
  • [...] --- เราสามารถระบุเซตของตัวอักษรที่จะถูกแทนได้ เช่น [acd] จะแทนตัวอักษร a, c, หรือ d ก็ได้ โดยเราสามารถใช้เครื่องหมาย - ในการระบุได้ด้วย เช่น [a-z] จะแทนตัวอักษรภาษาอังกฤษพิมพ์เล็ก หรือ [a-z0-9] จะรวมตัวเลขเข้าไปด้วย นอกจากนี้ ถ้าเราขึ้นต้นรายการด้วย ^ หรือ ! เซตนี้จะแทนตัวอักษรทั้งหมดที่ไม่อยู่ในรายการ เช่น [^0-9] จะแทนตัวอักษรทั้งหมดที่ไม่ใช่ตัวเลข

ตัวอย่าง

เราสามารถใช้แพทเทิร์นเหล่านี้หลากหลาย เช่น

  • *.c --- แทนทุกแฟ้มข้อมูลที่มีนามสกุล c
  • */*.c --- แทนทุกแฟ้มข้อมูลที่มีนามสกุล c ในทุก ๆ ไดเร็กทอรีย่อย
  • */h*.cpp --- แทนทุกแฟ้มข้อมูลที่มีนามสกุล c ที่มีชื่อขึ้นต้นด้วย h ในทุก ๆ ไดเร็กทอรีย่อย
  • test?/h*.cpp --- แทนทุกแฟ้มข้อมูลที่มีนามสกุล c ที่มีชื่อขึ้นต้นด้วย h ในทุก ๆ ไดเร็กทอรีย่อยที่มีชื่อว่า test ตามด้วยตัวอักษรหนึ่งตัว
  • test[0-9]/out-[a-z] --- แทนแฟ้มขึ้นต้นด้วย out- ตามด้วยตัวอักษรภาษาอังกฤษตัวเล็ก ในไดเร็กทอรีย่อยที่มีชื่อว่า test ตามด้วยตัวเลขหนึ่งตัว

How To Speak More Wisely

It had been three weeks since my throat started to feel sore, and it wasnt getting better. The pain was most acute when I spoke. So I decided to spend a few days speaking as little as possible. Every time I had the urge to say something, I paused for a moment to question whether it was worth irritating my throat.

[How To Speak More Wisely]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]

What Ive Learned About Learning

We learn more by looking for the answer to a question and not finding it than we do from learning the answer itself. ~Lloyd AlexanderI am a teacher and an avid learner, and Im passionate about both.Im a teacher because I help Eva homeschool our kids OK, she does most of the work, but I do help, mostly with math but with everything else too.

[What Ive Learned About Learning]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]

9 Essential Skills Kids Should Learn

Kids in todays school system are not being prepared well for tomorrows world.As someone who went from the corporate world and then the government world to the ever-changing online world, I know how the world of yesterday is rapidly becoming irrelevant.

[9 Essential Skills Kids Should Learn]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]

Stop Using The Wrong Type of Intelligence

A man should hear a little music, read a little poetry, and see a fine picture every day of his life, in order that worldly cares may not obliterate the sense of the beautiful which God has implanted in the human soul. Johann Wolfgang von Goethe

[Stop Using The Wrong Type of Intelligence]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]

Love Everything, Be Attached To Nothing

As human beings we all want to be happy and free from misery we have learned that the key to happiness is inner peace. The greatest obstacles to inner peace are disturbing emotions such as anger, attachment, fear and suspicion, while love and compassion and a sense of universal responsibility are the sources of peace and happiness. Dalai Lama

[Love Everything, Be Attached To Nothing]

[GoodvilleNews.com - good, positive news, inspirational stories, articles]