• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I show username only in script?  RSS feed

 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the title said I wonder how do i do that?  We know that using the command last will show the username, tty or pts, ip(I think), time and date, total time, and whether the user is log in or log off but I only wants username once in script? Any Ideas how I do that ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm not familiar with last, but if the data is consistently formatted, you could pipe it to cut, or possibly sed/awk, or run it from a perl script and use it's parsing ability to pick out what you want...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and if you only want to show the username once, then you could pipe that to uniq
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:and if you only want to show the username once, then you could pipe that to uniq


I see, that mean I need to use echo PS to run the script that output only the username (Removing or cutting the others). for instance when I use last command in the linux terminal will show this:


root pts/1 55.98.0.130 Fri Nov 14 18:52 - 22:16 (03:23)
root pts/0 c-57-161-249-108 Fri Nov 14 05:45 - 03:09 (1+21:23)
mrhope pts/0 c-57-161-249-109 Wed Nov 12 06:51 - 03:14 (1+20:22)
root   pts/1 c-57-161-249-108 Tue Nov 11 19:53 - 05:03 (09:09)


then after I created the script using what you said about pipe will only show this :

Root
mrhope

let see what getting using pipe
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should never get "Root" out, since your example shows "root"...

but I think you can do something as simple as this:

last | awk ' {print $1}' | sort -u

now...if you want to preserver the order...that's a little trickier, but the above command gave me this output on my server:

bjcapp
bjcroot
db295
hci
reboot
root
shutdown
wtmp
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:you should never get "Root" out, since your example shows "root"...

but I think you can do something as simple as this:

last | awk ' {print $1}' | sort -u

now...if you want to preserver the order...that's a little trickier, but the above command gave me this output on my server:

bjcapp
bjcroot
db295
hci
reboot
root
shutdown
wtmp

Ok the above command work in linux terminal but writing the script would look like this :
#!/bin/bash

last | awt| '{print$1}' | sort -u

or do i need to add only "  | awt| '{print$1}' | sort -u  " in the script as a if statement?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would there an if statement? What would be your condition?

Where did "awt" come from, and the additional pipe?

I am not an linux expert, so I'm not sure if that means something or is just a typo.
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Why would there an if statement? What would be your condition?

Where did "awt" come from, and the additional pipe?

I am not an linux expert, so I'm not sure if that means something or is just a typo.

Yes, is a mistype support to be awk mixing some java here. Anyhow I find the command using the awk won't work due the some array problem rather I will need use a similar ideas like the awk but in C++ or C instead.
sorry for this late reply 
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be more specific than "some array problem"? 

Believe me, folk around here are dying to help you. But you have to make it easy for them to do so.
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to be more specific but I do know that he doesn't want it to be simple rather complex (got do in either Script or Java, or C++/C). Anyhow this project going to take me a while to do.

He always quote  the " therac-25" for this project.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who is "he"?

You are saying it is SUPPOSED to be complex?

you say "got to do it eithe Script or Java".  calling AWK from a shell script or Java doesn't count?

and I have NO CLUE what " therac -25" means.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you somehow referring to Therac-25?  Not sure how that applies here...possibly he's trying to warn you of how dangerous software can be...which is why I WOULD use something like AWK that has been tested and vetted for years over trying to write my own parser...
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Are you somehow referring to Therac-25?  Not sure how that applies here...possibly he's trying to warn you of how dangerous software can be...which is why I WOULD use something like AWK that has been tested and vetted for years over trying to write my own parser...


I see then that the only thing i can use to only show the username in script.  I don't know what kind of flaw awk have that his think it is wrong
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jin Otanashi wrote:
I see then that the only thing i can use to only show the username in script.  I don't know what kind of flaw awk have that his think it is wrong


Woah!! Wait a second!!  Just what are you asking here?!?!?  Are you asking for alternate ways to get the username? Or are you asking for a way to get awk functionality without using awk?

The first question is very easy -- such as ... How about using the $USER environment variable? Or how about using the "whoami" command?

The second question is ridiculous hard, to the point of silly. It is like asking how to I make the C++ compiler run Java programs?

Henry
 
jin Otanashi
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
jin Otanashi wrote:
I see then that the only thing i can use to only show the username in script.  I don't know what kind of flaw awk have that his think it is wrong


Woah!! Wait a second!!  Just what are you asking here?!?!?  Are you asking for alternate ways to get the username? Or are you asking for a way to get awk functionality without using awk?

The first question is very easy -- such as ... How about using the $USER environment variable? Or how about using the "whoami" command?

The second question is ridiculous hard, to the point of silly. It is like asking how to I make the C++ compiler run Java programs?

Henry

ok sorry for slow or later reply a lot thing happen over the past week. Anyhow moving back to your question, yes i'm trying to find a way to get awk functionality without using awk (due to unknown reason why he doesn't wants it).
the whoami is a linux commands not script for printing out the terminal, just trying to create a code that just import/show Username only, ignoring/erasing other stuff using the command last in linux.  

last question, yes is silly but it may work if i just created a file name dot type (C++/C or java) using the other tools in the machine linux like nano or vim. finally I can compile or run the program using ./ in root directory (after touch filename that i will run)
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!