how to export name of logged in user correctly?

I have the following script here.

#!/bin/bash
#some initial variables
DLURL=https://raw.github.com/horvan/drubuntu/master
DIRURL=/opt/.drubuntu
#Get files to run the installation
wget -O $DIRURL/install.sh $DLURL/scripts/install/install.sh > /dev/null 2>&1
wget -O $DIRURL/functions.sh $DLURL/scripts/install/functions.sh > /dev/null 2>&1
wget -O $DIRURL/filetemplates.sh $DLURL/scripts/install/filetemplates.sh > /dev/null 2>&1
export nameofuser=`echo $USER`
.install.sh
sudo bash "$DIRURL"/install.sh 2> errors.txt

after execution the value of $nameofuser is not something like username but root.

how to make sure to match the name of the logged in user?

Answer

Use

export nameofuser=`who am i | awk '{print $1}'`

 OR 

export nameofuser=`logname`

as $USER can change if you call the script using sudo, etc.

https://stackoverflow.com/questions/4598001/how-do-you-find-the-original-user-through-multiple-sudo-and-su-commands

Leave a Reply

Your email address will not be published. Required fields are marked *