出力を列形式で表示する

出力を列形式で表示する

を使用して、ユーザー名、端末名、ログイン時間などを表示するシェル スクリプトを作成しましたcase。コードは次のとおりです。

echo "Press 1 for user name, 2 for terminal name, 3 for login date and 4 for time"
read ch
case $ch in
1)
echo `who | cut -c1-8 | cut -d" " -f1`
;;
2)
echo `who | cut -c9-16 | column`
;;
3)
echo `who | cut -c22-32 | sort`
;;
4)
echo `who | cut -c34-39`
;;
esac

このスクリプトを実行すると、出力は 1 行で表示されますが、これを列形式 (つまり、1 つの列に複数の行をリストしたもの) で表示する必要があります。、、cutおよびcolumnコマンドを試しましたsortが、まだ解決していません。出力は次のとおりです。

Press 1 for user name, 2 for terminal name, 3 for login date and 4 for time
1
bioinfo class class class class class class class class class class
[class@bio ~]$

答え1

awk代わりにを使用しますcut。例:

#!/bin/bash

echo "Press 1 for user name, 2 for terminal name, 3 for login date and 4 for time"
read ch
case $ch in
    1)  
    who | awk '{ print $1 }'
    ;;  
    2)  
    who | awk '{ print $2 }'
    ;;  
    3)  
    who | awk '{ print $3 " " $4 }'
    ;;  
    4)  
    who | awk '{ print $5 }'
    ;;  
    *)  
    echo "Wrong input"
esac

実行サンプル:

./whoList.sh 
Press 1 for user name, 2 for terminal name, 3 for login date and 4 for time
2
console
ttys000
ttys001

./whoList.sh 
Press 1 for user name, 2 for terminal name, 3 for login date and 4 for time
3
Oct 3
Oct 3
Oct 3

./whoList.sh 
Press 1 for user name, 2 for terminal name, 3 for login date and 4 for time
1
maulinglawns
maulinglawns
maulinglawns

./whoList.sh 
Press 1 for user name, 2 for terminal name, 3 for login date and 4 for time
4
09:01
09:44
11:01

./whoList.sh
Press 1 for user name, 2 for terminal name, 3 for login date and 4 for time
7
Wrong input

ご覧の通り、出力はすべて1つになっていますカラム1行ではありません。

編集: OS X 10.11.6でテスト済み

bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15)

関連情報