среда, 12 июня 2013 г.

How to su properly in a shell script & find the original user through multiple sudo commands

When you need to perform several commands from the other user inside a script this will do the job:
#!/bin/bash -
su - user <<HERE
ls -al
# other commands run by 'user'
# ...
HERE
When you need to find out the original owner of the current shell always use
who am i | awk '{print $1}'
or
logname
as no other methods are guaranteed (via stackoverflow).
Another useful tool is environment variable
$SHLVL: Incremented by one each time an instance of bash is started.
It won't show the user but can show the shell 'depth'.

Комментариев нет:

Отправить комментарий