有时当我运行命令时它不显示输出,所以我不确定它们是否有效。是否可以让所有命令都有反馈,如果它们运行正确与否?或者至少显示他们运行的反馈 ID(正确与否)
pre*_*ise 13
要检查某个命令是否成功运行,您可以使用以下命令检查上一个命令的返回状态,由 给出$?:
echo $?
Run Code Online (Sandbox Code Playgroud)
返回状态0表示命令成功完成,而非零输出(错误代码)表示遇到了一些问题或存在错误,并且可以从错误代码中知道类别。Linux/C 错误代码在/usr/include/asm-generic/errno-base.h和中定义/usr/include/asm-generic/errno.h。
同样在 bash 中,.bashrc定义了一个别名alert,可用于通知完成状态。您必须使用命令或命令组合附加别名,如下所示:
some_command --some-switch; alert
Run Code Online (Sandbox Code Playgroud)
您可以将以下代码行附加到您的~/.bashrc文件中,以显示最后执行的命令的返回状态。
Run Code Online (Sandbox Code Playgroud)# 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
Run Code Online (Sandbox Code Playgroud)
只是玩PS1:) ..多一点,
Run Code Online (Sandbox Code Playgroud)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 在他的帖子中所做的那样。)
Ben*_*n R 11
(我认为由于您在Ask Ubuntu中发帖,我们可以假设您在谈论默认 shell,即Bash。)
Stack Overflow question In a shell script: echo shell commands as they are execution(这不仅仅是一个特定于 Ubuntu 的解决方案)中有一个很好的答案。
您需要做的是使用set命令打开verbose 或xtrace。
set -o
Run Code Online (Sandbox Code Playgroud)
会给你一个列表,列出哪些当前参数被切换到on或off。
set -v
Run Code Online (Sandbox Code Playgroud)
或长版本:
set -o verbose
Run Code Online (Sandbox Code Playgroud)
将详细打开。
不过,我认为您想要的实际上是 xtrace。这不仅会回显您运行的每个命令,还会扩展参数并为您提供更多反馈。因此,如果我做一些像在终端输入 'hi' 这样愚蠢的事情,我将得到我输入内容的回显以及 shell 尝试执行命令 'hi' 所做的操作的报告/跟踪(见下面的截图) ):

要启用 xtrace:
set -x
Run Code Online (Sandbox Code Playgroud)
或者:
set -o xtrace
Run Code Online (Sandbox Code Playgroud)
要禁用这些参数,您(违反直觉)调用相同的命令,但使用加号+而不是破折号或减号,例如:
set +v
Run Code Online (Sandbox Code Playgroud)
将详细关闭,类似地:
set +x
Run Code Online (Sandbox Code Playgroud)
会变成X跟踪关闭。
关于 shell 选项的详细指南在第 33 章选项,高级 Bash 脚本指南。
您可以更改命令提示符以在上一个命令以 0 退出时显示绿色勾号,否则显示红色 X。Arch Linux Wiki有一些不错的代码可以添加到您的bash.rc:
set_prompt () {
Last_Command=$? # Must come first!
Blue='\[\e[01;34m\]'
White='\[\e[01;37m\]'
Red='\[\e[01;31m\]'
Green='\[\e[01;32m\]'
Reset='\[\e[00m\]'
FancyX='\342\234\227'
Checkmark='\342\234\223'
# Add a bright white exit status for the last command
#PS1="$White\$? "
# If it was successful, print a green check mark. Otherwise, print
# a red X.
if [[ $Last_Command == 0 ]]; then
PS1+="$Green$Checkmark "
else
PS1+="$Red$FancyX "
fi
# If root, just print the host in red. Otherwise, print the current user
# and host in green.
if [[ $EUID == 0 ]]; then
PS1+="$Red\\h "
else
PS1+="$Green\\u@\\h "
fi
# Print the working directory and prompt marker in blue, and reset
# the text color to the default.
PS1+="$Blue\\w \\\$$Reset "
}
PROMPT_COMMAND='set_prompt'
Run Code Online (Sandbox Code Playgroud)
(我已经停用实际的错误代码,因为我不喜欢它,如果你想看到确切的代码只是删除#此行:#PS1="$White\$? ")
这是它的外观:

