[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: sudo and shell builtins



Just an idea (untested!):

Put into your .bashrc:
----8<------------
export SUDO_PWD;
sucd() {
	if [ "$1" ]; then
		local SUDOTMP
		SUDOTMP=`sudo _sudotestcd "$1"`
		if [ $? -eq 0 ]; then
			SUDO_PWD="$SUDOTMP"
		fi
	else
		# without an argument, reset the sudo_pwd to pwd
		SUDO_PWD="$PWD"
	fi
}
cd() {
	builtin cd "$@"
	SUDO_PWD=PWD
}
alias sudo=_sudocd
---->8------------

Put into /usr/local/bin/_sudotestcd:
----8<------------
#!/bin/sh
cd "$SUDO_PWD" && cd "$1" && echo "$PWD"
---->8------------

Put into /usr/local/bin/_sudocd:
----8<------------
#!/bin/sh
cd "$SUDO_PWD" && exec "$@"
---->8------------

Add the above to the list of allowed sudo commands.

The idea is to use an environment var (SUDO_PWD) to keep track of the "sudo'ed" directory, and submit changes through a pipe. This assumes of course that sudo won't clean out SUDO_PWD. As I've said I haven't tested it so there might also be problems with whitespace or such. I'm also not experienced enough to be sure there's no risk in the above sudo helper shell scripts to get a root shell, please anyone tell if there is.

Additionally you could use a PROMPT_COMMAND that reflects the state of SUDO_PWD instead of PWD so you always see where you are in the virtual sudo working dir.

Christian.



Reply to: