選択オプションの表示方法を変更できますか?

選択オプションの表示方法を変更できますか?

bashではselectとcaseを使用しています。現在、9つのオプションがあり、素敵できれいな3x3オプショングリッドを作成できますが、外観は次のとおりです。

    1) show all elements  4) write to file      7) clear elements
    2) add elements       5) generate lines     8) choose file
    3) load file          6) clear file         9) exit

列の前の行に表示したいです。

1) show all elements  2) add elements    3) load file
4) write to file      5) generate lines  6) clear file  
7) clear elements     8) choose file     9) exit

これを行う方法はありますか?シェルオプションなど、スクリプトでの設定や設定解除が容易なことが望ましい。重要な場合、オプションは配列に格納され、配列インデックスによってケースブロック内で参照されます。

OPTIONS=("show all elements" "add elements" "load file" "write to file" "generate lines" "clear file" "clear elements" "choose file" "exit")

...

select opt in "${OPTIONS[@]}"
do
case $opt in
    "${OPTIONS[0]}")

...

    "${OPTIONS[8]}")
        echo "Bye bye!"
        exit 0
        break
        ;;

    *)
        echo "Please enter a valid option."
esac
done

ベストアンサー1

独自の「選択」を作成します。

#!/bin/bash

options=("show all elements" "add elements" "load file" "write to file" "generate lines" "clear file" "clear elements" "choose file" "exit")
width=25
cols=3

for ((i=0;i<${#options[@]};i++)); do 
  string="$(($i+1))) ${options[$i]}"
  printf "%s" "$string"
  printf "%$(($width-${#string}))s" " "
  [[ $(((i+1)%$cols)) -eq 0 ]] && echo
done

while true; do
  echo
  read -p '#? ' opt
  case $opt in
    1)
      echo "${options[$opt-1]}"
      ;;

    2)
      echo "${options[$opt-1]}"
      ;;

    9)
      echo "Bye bye!"
      break
      ;;
  esac
done

出力:

1)すべての要素を表示する2)要素を追加する3)ファイルをロードする             
4) ファイルへの書き込み 5) ライン生成 6) ファイルの消去            
7) 要素の消去8) ファイル選択9) 終了                  
#?

おすすめ記事