Category: Shell

  • How to disable visual mode on mouse select for vim

    How to disable visual mode on mouse select for vim

    หากใครใช้ vi command แล้วพบว่าเวลา select ด้วย mouse แล้วพบว่าเข้า visual mode ซึ่งบางครั้งก็สร้างปัญหาในการ copy และ paste

  • How to convert string to lower case

    วิธีการ convert string จาก Upper case (ตัวใหญ่) ไปเป็น Lower case (ตัวเล็ก) บน bash shell สามารถทำได้หลายวิธีดังนี้ ใช้คำสั่ง tr [tum@ezylinux ~]$ echo ‘Hello World!’ | tr ‘[:upper:]’ ‘[:lower:]’ hello world! ใช้คำสั่ง awk…

  • How to get a day before current time in Bash

    How to get a day before current time in Bash

    การแสดงวันที่ของเมื่อวานหรือก่อนหน้า บน bash ทำได้โดยใช้ option -d ดังตัวอย่างดังนี้ แสดงวันที่เมื่อวาน

  • How to trim space from bash variable

    How to trim space from bash variable

    Tip เล็กๆ ในการลบ space (ช่องว่าง) ที่อยู่ในตัวแปรของ bash shell ต้องการลบ space ด้านหลัง ให้ใช้ sed ‘s/ *$//g’ ตัวอย่าง echo ” www ezylinux com ” | sed ‘s/ *$//g’

  • How to count blank lines and non-blank lines in a file

    How to count blank lines and non-blank lines in a file

    เทคนิคสำหรับการนับบรรทัดว่างและการนับบรรทัดที่ไม่ว่างใน shell เพื่อนำไปต่อยอดการใช้งานหรือแก้ไขปัญหาต่างๆครับ วิธีการนับบรรทัดว่างในไฟล์ [tum@ezylinux ~]$ grep “^$” /path/to/textfile | wc -l

  • How to redirect stdout and stderr to log file

    How to redirect stdout and stderr to log file

    วิธีการ redirect standard output และ standard error ไปเก็บไว้ยัง log file การ redirect stdout and stderr ไปยัง log file ที่ถูกต้อง command > /path/to/log 2>&1 ระบบจะทำการ redirect stdout ไปยัง log file และพร้อมกับ…

  • If / else statements on bash script

    If / else statements on bash script

    สรุปการใช้ If / else statements บน Bash script พร้อมทั้งเงื่อนไขการเปรียบเทียบ ใน bash จะมีการใช้งาน If / else statements ดังนี้

  • How to fix “unary operator expected error” in bash script

    How to fix “unary operator expected error” in bash script

    หากเขียน shell script แล้วเมื่อเวลารันทดสอบเกิด error ตามตัวอย่างด้านล่าง [: =: unary operator expected error ให้ลองตรวจสอบเงื่อนไข if ในบรรทัดที่ฟ้อง error ดูว่าตัวแปรมีค่าหรือไม่ และมีการเขียน code แบบ [ $Ans = “Y” ] หรือไม่ ถ้าใช่ให้ลองให้ไขเป็น

  • How to extract a single file / directory from tar archive

    การ extract บาง file หรือบาง directory ออกจาก tarball บน UNIX หรือ Linux สามารถทำได้โดยไม่ต้องทำการ extract file หรือ directory ออกจาก tar ทั้งหมดก่อน ซึ่งจะช่วยเพิ่มความสะดวกและประหยัดเนื่อที่เมื่อมีการ extract file หรือ directory วิธีการ extract บาง file หรือบาง…

  • How to redirect stderr and stdout  to a file

    How to redirect stderr and stdout to a file

    การจัดการ output ใน linux ไม่ใช่เรื่องที่ไกลตัว เนื่องจากบ่อยครั้งที่เราจะเห็นคำสั่งต่างๆ มีการ redirect error และ output ไปยังไฟล์ เพื่อทำการจัดเก็บและสามารถนำไปตรวจสอบได้ภายหลัง โดยเฉพาะอย่างยิ่งคำสั่งหรือโปรแกรมที่เมื่อรันแล้วมีการแสดงข้อมูลมากทำให้เราไม่สามารถอ่านได้ทันจำเป็นต้องทำการ redirect ไปเก็บไว้ยังไฟล์ เพื่อที่เราจะสามารถเปิดอ่านข้อมูลของการทำงานได้อย่างครบถ้วน บทความนี้เราจะมาพูดถึงเรื่องของการจัดการ output และ error ในขณะที่ได้มีการรันคำสั่ง