Hai.Xu
Hai.Xu

Reputation: 50

how to check the command name is "find" and exit prompt before command prompt in zsh

any can help me?

original

➜  ~ find
here preexec block...
usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression]
       find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]

I want do it like this!

➜  ~ find
here preexec block...
haha!find disable by super manager
➜  ~

I tried to use the preexec precmd hook function in zsh.define zshrc

precmd(){
  # echo 'here precmd block...'
  echo 'here precmd block...'
  if [[ "npm" == "$1" ]];
  then
    echo 'haha 123'
    exit 0
  fi
}

preexec(){
  echo 'here preexec block...'
  if [[ "npm" == "$1" ]];
  then
    echo 'haha 123'
    exit 0
  fi
}

But the result is killing the shell.

Upvotes: 0

Views: 311

Answers (1)

Masklinn
Masklinn

Reputation: 42272

This is a variant of Use preexec() to evaluate entered command

The gist is preexec executes in the shell's context (so kills the shell if you exit), and it doesn't provide for altering or cancelling the command being executed, but you should be able to hook into accept-buffer to do what you want.

A much simpler option is to simply alias the executable you want to disable to something else:

> alias find=echo
> find . -name 'foo'
. -name foo

though it will only work on generic lookups, not lookups specifically through /bin/env or the executable's path.

Upvotes: 1

Related Questions