I have some code similar to this:
while read -r col1 col2 col3 col4 col5 col6 col7 col8 TRASH; do
echo -e "${col1}\n${col2}\n${col3}\n${col4}\n${col5}\n${col6}\n"
done< <(ll | tail -n+2 | head -2)
(I'm not actually using ls
/ ll
but I believe this redacted example displays the same issue I am having)
The problem is I need a conditional statement if ll | tail -n+2 | head -2
fails so I'm trying to create a mapfile instead and then read
through it in a script. The mapfile gets created properly but I don't know how to redirect it in order to be properly read.
code
if ! mapfile -t TEST_ARR < <(ll | tail -n+2 | head -2); then
exit 1
fi
while read -r col1 col2 col3 col4 col5 col6 col7 col8 TRASH; do
echo -e "${col1}\n${col2}\n${col3}\n${col4}\n${col5}\n${col6}\n"
done<<<"${TEST_ARR[@]}"
mapfile contents
declare -a TEST_ARR=(
[0]="drwxr-xr-x@ 38 wheel 1.2K Dec 7 07:10 ./"
[1]="drwxr-xr-x 33 wheel 1.0K Jan 18 07:05 ../"
)
output
$ while read -r col1 col2 col3 col4 col5 col6 col7 col8 TRASH; do
> echo -e "${col1}\n${col2}\n${col3}\n${col4}\n${col5}\n${col6}\n"
> done<<<"${TEST_ARR[@]}"
drwxr-xr-x@
38
wheel
1.2K
Dec
7
String redirect is clearly wrong in this case but I'm not sure how else I can redirect my array.
read
that way and had a method for setting all my variables withawk
but it would not have been efficient. This is great! – jesse_b Jan 20 '18 at 01:49