Code: Select all
sudo myscript.sh
Code: Select all
sudo myscript.sh
Code: Select all
sudo USER=mark ./myscript.sh
Code: Select all
#!/bin/sh
USER=mark
echo $USER
Hahahahahaha... I agree! Wouldn't 'sudo'ing the individual script lines run into the same problem?mockturtl wrote:... That said, it smells a little.
foo.sh:markfilipak wrote:Hahahahahaha... I agree! Wouldn't 'sudo'ing the individual script lines run into the same problem?mockturtl wrote:... That said, it smells a little.
Code: Select all
#!/bin/sh
echo $USER
sudo ./bar.sh
echo $USER
Code: Select all
#!/bin/sh
echo $USER
Indeed. I'm having problems with my Xfce desktop. So, quite unrelated to my OP, I conducted the following experiment:mockturtl wrote:(More generally, sudo doesn't preserve the environment by default.
Code: Select all
mark@Iris ~ $ su
Password:
mark@Iris ~ $ sudo env Iris mark # env
[sudo] password for mark:
COLORTERM=Terminal COLORTERM=Terminal
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbu...
DEFAULTS_PATH=/usr/share/gconf/default.default....
DESKTOP_SESSION=default
DESKTOP_STARTUP_ID=
DISPLAY=:0.0 DISPLAY=:0.0
HOME=/home/mark HOME=/root
LANG=en_US.UTF-8 LANG=en_US.UTF-8
LOGNAME=root LOGNAME=root
MAIL=/var/mail/root MAIL=/var/mail/root
MANDATORY_PATH=/usr/share/gconf/default.mandato...
MDMSESSION=default
MDM_LANG=en_US.UTF-8
MDM_XSERVER_LOCATION=local
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/... PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/...
PWD=/home/mark
SESSION_MANAGER=local/Iris:@/tmp/.ICE-unix/1251...
SHELL=/bin/bash SHELL=/bin/bash
SHLVL=2
SSH_AGENT_PID=1467
SSH_AUTH_SOCK=/tmp/ssh-AjPJ14f3GtEC/agent.1251
SUDO_COMMAND=/usr/bin/env
SUDO_GID=1000
SUDO_UID=1000
SUDO_USER=mark
TERM=xterm TERM=xterm
USER=root USER=root
USERNAME=root USERNAME=mark
WINDOWID=62914564
WINDOWPATH=8
XAUTHORITY=/home/mark/.Xauthority XAUTHORITY=/home/mark/.Xauthority
XDG_CONFIG_DIRS=/etc/xdg/xdg-default:/etc/xdg
XDG_DATA_DIRS=/usr/share/default:/usr/local/sha...
XDG_RUNTIME_DIR=/run/user/root
XDG_SESSION_COOKIE=461c99289f676f26ea71a0485157...
_=/usr/bin/env
mark@Iris ~ $ Iris mark #
Code: Select all
sudo xfdesktop
Code: Select all
su
xfdesktop
Well, I don't follow that - remember: I'm a newbie.mockturtl wrote:Try adding `export myvar=someval` in foo.sh, and `echo $myvar` in bar.sh. Without sudo, the value is inherited; with it, not.)
Don't do graphical things with sudo -- that's for CLI. The GUI analogue [for a gnome-based desktop] is gksudo. (resp., su; gksu)I would conclude that one should not install system thingies using 'sudo'.
That's pretty deep in dragon country, I think.I got an error that 'SESSION_MANAGER' was undefined.
Code: Select all
As can easily be seen, 'sudo' (left) and 'su' (right) are not at all equivalent.
Declaring a variable syntax: name=value (or with double-quotes, "value", or single-quotes, 'value', with or without interpolation).Well, I don't follow that - remember: I'm a newbie.
Code: Select all
#!/bin/sh
export hello="World!"
./bar.sh # without sudo, outputs 'mark World!'
sudo ./bar.sh # with sudo, outputs 'root'; $hello has no value
sudo -E ./bar.sh # with sudo, preserving environment, outputs 'root World!'
Code: Select all
#!/bin/sh
echo $USER $hello
Let's see the script.markfilipak wrote:I'm sorry, mockturtl, but I don't understand what you're writing.
Code: Select all
#!/bin/bash # Usage: sudo ./setup.sh <vboxsf-name>
cd ~ #
mkdir "./$1" # Create shared folder (VBox-share, not net-share)
mount -t vboxsf "$1" "./$1" # For this session, mount shared folder.
apt-get purge virtualbox* # Remove guest additions already installed.
cd /media/$USER #
for d in `ls ./` # Find & run Guest Additions (I.E., VBoxLinuxAdditions.run).
do
if test -x "./$d/VBoxLinuxAdditions.run"
then
if -z `grep vboxsf /etc/fstab`; then echo -e "$1 /home/$USER/$1 vboxsf defaults 0 0" | tee -a /etc/fstab; fi
sh "./$d/VBoxLinuxAdditions.run"
exit
fi
done
echo "Could not find Guest Additions."
Code: Select all
#!/bin/bash # Usage: sudo ./setup.sh <vboxsf-name>
cd ~ #
mkdir "./$1" # Create shared folder (VBox-share, not net-share)
sudo mount -t vboxsf "$1" "./$1" # For this session, mount shared folder.
sudo apt-get purge virtualbox* # Remove guest additions already installed.
cd /media/$USER #
for d in `ls ./` # Find & run Guest Additions (I.E., VBoxLinuxAdditions.run).
do
if test -x "./$d/VBoxLinuxAdditions.run"
then
if -z `grep vboxsf /etc/fstab`; then echo -e "$1 /home/$USER/$1 vboxsf defaults 0 0" | sudo tee -a /etc/fstab; fi
sudo sh "./$d/VBoxLinuxAdditions.run"
exit
fi
done
echo "Could not find Guest Additions."