修改Shell脚本实现gnu-pass密码在dmenu-mac全场景展示
原脚本仅能匹配单目录下的Pass条目(如Social/example@email.com),无法处理以下两种场景:
- 无目录的直接条目(如
example@email.com) - 多级目录下的条目(如
Social/Personal/example@email.com)
修改后的完整脚本
#!/usr/bin/env bash # 开启globstar以支持多级目录递归匹配 shopt -s globstar typeit=0 if [[ $1 == "--type" ]]; then typeit=1 shift fi if [[ -n $DISPLAY ]]; then dmenu=/opt/homebrew/bin/dmenu-mac xdotool="xdotool type --clearmodifiers --file -" else echo "Error: No Wayland or X11 display detected" >&2 exit 1 fi prefix=${PASSWORD_STORE_DIR-~/.password-store} # 同时捕获根目录下的直接条目和所有子目录下的条目 password_files=( "$prefix"/*.gpg "$prefix"/**/*.gpg ) # 移除前缀路径 password_files=( "${password_files[@]#"$prefix"/}" ) # 移除.gpg后缀 password_files=( "${password_files[@]%.gpg}" ) # 过滤空条目(防止根目录无直接条目时产生空值) password_files=( "${password_files[@]//\/\.gpg/}" ) password=$(printf '%s\n' "${password_files[@]}" | "$dmenu" "$@") [[ -n $password ]] || exit if [[ $typeit -eq 0 ]]; then pass show -c "$password" 2>/dev/null else pass show "$password" | { IFS= read -r pass; printf %s "$pass"; } | $xdotool fi
关键修改说明
- 开启多级目录匹配:通过
shopt -s globstar让Bash的**通配符支持递归匹配所有层级的子目录,解决多级目录条目无法被捕获的问题。 - 扩展文件匹配规则:将文件路径匹配改为
"$prefix"/*.gpg "$prefix"/**/*.gpg,同时覆盖根目录下的直接条目和所有子目录下的条目。 - 过滤无效条目:添加空条目过滤逻辑,避免根目录无直接条目时,匹配规则产生无效的空字符串选项。
内容的提问来源于stack exchange,提问作者gachikuku
相关产品推荐
相关产品推荐

