ในการตรวจสอบว่าคำสั่งบางคำสั่งทำงานได้สำเร็จหรือไม่คุณสามารถตรวจสอบสถานะการส่งคืนที่ได้รับจาก$?
คำสั่งก่อนหน้าด้วย:
echo $?
สถานะการส่งคืน0
หมายถึงคำสั่งที่ดำเนินการสำเร็จแล้วในขณะที่เอาต์พุตที่ไม่เป็นศูนย์ ( รหัสข้อผิดพลาด ) หมายถึงพบปัญหาบางอย่างหรือมีข้อผิดพลาดและสามารถทราบหมวดหมู่ได้จากรหัสข้อผิดพลาด รหัสข้อผิดพลาดลินุกซ์ / C ที่กำหนดไว้ในและ/usr/include/asm-generic/errno-base.h
/usr/include/asm-generic/errno.h
นอกจากนี้ในทุบตี.bashrc
กำหนดนามแฝงalert
ซึ่งสามารถใช้ในการแจ้งเตือนด้วยสถานะของความสำเร็จ คุณจะต้องแนบนามแฝงกับคำสั่งหรือคำสั่งผสมเช่นนี้:
some_command --some-switch; alert
คุณสามารถผนวกบรรทัดของรหัสต่อไปนี้ไปยัง~/.bashrc
ไฟล์ของคุณเพื่อแสดงสถานะการส่งคืนของคำสั่งสุดท้ายที่ดำเนินการ ..
# show the return code of last command executed
PS1='${debian_chroot:+($debian_chroot)}\u@\h(lst ret. $(echo $?) ):\w\$ '
(เปิดไฟล์~/.bashrc
พร้อมตัวแก้ไขข้อความที่คุณเลือกและคัดลอกบรรทัดข้างต้นวางไว้ในไฟล์และบันทึกเรียกใช้อินสแตนซ์ใหม่ของเทอร์มินัลและคุณควรเปิดใช้งานหรือคุณสามารถกำหนดฟังก์ชันและใช้งานแทน มันมีPS1
เหมือนเช่นภาพประกอบด้านล่าง.)
ตัวอย่างเล็ก ๆ น้อย ๆ :
hash@precise(lst ret. 0 ):~$ ls -sh someFileThatsNotThere
ls: cannot access someFileThatsNotThere: No such file or directory
hash@precise(lst ret. 2 ):~$
hash@precise(lst ret. 2 ):~$ aCommandThatsNot
aCommandThatsNot: command not found
hash@precise(lst ret. 127 ):~$
hash@precise(lst ret. 127 ):~$ echo "you should get a lst ret. 0, I believe the system has echo installed :)"
you should get a lst ret. 0, I believe the system has echo installed :)
hash@precise(lst ret. 0 ):~$
hash@precise(lst ret. 0 ):~$ sudo touch /tmp/someTestFile
[sudo] password for hash:
hash@precise(lst ret. 1 ):~$
hash@precise(lst ret. 1 ):~$ chown $USER:$USER /tmp/someTestFile
chown: changing ownership of `/tmp/someTestFile': Operation not permitted
เพิ่งเล่นกับPS1
:) .. อีกนิด
function showRetStat {
## line1: initiliazing retStat with the return status of the previous command
retStat=$?
## line2: Left padding the return status with spaces. If you prefer the unpadded one, you can just replace
# $retStatFtd in the lines initializing noErrStr and errStr among other possible ways.
retStatFtd=$(sed -e :a -e 's/^.\{1,2\}$/ &/;ta' <<< $retStat)
## lines3&4: Setting the strings to display for a successful and unsuccessful run of previous command
# which we are going to display with the prompt string. Change the strings to display text of your
# choice like you may set noErrStr="yippie!" , errStr="oopsie!" in place of what they're now.
noErrStr="retStat "$retStatFtd" :: PASS ^_^"
errStr="retStat "$retStatFtd" :: FAIL x_x"
## line5: Applying the logic and display the proper string at the prompt. Space padded number i.e. retStatFtd, here,
# worked in the logic, originally I intended to use this for the display while retStat in the conditional
# check; you could make the function one statement less if you want to.
echo "$([ $retStatFtd = 0 ] && echo "$noErrStr" || echo "$errStr")"
}
## Combining the function showRetStat into the prompt string.
PS1='${debian_chroot:+($debian_chroot)}\u@\h($(showRetStat)):\w\$ '
(คุณสามารถปรับเปลี่ยนฟังก์ชั่นเพื่อทำให้แฟนซีมากขึ้นเช่น @gronostaj ทำในโพสต์ของเขา)