I need to run a script that uses readarray -d / -t
to split filepaths into arrays, but the readarray
of the targeted system doesn't support the -d
option (bash version 4.2.46)
As I don't know the exact behaviour of readarray -d / -t
, it is difficult for me to write a workaround for it.
It doesn't seem possible to replace it with IFS=/ read -a
because filenames containing control characters will break it, as shown here:
IFS=/ read -a arr < <(echo /home/fravadona/$'\n'/underneath)
declare -p arr
# OUTPUT: declare -a arr='([0]="" [1]="home" [2]="fravadona")'
So, my first question is, what's the expected result of:
readarray -d / -t arr < <(echo /home/fravadona)
readarray -d / -t arr < <(echo /home/fravadona/)
readarray -d / -t arr < <(echo /home/fravadona/$'\n'/underneath)
And lastly, does the following code emulates readline -d / -t
correctly?
filepath=/home/fravadona/$'\n'/underneath
unset arr; declare -a arr
prefix="${filepath%%/*}"
suffix="${filepath#*/}"
until [ "X$prefix" == "X$suffix" ]
do
arr+=( "$prefix" )
prefix="${suffix%%/*}"
suffix="${suffix#*/}"
done
arr+=( "$prefix" )