6

私が持っていると言う:

>which -a foo
/bin/foo
/usr/bin/foo

私は次のようなものが欲しいです:

>foo
Warning: multiple foo in PATH
... foo gets executed ...

この機能により、今日は本当に多くの時間を節約できたでしょう。これはもっと早く起こっていると思っていたはずですが、最初は問題がはっきりしていなかったので、まったく逆の方向に掘り始めました。

4

2 に答える 2

6

ええと、あなたはそれをすることができます、しかしそれはあなたが思うかもしれないほど簡単ではありません。

まず、PATH内のすべてのディレクトリをチェックする関数を作成し、そこで実行しようとしているコマンドを探す必要があります。次に、この関数を現在のシェルのDEBUGトラップにバインドする必要があります。

私はそれを行う小さなスクリプトを書きました:

$ cat /tmp/1.sh

check_command()
{
    n=0
    DIRS=""
    for i in $(echo $PATH| tr : " ")
    do 
        if [ -x "$i/$1" ]
        then 
            n=$[n+1]
            DIRS="$DIRS $i"
        fi
    done
    if [ "$n" -gt 1 ]
    then
      echo "Warning: there are multiple commands in different PATH directories: "$DIRS
    fi
}

preexec () {
    check_command $1
}
preexec_invoke_exec () {
    [ -n "$COMP_LINE" ] && return  # do nothing if completing
    local this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`;
    preexec "$this_command"
}
trap 'preexec_invoke_exec' DEBUG

使用例:

$ . /tmp/1.sh
$ sudo cp /bin/date /usr/bin/test_it
$ sudo cp /bin/date /bin/test_it
$ test_it
Warning: there are multiple commands in different PATH directories:  /usr/bin /bin
Wed Jul 11 15:14:43 CEST 2012
$ 
于 2012-07-11T13:08:03.230 に答える
2

一般化するのは少しトリックですが、それは可能です。と魔法については、https://unix.stackexchange.com/q/42579/20437への私の回答を参照してください。checkSanity関数は次のようになります。historyPROMPT_COMMAND

checkSanity() {
  cmd="${1%% *}" # strip everything but the first word
  candidates=$(which -a $cmd | wc -l)
  if (( candidates > 1 )); then
    echo "Warning: multiple $cmd in PATH"
  fi
}

ただし、コマンドの開始時ではなく、コマンドの終了後に警告が出力されます。代わりにDEBUGトラップを使用して、目的の結果を取得します。

trap 'checkSanity "$BASH_COMMAND"' DEBUG
于 2012-07-11T13:05:02.650 に答える