0

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
I0_ol
  • 1,054
  • 1
  • 14
  • 28
  • Please see if this helps, looks you need padding. http://stackoverflow.com/questions/4409399/padding-characters-in-printf – Rao Sep 28 '16 at 06:57

2 Answers2

1

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 
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24
1

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
ccarton
  • 3,556
  • 16
  • 17