如何根据旧定义重新定义 bash 函数?

如何根据旧定义重新定义 bash 函数?

有什么方法可以根据旧定义重新定义 bash 函数吗?例如,我想将以下代码块添加到函数的前导码中command_not_found_handle ()

# Check if $1 is instead a bash variable and print value if it is
local VAL=$(eval echo \"\$$1\")
if [ -n "$VAL" ] && [ $# -eq 1 ]; then
    echo "$1=$VAL"
    return $?
fi

它当前在 /etc/profile.d/PackageKit.sh 中定义,并由 bash 启动脚本获取。

这样,我只需键入变量名称即可在命令提示符下查询环境变量的值(前提是不存在该名称的此类命令)。例如

user@hostname ~:$ LANG
LANG=en_AU.utf8

我知道我可以复制并粘贴当前定义并在 中添加我自己的更改~/.bashrc,但我正在寻找一种涉及代码重用的更优雅的方法。

实现我的目标或代码改进/扩展的更好方法也值得赞赏。

ベストアンサー1

您可以打印出函数的当前定义,然后将其包含在子句内的函数定义中eval

current_definition=$(declare -f command_not_found_handle)
current_definition=${current_definition#*\{}
current_definition=${current_definition%\}}
prefix_to_add=$(cat <<'EOF'
  # insert code here (no special quoting required)
EOF
)
suffix_to_add=$(cat <<'EOF'
  # insert code here (no special quoting required)
EOF
)
eval "command_not_found_handle () {
  $prefix_to_add
  $current_definition
  $suffix_to_add
}"

我发现更清晰的另一种方法是用新名称定义原始函数,然后从您的定义中调用它。仅当您不需要对原始定义的局部变量进行操作时,这才有效。

eval "original_$(declare -f command_not_found_handle)"
command_not_found_handle () {
  original_command_not_found_handle
}

おすすめ記事