Yrgl
Yrgl

Reputation: 653

How to recognize user name who run script

In order to use chown command I need to know who's running my script. Real name because it's going to be run with sudo. How can i do it?

Upvotes: 0

Views: 2857

Answers (4)

Bhargav
Bhargav

Reputation: 21

#!/bin/bash
# Init
FILE="/tmp/out.$$"
GREP="/bin/grep"
#....
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# ...

Upvotes: 2

user332325
user332325

Reputation:

id -run gives you real UID, not effective one.

Upvotes: 0

rndstr
rndstr

Reputation: 777

For completeness, there is also id -un and whoami but env vars should probably be preferred unless you have a reason not to.

Upvotes: 3

lhf
lhf

Reputation: 72312

Try the LOGNAMEor USER env variables.

Upvotes: 4

Related Questions