[Solved] How to get domain user name. User whoami

Forum rules
Before you post please read how to get help
Post Reply
ruyzz
Level 1
Level 1
Posts: 19
Joined: Sat Feb 24, 2018 11:01 pm

[Solved] How to get domain user name. User whoami

Post by ruyzz » Mon Apr 30, 2018 3:04 pm

With "Whoami" I get the user's name through the terminal Ctrl + Alt + T, but with bash it has root.

#! / bin / bash

usr = $ (whoami)


But through bash it gives me root and not the current username.

Please help me!
Last edited by ruyzz on Tue May 01, 2018 4:22 pm, edited 1 time in total.

User avatar
xenopeek
Level 24
Level 24
Posts: 23220
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: How to get domain user name. User whoami

Post by xenopeek » Mon Apr 30, 2018 4:01 pm

That means you're running your script with sudo. Is that needed?

In that case you can use the environment variable $SUDO_USER, it will have the name of the user that invoke sudo yourscript.sh. This variable is set by sudo.

Reliable way in bash is to test if $EUID equals 0 (zero). If so, your script is running as root. In that case use the $SUDO_USER variable for the username. Otherwise (user is running your script as themselves) you can use $USER variable.
Image

ruyzz
Level 1
Level 1
Posts: 19
Joined: Sat Feb 24, 2018 11:01 pm

Re: How to get domain user name. User whoami

Post by ruyzz » Mon Apr 30, 2018 6:26 pm

Thank you.

I have another problem. I want to access the desktop taking into account the current user, but it returns me:

domain \ user, when it should be: domain / user

The problem is the backslash.

Kyowash
Level 3
Level 3
Posts: 106
Joined: Mon Sep 25, 2017 1:22 pm
Location: /dev/full

Re: How to get domain user name. User whoami

Post by Kyowash » Tue May 01, 2018 10:40 am

ruyzz wrote:
Mon Apr 30, 2018 6:26 pm
Thank you.

I have another problem. I want to access the desktop taking into account the current user, but it returns me:

domain \ user, when it should be: domain / user

The problem is the backslash.
You can use sed:

Code: Select all

$ echo "user \ domain" | sed 's/\\/\//g'
user / domain

lmuserx4849

Re: How to get domain user name. User whoami

Post by lmuserx4849 » Tue May 01, 2018 3:29 pm

Are environmental variables an option: $USER, $HOSTNAME

Type set|less to view.

Bash documentation: HOSTNAME
Login documentation: USER

ruyzz
Level 1
Level 1
Posts: 19
Joined: Sat Feb 24, 2018 11:01 pm

Re: [Solved] How to get domain user name. User whoami

Post by ruyzz » Tue May 01, 2018 4:25 pm

Thank you, xenopeek, Kyowash and lmuserx4849!

Post Reply

Return to “Scripts & Bash”