[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: |
Sun, 18 Dec 2022 01:07:11 +0100 |
On 16/12/2022 23:18, Chet Ramey wrote:
Your arguments undergo word expansion, including double-quote processing,
before compgen sees them.
Thank you very much for your thorough explanation. I also take this
opportunity to thank you for
https://lists.gnu.org/archive/html/bug-bash/2022-11/msg00035.html
and I thank Koichi too.
I had seen, using set -x, that backslashes were given special
treatment but, in my naive view, I didn't grasp that was a consequence
of a larger problem.
So I have a function that works now. Maybe someone will have a wiser
solution.
#!/usr/bin/env bash
[[ "$(find /tmp/ -maxdepth 1 -type d -name "Dir\\\\A")" ]] && exit 2
mkdir -p /tmp/Dir\\A/dir\\\\B/dir\\\\\\C/dir\\\\\\\\D/
touch /tmp/Dir\\A/dir\\\\B/dir\\\\\\C/dir\\\\\\\\D/file\\\\\\\\A
set -o emacs
f_comp() {
local COMP CPR="${1%\/*}" CPO="${1##*\/}"
#echo 1>&2; set -x
COMP="$(compgen -f "${CPR//\\/\\\\\\\\}${1:+/}${CPO//\\/\\\\}")"
set +x
CPR="${COMP%\/*}" CPO="${COMP##*\/}"
printf '%s' "${CPR//\\\\/\\}${1:+/}$CPO"
}
bind -X
for DF in "/tmp/Dir" "/tmp/Dir\\A" "/tmp/Dir\\A/" "/tmp/Dir\\A/dir\\" \
"/tmp/Dir\\A/dir\\\\B" "/tmp/Dir\\A/dir\\\\B/" \
"/tmp/Dir\\A/dir\\\\B/dir\\\\\\" \
"/tmp/Dir\\A/dir\\\\B/dir\\\\\\C/dir\\\\\\\\" \
"/tmp/Dir\\A/dir\\\\B/dir\\\\\\C/dir\\\\\\\\D/file\\\\\\\\"; do
printf '%s\n' "$(( ++COUNT )) $DF >>> $(f_comp "$DF")"
done
rm -rf /tmp/Dir\\A/
### Output:
1 /tmp/Dir >>> /tmp/Dir\A
2 /tmp/Dir\A >>> /tmp/Dir\A
3 /tmp/Dir\A/ >>> /tmp/Dir\A/dir\\B
4 /tmp/Dir\A/dir\ >>> /tmp/Dir\A/dir\\B
5 /tmp/Dir\A/dir\\B >>> /tmp/Dir\A/dir\\B
6 /tmp/Dir\A/dir\\B/ >>> /tmp/Dir\A/dir\\B/dir\\\C
7 /tmp/Dir\A/dir\\B/dir\\\ >>> /tmp/Dir\A/dir\\B/dir\\\C
8 /tmp/Dir\A/dir\\B/dir\\\C/dir\\\\ >>> /tmp/Dir\A/dir\\B/dir\\\C/dir\\\\D
9 /tmp/Dir\A/dir\\B/dir\\\C/dir\\\\D/file\\\\ >>>
/tmp/Dir\A/dir\\B/dir\\\C/dir\\\\D/file\\\\A