[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: compgen stops processing backslashes after any call to bind
From: |
of1 |
Subject: |
Re: compgen stops processing backslashes after any call to bind |
Date: |
Mon, 19 Dec 2022 16:43:46 +0100 |
Well, it is said that directories sometimes harbor more than one item :)
Fabien (of1)
#!/usr/bin/env bash
[[ -e "/tmp/Dir\\A" ]] && exit 2
mkdir -p /tmp/Dir\\A/dir\\\\B/ /tmp/Dir\\A/dir\\\\\\C/
touch /tmp/Dir\\A/file\\\\\\\\A "/tmp/Dir\\A/file\\ \\\\B"
f_comp() {
local CP CPR="${1%\/*}" CPO="${1##*\/}"
declare -a COMP
readarray -t COMP \
< <(compgen -f "${CPR//\\/\\\\\\\\}${1:+/}${CPO//\\/\\\\}")
for CP in "${COMP[@]}"; do
CPR="${CP%\/*}"
printf '%s\n' "${CPR//\\\\/\\}${1:+/}${CP##*\/}"
done
}
set -o emacs
bind -X
for DF in "/tmp/Dir\\A" "/tmp/Dir\\A/" "/tmp/Dir\\A/dir\\" \
"/tmp/Dir\\A/file\\"; do
printf '%s\n' "$(( ++COUNT )) $DF >>>" "$(f_comp "$DF")" ""
done
rm -rf /tmp/Dir\\A/
### Output:
1 /tmp/Dir\A >>>
/tmp/Dir\A
2 /tmp/Dir\A/ >>>
/tmp/Dir\A/file\ \\B
/tmp/Dir\A/file\\\\A
/tmp/Dir\A/dir\\\C
/tmp/Dir\A/dir\\B
3 /tmp/Dir\A/dir\ >>>
/tmp/Dir\A/dir\\\C
/tmp/Dir\A/dir\\B
4 /tmp/Dir\A/file\ >>>
/tmp/Dir\A/file\ \\B
/tmp/Dir\A/file\\\\A