Hi there,
I have a bash script that I grabbed from work which checks that the user running the script is a specific user (we'll call the user foo). If the user is not foo then the current user name is exported to an environment variable and the script re-executes through sudo as foo (sudo -u foo $0 $*).
So far so good, however when the script gets re-run the environment variable no longer exists. It runs fine at work (REL, but I don't know which version), but not locally (Fedora 8).
My suspicion is that sudo as a different user is causing me to lose the environment variable, but that doesn't sound right either. Any hints?
Code:
#!/bin/bash
echo "0--owner=$OWNER"
echo "0--user=$USER"
if [ "$1" = "" ]; then
echo "Usage: foo <bar>"
exit;
fi
base=/foo/bar
if [ ! -d "$base" ]; then
echo "Base directory '$base' does not exist"
exit 1
fi
if [ "$USER" != "foo" ]; then
export OWNER=$USER
echo "1--owner=$OWNER"
sudo -u foo $0 $*
echo "DONE"
exit;
fi
echo "2--owner = $OWNER"
if [ "$OWNER" = "" ]; then
OWNER=$USER
fi
echo "3--owner = $OWNER"
...
<meat of the script>
Results of execution:
0--owner=
0--user=john
1--owner=john
0--owner=
0--user=foo
2--owner =
3--owner = foo