RPi-Distro / raspberrypi-sys-mods

A collection of Raspberry Pi-sourced system configuration files and associated scripts
99 stars 36 forks source link

Support all characters in WiFi SSID and key from rpi-imager #73

Closed MichaIng closed 1 year ago

MichaIng commented 1 year ago

Fixes: https://github.com/RPi-Distro/raspberrypi-sys-mods/issues/72

In here documents, without the identifier (EOL) being single-quoted, variables and command substitutions are expanded, like in double-quoted strings. To prevent an unintended second expansion of the content of these variables when the document itself is loaded, the calls should be single-quoted. To allow using the single-quote character itself as content of these variables, it needs to be escaped so that ' becomes '\'', i.e. the single-quoted string is ended, then an escaped single-quote character is printed, finally the second part of the single-quoted string is opened again.

MichaIng commented 1 year ago

It seems to work here:

root@micha:~# test=abc\'123
root@micha:~# dash << EOF
echo '${test//\'/\'\\\'\'}'
echo '123'\''abc'
EOF
abc'123
123'abc

On Debian ash == dash: https://packages.debian.org/ash

But '"'"' indeed works fine as well, so I can replace that:

root@micha:~# dash << EOF
echo '${test//\'/\'"\'"\'}'
echo '123'"'"'abc'
EOF
abc'123
123'abc
maxnet commented 1 year ago

You are right, it does work. Removed my post.

MichaIng commented 1 year ago

Ah I missed that imager_custom itself is bourne shell, not bash. So the substitution on that end seems to not work. Let me check this first.

MichaIng commented 1 year ago

Bourne shell does not support ${var//pattern/replacement}, so the only way I see to do this here is sed. First idea:

# test=abc\'123
# dash << EOF
> echo '$test' | sed "s/'/'\\\\''/g"
> EOF
dash: 2: Syntax error: Unterminated quoted string

Makes sense, since after $test expansion, inside the here document it becomes 'abc'123'. So doing the replacement first:

# test=$(echo "$test" | sed "s/'/'\\\\''/g")
# echo $test
abc'\''123
# dash << EOF
> echo '$test'
> EOF
abc'123

I was worried as we do again double-quote expansion, but since it is not doubled, it works fine:

# test=abc\'123\$def\"456
# echo "$test"
abc'123$def"456
# test=$(echo "$test" | sed "s/'/'\\\\''/g")
# echo "$test"
abc'\''123$def"456
# dash << EOF
> echo '$test'
> EOF
abc'123$def"456
XECDesign commented 1 year ago

Many thanks! It seems to resolve the issue in my testing.

I'll leave it open for a little longer in case anybody else has any comments or improvements.