Bash and remote directories
May. 15th, 2006 08:44 pmMost of the files I copy around tend to be system specific, so it usually makes more sense to copy them to a local directory like
/tmp/$LOGNAME rather than a global directory like $HOME. So far, so good. But what happens if /tmp/$LOGNAME doesn't exist and I attempt to scp to it? A big ugly mess with a potentially sensitive file sitting around on /tmp for anyone to find, not to mention problems with whatever I've got set up in my profile to automatically create my jtmpdir.
My solution to problem? A bit of bash magic. It turns out that bash sources the contents of .bashrc for both interactive non-login shells and non-interactive remote shells, e.g. ssh and scp. This make it possible to customise the remote environment thus:
stty > /dev/null 2>&1
if [ $? != 0 ]
then
# Remote shell customisation
export ENVIRONMENT=REMOTE
export TMPDIR=/tmp/$LOGNAME
[ ! -d $TMPDIR ] && mkdir -m 0700 $TMPDIR
else
# Interactive shell customisation
[ -f ~/.bash_profile ] && source ~/.bash_profile
fi
The nice thing about this is that the mkdir is pretty much guaranteed to run before the start of the copy part of an scp session making the whole remote directory existence thing a don't care situation.
no subject
Date: 2006-05-16 01:47 pm (UTC)no subject
Date: 2006-05-16 08:16 pm (UTC)