English 中文(简体)
Tell if a user has SUed in a shell script?
原标题:

I have a script which executes a git-pull when I log in. The problem is, if I su to a different user and preserve my environment with an su -lp, the script gets run again and usually gets messed up for various reasons because I m the wrong user. Is there a way to determine in a shell script whether or not I m currently SUing? I m looking for a way that doesn t involve hard coding my username into the script, which is my current solution. I use Bash and ZSH as shells.

最佳回答

You could use the output of the who command with the id command:

WHO=`who am i | sed -e  s/ .*// `
ID_WHO=`id -u $WHO`
ID=`id -u`
if [[ "$ID" = "$ID_WHO" ]]
then
    echo "Not su"
else
    echo "Is su"
fi
问题回答
if test "$(id -u)" = "0"; 
  : # commands executed for root
else
  : # commands executed for non root
fi

If you are changing user identities with an suid executable, your real and effective user id will be different. But if use use su (or sudo), they ll both be set to the new user. This means that commands that call getuid() or geteuid() won t be useful.

A better method is to check who owns the terminal the script is being run on. This obviously won t work if the process has detached from it s terminal, but unless the script is being run by a daemon, this is unlikely. Try stat -c %U $(tty). I believe who am i will do the same thing on most Unix-like OSes as well.

You can use "$UID" environment variable.

If its value is ZERO, then the user has SUDOed.. Bcos root as $UID==0

Well.... on linux, if I su to another user the process su is in the new user s process list.

sudo... doesn t leave such pleasant things for you.

I m using zsh... but I don t think anything in this is shell specific.

if:

%ps | grep " su$"

returns anything, then you re running in an su d shell.

Note: there is a space before su$ in that to exclude command simply ending in su. Doesn t guard against any custom program/script called su, though.





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签