Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 05-02-2004, 10:22 AM   #1
LQ Newbie
Registered: Feb 2003
Posts: 7

Rep: Reputation: 0
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
Old 05-02-2004, 01:19 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
When I kill parent process, child isn't killed BengeBoy Linux - Software 0 08-19-2004 04:38 PM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM
child and parent process error jdevanand Programming 1 04-29-2002 08:13 AM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:27 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration