Bash Scripting - child process affecting parent process
So, I have a question about bash scripting. What I'm trying to do is write a simple script that I can use as an alias for "cd" which checks the directory that you're cd-ing to, and if it's not under your home directory, doesn't let you cd there.
I know there are better ways to do this (chroot, for instance, or rbash, although if I knew a way to have rbash but allow you to cd but just not outside your home tree that would be cool), but just bear with me.
Here's the script:
cur=$PWD
#echo "CUR: $cur"
cd $1
new=`pwd`
#echo "NEW: $new"
true=`echo $new | grep $HOME`
#echo "TRUE: $true"
if [ -z $true ]; then
echo "RESTRICTED"
cd $cur
echo $PWD
else
echo $PWD
fi
Seems to work fine when I call it as a script: for example if I run it (let's call it scd) as:
scd /
it outputs "RESTRICTED"
but it will accept
scd $HOME/dir
problem is that if you call it as a script like that, the child process doesn't reference/affect the parent process, so it won't seem as if you really are changing directory. And if I try using it as an alias with ". " so that the parent is affected by the child, it chokes on me:
-bash: /home/mthaddon: is a directory
Any help appreciated.
Thanks, Tom
|