Bash: Some issue when using read <<<“$VARIABLE” on a read-only root partition. Any known workarounds

bashhere-documentreadreadonlytmp

Just by coincidence I had to use my ATA-ID-to-device-name script (found here: https://serverfault.com/questions/244944/linux-ata-errors-translating-to-a-device-name/426561#426561) on a read-only / partition. In case you're curious, it was an Ubuntu recovery console which will let you access your / partition, but will mount it read-only by default. I am glad about that, because otherwise I would probably never have found out that my script behaves strangely on a R/O system due to a specific line, this one:

IFS=: read HostMain HostMid HostSub <<< "$HostFull"

This does not work if there is no write permission. I wouldn't have assumed it would fail, though. But apparently the <<< operator does require to write some temporary file to somewhere.

But is there any way to circumvent the creation of a temporary file, or, is there any way to specify where the file is written to? In the Ubuntu recovery console, there is—oddly enough—write permission on the /run directory, so that would do, if I could somehow "tell" read to write the temp file to somewhere else than usual.

Best Answer

An array could make the string parsing without the need for a temporal file. Don't forget to turn off globbing.

set -f
IFS=: Hosts=($HostFull)
HostMain=${Hosts[0]}
HostMid=${Hosts[1]}
HostSub=${Hosts[2]}
set +f
Related Question