ที่ Shell prompt พิมพ์คำสั่ง man date ได้อะไรมาไม่รู้เยอะแยะ… จากคู่มือจะเอารูปแบบวันที่ 12-09-2017 ตัวเลือกที่เกี่ยวข้องได้แก่ %d %D %e %F %g %G %m %y %Y เป็นต้น ลองส่งคำสั่ง date +”%d-%m-%Y” ได้ผลลัพธ์ 12-09-2017 ตรงตามที่ต้องการ มาเขียนสคริปต์กันหน่อย อยากได้เมื่อวานทำไง วันนี้เล่น tcsh shell สร้างแฟ้ม date.tcsh ด้วย editor ที่ชื่นชอบมีข้อความว่า #!/bin/tcsh -f set tday=`date +”%d”` set tmonth=`date +”%m”` set tyear=`date +”%Y”` echo “Today is ${tday}-${tmonth}-${tyear}.” set yday=`expr ${tday} – 1` echo “Yesterday was ${yday}-${tmonth}-${tyear}.” ทดสอบสคริปต์ด้วยคำสั่ง tcsh date.tcsh ไม่อยากพิมพ์ tcsh ทุกครั้งเพิ่ม execution bit ด้วยคำสั่ง chmod +x date.tcsh เรียกใช้ได้โดยพิมพ์ ./date.tcsh (อ่านว่า จุด-ทับ-เดต-จุด-ที-ซี-เอส-เอช) ผลลัพธ์ที่ได้ Today is 12-09-2017. Yesterday was 11-09-2017. อยากได้เมื่อวานทำไมมันยากอย่างนี้ ฮา… ซึ่งเมื่อกลับไปอ่านคู่มือ (man date) ให้ดี..อีกครั้งจะพบว่ามีตัวเลือก -d, –date=STRING display time described by STRING, not ‘now’ และเมื่่อเลื่อนลงมาล่างสุดจะพบว่า DATE STRING The –date=STRING is a mostly free format human readable date string such as “Sun, 29 Feb 2004 16:21:42 -0800” or “2004-02-29 16:21:42” or even “next Thursday”. A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. An empty string indicates the beginning of the day. The date string format is more complex than is easily documented here but is fully described in the info documentation. โอ้ววว มันเขียนไว้หมดแล้ว… เขียนใหม่ได้ว่า date -d yesterday ได้ผลลัพธ์ Mon Sep 11 21:43:51 +07 2017 เปลี่ยนให้ผลลัพธ์ออกมาในรูปแบบที่ต้องการได้ด้วยคำสั่ง date -d yesterday +”%d-%m-%Y” ก็จะได้ผลลัพธ์ว่า 11-09-2017 แก้สคริปต์