-2

I have written a shell script that displays username, terminal name, login time etc using case. The code is:

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

When I run this script the output comes in a single line and I want it to be displayed in a columnar format (i.e. listed across multiple lines in a single column). I have tried the cut, column and sort commands, and yet no respite. The output is:

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 ~]$

closed as unclear what you're asking by Thomas Dickey, Stephen Kitt, Anthony Geoghegan, GAD3R, HalosGhost Oct 3 '16 at 13:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    see if this helps: unix.stackexchange.com/questions/308631/… – Sundeep Oct 3 '16 at 7:37
  • 2
    Please edit your question and clarify. What is class and why is it repeated so many times? How did you run the script? Which option did you choose to get this output? Why are you using echo for commands that print their output to stdout anyway? – terdon Oct 3 '16 at 8:33
  • It is pretty clear that class is the username, result obtained after using the who command. – user3382203 Oct 4 '16 at 5:54
3

I would use awk instead of cut for this, e.g.:

#!/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

Execution samples:

./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

The output, as you can see, is all in one column, not on a single line.

Edit: Tested under OS X 10.11.6

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.