2

In bash, when I implement precmd() functionality via trap ??? DEBUG, I can make the typed command fail to execute, if the command in precmd() call returns non-zero return code; by using set -o functrace and shopt -s extdebug.

I'm trying to implement similar functionality in tcsh via postcmd alias in tcsh. However, I was unable to find any details in tcsh documentation on how to do the same thing as in bash (if the command in postcmd fails and returns non-zero RC, don't execute the command typed at the prompt).

How can I achieve that effect in tcsh?

Desired outcome:

# 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?

I'd prefer the answer to be OS portable, but if required, at least it must work on RedHat Enterprise Linux 6

DVK
  • 321

0 Answers0