gpt4 book ai didi

linux - 自定义 bash 完成的提示

转载 作者:IT王子 更新时间:2023-10-29 00:38:14 24 4
gpt4 key购买 nike

我正在开发自定义 bash 完成命令以从调度系统(LSF、PBS、SLURM)捕获作业 ID。我已经有了基本的功能,但我现在想用我在运行 zsh 时看到的“提示”来扩展它。

例如,当我在下面的 grep 示例中按 TAB 键时,我得到:

grep -<TAB>
--after-context -A -- specify lines of trailing context
--basic-regexp -G -- use basic regular expression
--before-context -B -- specify lines of leading context
...

-- 之后的第三列是我想添加到我自己的 bash 完成中的内容。它的正确技术术语是什么?提示? compgen 是否提供执行此操作的功能?

我附上了我目前的工作示例,它只提供 ID。该示例使用 LSF。

# LSF Job ID completion
function _mycurrentjobs()
{
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "$(bjobs -noheader -u $USER -o JOBID)" -- $cur))
return 0
}
complete -F _mycurrentjobs bkill bjobs bstatus bpeek bstop bresume

提供 ID 和我想要的提示的命令是:bjobs -noheader -u $USER -o "JOBID JOB_NAME"

最佳答案

在查看有关主机完成的类似帖子后 bash autocompletion: add description for possible completions我或多或少得到了正确的行为。我在我的作业 ID 查询命令中使用 - 作为分隔符

function _mycurrentjobs()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local OLDIFS="$IFS"
local IFS=$'\n'
COMPREPLY=( $(compgen -W "$(bjobs -noheader -u $USER \
-o "JOBID JOB_NAME delimiter='-'")" -- $cur))
IFS="$OLDIFS"
if [[ ${#COMPREPLY[*]} -eq 1 ]]; then #Only one completion
COMPREPLY=( ${COMPREPLY[0]%%-*} ) #Remove the separator and everything after
fi
return 0
}
complete -F _mycurrentjobs bkill bjobs bstatus bpeek bstop bresume

关于linux - 自定义 bash 完成的提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45792960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com