यह जाँचने के लिए कि क्या कुछ कमांड सफलतापूर्वक काम कर रहा है या नहीं, आप पिछली स्थिति के अनुसार दिए गए रिटर्न स्टेटस की जाँच कर सकते हैं $?:
echo $?
वापसी की स्थिति का 0मतलब है कि कमांड सफलतापूर्वक पूरा हो गया है, जबकि एक गैर-शून्य आउटपुट ( त्रुटि कोड ) का अर्थ होगा कि कुछ समस्याएं सामने आई थीं या कोई त्रुटि हुई है और श्रेणी को त्रुटि कोड से जाना जा सकता है। लिनक्स / सी त्रुटि कोड में परिभाषित कर रहे हैं /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 जैसी कुछ अपनी पोस्ट में करता है।)