-   Linux - General (
-   -   Bash Scripting - child process affecting parent process (

mthaddon 05-02-2004 10:22 AM

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:

#echo "CUR: $cur"
cd $1
#echo "NEW: $new"
true=`echo $new | grep $HOME`
#echo "TRUE: $true"
if [ -z $true ]; then
cd $cur
echo $PWD
echo $PWD

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

paulsm4 05-02-2004 01:19 PM

You're right - there are better ways to do this.

And you're also right - if you want to do something (like "cd") that affects the parent shell who's calling the script, then "sourcing" the script (like with ".") is probably the way to go.

As to your particular error: I'd do a "set -x" in your script (or something equivalent - like strategically placed "echo" statements) to find exactly which script line the error is coming from. And then debug it backwards from there.

'Hope that helps .. PSM

All times are GMT -5. The time now is 01:51 PM.