Reputation: 1109
The following script lists items in a directory. It produces an output of 3 numbered columns. The numbered output is listed horizontally from left to right across the columns. I would instead like the output to be listed vertically down the first column, then the second, and then the third. How do I accomplish this?
The script
#!/bin/bash
menu=( $(ls ${HOME}) )
i=0
for m in ${menu[@]}
do
echo "$(( i++ ))) ${m}"
done | xargs -L3 | column -t
The output
0) item 1) item 2) item
3) item 4) item 5) item
6) item 7) item 8) item
9) item 10) item 11) item
12) item 13) item 14) item
The desired output
0) item 5) item 10) item
1) item 6) item 11) item
2) item 7) item 12) item
3) item 8) item 13) item
4) item 9) item 14) item
Upvotes: 0
Views: 202
Reputation: 3666
Without rewriting your code too much, this will work:
#!/bin/bash
menu=( $(ls ${HOME}) )
totalRows=$(( ${#menu[*]} / 3 + 1 ))
i=0
for m in ${menu[@]}
do
echo "$(( i/3 + (i%3)*totalRows ))) ${m}"
let i++
done | xargs -L3 | column -t
Upvotes: 1
Reputation: 4112
you can also try something like this;
#!/bin/bash
menu=( ${HOME}/* )
menLen=${#menu[@]}
rowCounts=$(echo $(( $menLen / 3 )))
for (( c=0; c<$rowCounts; c++ ))
do
findex=$c;
sindex=$(echo $(( $findex + $rowCounts )))
tindex=$(echo $(( $sindex + $rowCounts )))
printf "%-40s \t %-40s \t %-40s \n" "$findex ) ${menu[$findex]##*/}" "$sindex ) ${menu[$sindex]##*/}" "$tindex ) ${menu[$tindex]##*/}"
done
Upvotes: 1