在bash中,当我通过实现precmd()功能时trap ??? DEBUG
,如果precmd()调用中的命令返回非零返回代码,我可以使键入的命令无法执行;通过使用set -o functrace
和shopt -s extdebug
.
我正在尝试通过 tcshpostcmd
中的别名在 tcsh 中实现类似的功能。但是,我无法在 tcsh 文档中找到有关如何执行与 bash 相同的操作的任何详细信息(如果命令postcmd
失败并返回非零 RC,则不要执行在提示符下键入的命令)。
我怎样才能在tcsh中达到这种效果?
期望的结果:
# Set up postcmd alias to check of current user is "OK" (in OK file)
MYUSER:~> alias postcmd 'grep $USER /tmp/ok_users > /dev/null'
# Add myself to OK list (in another xterm)
MYUSER:~> echo "$USER" > ! /tmp/ok_users
MYUSER:~> cat /tmp/ok_users
MYUSER
MYUSER:~> grep $USER /tmp/ok_users | wc -l; echo $?
1
0
# Run command - expect it to succeed:
MYUSER:~> echo TEST2
TEST2 # <==== As expected
##########
# Now, let's make sure I'm NOT in OK users (in another xterm).
MYUSER:~> echo "junk" > ! /tmp/ok_users
MYUSER:~> cat /tmp/ok_users
junk
MYUSER:~> grep $USER /tmp/ok_users | wc -l ; echo $?
0
1
# Now, what I need is, for my typed command to NOT be executed
# due to "postcmd" hook command (grep) failing
MYUSER:~> echo TEST2
MYUSER:~> <==== Want to NOT echo anything. How do I do that?
我希望答案是操作系统可移植的,但如果需要,至少它必须在 RedHat Enterprise Linux 6 上运行