โดยปกติ Shell ใน Linux จะมีการตั้งค่าในการแสดงผลต่างๆไว้ ทำให้เวลาเข้าใช้งานผู้ใช้งานสามารถทราบรายละเอียดของการทำงานต่างๆ ณ ปัจจุบันของตัวผู้ใช้งานเองได้ โดยตัวแปรที่เก็บและกำหนดลักษณะการแสดงผลต่างๆของ Shell มีดังนี้
- PS1 – เป็นตัวแปรที่เก็บค่าของ default prompt (ด้านซ้ายสุดที่จะขึ้นทุกครั้งที่อยู่ในงาน Shell)
- PS2 – เป็นตัวแปรที่เก็บค่าของ prompt เมื่อมีการใส่คำสั่งหลายๆบรรทัด
- PS3 – เป็นตัวแปรที่จะใข้งานเมื่อมีการเลือกคำสั่งตอนใช้งาน Bash Script
- PS4 – เป็นตัวแปรเก็บค่าการแสดงผล Bash script แต่ละบรรทัดตอนทำ Debug Mode
1. แก้ไข Bash Prompt PS1
อย่างที่บอกไปแล้วว่า PS1 เป็น ตัวแปรที่เก็บค่าของ default prompt เรามาลองดูกันว่า default เป็นยังไง
1 |
echo $PS1 |
จะเห็นว่ามีการใส่ “debian_chroot” หมายถึงว่าถ้ามีการเปลี่ยน root environment จะมีการเตือนด้วย โดยในส่วนของ PS1 นั้นเราสามารถใส่ option ต่างๆได้ดังนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
\a The ASCII bell character (you can also type \007) \d Date in “Sat Sep 04″ format \e ASCII escape character (you can also type \033) \h First part of hostname (such as “mybox”) \H Full hostname (such as “mybox.mydomain.com”) \j The number of processes you’ve suspended in this shell by hitting ^Z \l The name of the shell’s terminal device (such as “ttyp4″) \n Newline \r Carriage return \s The name of the shell executable (such as “bash”) \t Time in 24-hour format (such as “23:59:59″) \T Time in 12-hour format (such as “11:59:59″) \@ Time in 12-hour format with am/pm \u Your username \v Version of bash (such as 2.04) \V Bash version, including patchlevel \w Current working directory (such as “/home/koithara”) \W The “basename” of the current working directory (such as “koithara”) \! Current command’s position in the history buffer \# Command number (this will count up at each prompt, as long as you type something) \$ If you are not root, inserts a “$”; if you are root, you get a “#” \xxx Inserts an ASCII character based on three-digit number xxx (replace unused digits with zeros, such as “\007″) \\ A backslash \[ This sequence should appear before a sequence of characters that don’t move the cursor (like color escape sequences). This allows bash to calculate word wrapping correctly. \] This sequence should appear after a sequence of non-printing characters. |
ทีนี้ถ้าผมแก้ใหม่โดยการให้แสดงเวลาในการใช้งานของ prompt shell ด้วย โดยเพิ่มเวลาเอาไปไว้ท้ายสุดของ prompt ก็ทำการ เพิ่ม \t เข้าไปด้านท้ายสุด จะได้เป็น
1 |
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m \t\]\$" |
หากต้องการแก้แบบถาวรเราสามารถทำได้โดยการแก้ .bashrc ของแต่ละ user
2. แก้ไข continuation prompt shell ใน PS2
อย่างที่กล่าวไปแล้วว่า PS2 เป็นตัวแปรที่เก็บค่าของ prompt เมื่อมีการใส่คำสั่งหลายๆบรรทัด ซึ่งโดยปกติถ้าเราพิมพ์คำสั่งเป็น
1 2 |
apt-get update && \ apt-get upgrade -y |
โดย default PS2 จะกำหนดเป็น > ทำให้เวลาแสดงผลมีค่าเป็น
ทีนี้หากต้องการเปลี่ยนให้เป็น “continue>” จะใช้คำสั่งเป็น
1 |
export PS2="continue>" |
3. เปลี่ยนการเลือกคำสั่งของ Bash Script โดยใช้ PS3
หากเราเขียน shell script ให้มีการรับ input จาก user
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash echo "please select a value to display a month on the list below" select i in jan feb mar apr exit do case $i in jan) echo "January";; feb) echo "February";; mar) echo "March";; exit) exit;; esac done |
จะเห็นว่าตอนที่รอรับ input จาก user จะมีการแสดง “#?” ขึ้นมา หากเราต้องการเปลี่ยนคำนี้เป็น “choice: ” เราสามารถกำหนดได้ผ่าน PS3 เป็น
1 |
export PS3="choice: " |
4. กำหนดการแสดงผลแต่ละบรรทัดที่ Debug ใน Shell Script ด้วย PS4
โดยปกติการ debug ใน Bash script เราสามารถที่จะใช้ echo เองในแต่ละขั้นตอนได้ แต่เพื่อให้สะดวกกว่านั้น เราสามารถใช้ PS4 ซึ่งเป็นตัวแปรเก็บค่าการแสดงผล Bash script แต่ละบรรทัดตอนทำ Debug Mode แทนได้ โดยปกติ default ของ PS4 คือ “+” หากเราต้องการกำหนดเป็น “[script line->:] ” ใช้คำสั่งเป็น
1 |
export PS4="[script line->:] " |
Source:: Linoxide