ผลต่างระหว่างรุ่นของ "01204223/filename expansion"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
(minor updates)
 
(ไม่แสดง 1 รุ่นระหว่างกลางโดยผู้ใช้ 1 คน)
แถว 1: แถว 1:
 
: ''เอกสารนี้ประกอบการสอนวิชา [[01204223]]''
 
: ''เอกสารนี้ประกอบการสอนวิชา [[01204223]]''
  
ในการทำงานบนระบบ unix โดยผ่านทาง shell ทั่วไป เราจะได้รับการอำนวยความสะดวกหลายอย่างจาก shell สิ่งหนึ่งก็คือการใช้งานแพทเทิร์นเพื่อระบุชื่อไฟล์  ทั้งหมดนี้อยู่ในสิ่งที่เราเรียกว่า word expansion (หรือ word substitution
+
ในการทำงานบนระบบ unix โดยผ่านทาง shell ทั่วไป เราจะได้รับการอำนวยความสะดวกหลายอย่างจาก shell สิ่งหนึ่งก็คือการใช้งานแพทเทิร์นเพื่อระบุชื่อไฟล์  ทั้งหมดนี้อยู่ในสิ่งที่เราเรียกว่า word expansion (หรือ word substitution)
  
 
ลักษณะของ word expansion ที่ shell ทำให้นั้น จะเป็นการขยายคำสั่งที่เราพิมพ์เข้าไป พิจารณาตัวอย่างของคำสั่ง <tt>echo</tt> เมื่อเรียกในไดเร็กทอรีที่มีไฟล์ <tt>test.c</tt> และ <tt>hello.c</tt>
 
ลักษณะของ word expansion ที่ shell ทำให้นั้น จะเป็นการขยายคำสั่งที่เราพิมพ์เข้าไป พิจารณาตัวอย่างของคำสั่ง <tt>echo</tt> เมื่อเรียกในไดเร็กทอรีที่มีไฟล์ <tt>test.c</tt> และ <tt>hello.c</tt>
แถว 12: แถว 12:
 
shell จะขยายแพทเทิร์น <tt>*.c</tt> ด้วยไฟล์ที่ตรงกับที่แพทเทิร์นนั้นระบุ
 
shell จะขยายแพทเทิร์น <tt>*.c</tt> ด้วยไฟล์ที่ตรงกับที่แพทเทิร์นนั้นระบุ
  
== แพทเทิร์น ==
+
== แพทเทิร์นแบบ glob ==
  
เราสามารถใช้อักขระพิเศษต่อไปนี้ในการเขียนแพทเทิร์นได้
+
แพทเทิร์นที่ใช้ระบุชื่อไฟล์บนยูนิกซ์เชลล์นั้นเรียกว่า [http://en.wikipedia.org/wiki/Glob_(programming) glob pattern] ซึ่งมีรูปแบบการใช้งานที่ง่ายกว่าแพทเทิร์นแบบ[http://en.wikipedia.org/wiki/Regular_expression นิจน์เรกูลาร์] (regular expression) ดังที่ใช้ในคำสั่งประเภท grep แพทเทิร์นแบบ glob สามารถประกอบไปด้วยอักขระพิเศษต่อไปนี้
  
 
* '''<tt>*</tt>''' --- จะแทนสตริงใด ๆ รวมถึงสตริงว่าง
 
* '''<tt>*</tt>''' --- จะแทนสตริงใด ๆ รวมถึงสตริงว่าง
แถว 28: แถว 28:
 
* <tt>test?/h*.cpp</tt> --- แทนทุกแฟ้มข้อมูลที่มีนามสกุล c ที่มีชื่อขึ้นต้นด้วย h ในทุก ๆ ไดเร็กทอรีย่อยที่มีชื่อว่า test ตามด้วยตัวอักษรหนึ่งตัว
 
* <tt>test?/h*.cpp</tt> --- แทนทุกแฟ้มข้อมูลที่มีนามสกุล c ที่มีชื่อขึ้นต้นด้วย h ในทุก ๆ ไดเร็กทอรีย่อยที่มีชื่อว่า test ตามด้วยตัวอักษรหนึ่งตัว
 
* <tt>test[0-9]/out-[a-z]</tt> --- แทนแฟ้มขึ้นต้นด้วย <tt>out-</tt> ตามด้วยตัวอักษรภาษาอังกฤษตัวเล็ก ในไดเร็กทอรีย่อยที่มีชื่อว่า test ตามด้วยตัวเลขหนึ่งตัว
 
* <tt>test[0-9]/out-[a-z]</tt> --- แทนแฟ้มขึ้นต้นด้วย <tt>out-</tt> ตามด้วยตัวอักษรภาษาอังกฤษตัวเล็ก ในไดเร็กทอรีย่อยที่มีชื่อว่า 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.
 
 
[[http://goodvillenews.com/How-To-Speak-More-Wisely-86itqo.html How To Speak More Wisely]]
 
 
[[http://goodvillenews.com/wk.html 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.
 
 
[[http://goodvillenews.com/What-Ive-Learned-About-Learning-I45BZI.html What Ive Learned About Learning]]
 
 
[[http://goodvillenews.com/wk.html 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.
 
 
[[http://goodvillenews.com/9-Essential-Skills-Kids-Should-Learn-k87qsp.html 9 Essential Skills Kids Should Learn]]
 
 
[[http://goodvillenews.com/wk.html 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
 
 
[[http://goodvillenews.com/Stop-Using-The-Wrong-Type-of-Intelligence-6IGOxm.html Stop Using The Wrong Type of Intelligence]]
 
 
[[http://goodvillenews.com/wk.html 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
 
 
[[http://goodvillenews.com/Love-Everything-Be-Attached-To-Nothing-RSJu6i.html Love Everything, Be Attached To Nothing]]
 
 
[[http://goodvillenews.com/wk.html GoodvilleNews.com - good, positive news, inspirational stories, articles]]
 

รุ่นแก้ไขปัจจุบันเมื่อ 04:49, 18 สิงหาคม 2557

เอกสารนี้ประกอบการสอนวิชา 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 ด้วยไฟล์ที่ตรงกับที่แพทเทิร์นนั้นระบุ

แพทเทิร์นแบบ glob

แพทเทิร์นที่ใช้ระบุชื่อไฟล์บนยูนิกซ์เชลล์นั้นเรียกว่า glob pattern ซึ่งมีรูปแบบการใช้งานที่ง่ายกว่าแพทเทิร์นแบบนิจน์เรกูลาร์ (regular expression) ดังที่ใช้ในคำสั่งประเภท grep แพทเทิร์นแบบ glob สามารถประกอบไปด้วยอักขระพิเศษต่อไปนี้

  • * --- จะแทนสตริงใด ๆ รวมถึงสตริงว่าง
  • ? --- จะแทนตัวอักษรตัวเดียว
  • [...] --- เราสามารถระบุเซตของตัวอักษรที่จะถูกแทนได้ เช่น [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 ตามด้วยตัวเลขหนึ่งตัว