
Ich habe ein Shell-Skript geschrieben, das Benutzernamen, Terminalnamen, Anmeldezeit usw. anzeigt case
. Der Code lautet:
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
Wenn ich dieses Skript ausführe, wird die Ausgabe in einer einzigen Zeile ausgegeben und ich möchte, dass sie in einem Spaltenformat angezeigt wird (d. h. über mehrere Zeilen in einer einzigen Spalte aufgelistet). Ich habe die Befehle cut
, column
und sort
ausprobiert, aber es hat sich nichts geändert. Die Ausgabe lautet:
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 ~]$
Antwort1
Ich würde hierfür beispielsweise awk
anstelle von verwenden: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
Ausführungsbeispiele:
./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
Die Ausgabe ist, wie Sie sehen können, alles in einemSpalte, nicht in einer einzigen Zeile.
Edit: Getestet unter OS X 10.11.6
bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15)