changing directories (i'm sure there is a simple solution for this)

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers changing directories (i'm sure there is a simple solution for this)
# 1  
Old 02-28-2002
changing directories (i'm sure there is a simple solution for this)

I just want to exit my script in a new directory from a bash shell. Problem is that the script internally changes to the directory I want to move to, however when exits is still in the original directory. Does that make sense?

ie usage: goto null

changing from /usr/bin/xtra/test/test3/

will move to /usr/bin/null/test/test3/

current_directory=`pwd | cut -f 4 -d "/"`
new_directory=`pwd | sed s=$current_directory=$1=`
cd $new_directory

Help is appreciated. Thanks!
# 2  
Old 02-28-2002
I think it is impossible.
Because when you run your script, you open a new subshell proccess.
To do this in your current subshell proccess, you can put your script in the .profile for example.

Tries to execute it thus:

. ./your_script

# 3  
Old 02-28-2002
What about using a function?

Put something like this in your .profile or .bash_profile:

goto () {

Then you could be in /usr/bin/xtra/test/test3/, type "goto null", and you'd then be in /usr/bin/null/test/test3/.
# 4  
Old 02-28-2002
Hammer & Screwdriver adding at script to change directory

u can add this extra line on your script to change directory at the very last syntax should be

cd / = to return to root directory

cd /?whatever directory u want to go back

# 5  
Old 02-28-2002
If it's in a script, simply putting "cd /wherever" at the end will not do anything to change where you're at. A script executes as a child process, which does nothing to affect your environment. The script may change directory, but once it exits, you'll still be where you started.
Try it...
echo "I am in $PWD right now"
cd /usr/bin
echo I am in $PWD now..."

I am in /home/livinfree right now
I am in /usr/bin now...

$ pwd

It may work if you source it, but that might now always be a solution.

$ . ./
# 6  
Old 02-28-2002
and I thought you could do anything in unix (well at least to the variable of time)

cheers for the reply peoples, very much appreciated for the thoughtful responses.

however I just have a supplementry question for LivinFree: In regards to the function, can you please explain what is happening or point me to a place I can find it? I've never come across functions with the same syntax before in a bourne shell and would like to find out more.

And can it handle any number of directories?

What about using a function?

Put something like this in your .profile or .bash_profile:

goto () {

Then you could be in /usr/bin/xtra/test/test3/, type "goto null", and you'd then be in /usr/bin/null/test/test3/. mentry question for livingfree

Ok, now that i have tested this command I have a better idea what is happening, however it really isn't what I want.

Only because it doesn't work from any directory level and when it is in the right directory structure I only get something like this

(shakey on prodsrv)/usr/bin/code/training/shakey>goto lib
bash: /usr/bin/lib/training/shakey: is a directory

Although I like the idea of it being in the .bash_profile rather than a script.

Any alternate ideas or can we work from here somewhere?

# 7  
Old 03-01-2002
Oops, this was my silly error. The function should read:
goto () { 
cd ${PWD%/*/*/*}/${1}/${PWD#/*/*/*/} 

I forgot the cd in the first example, so it was trying to execute /usr/bin/lib/training/shakey. That's why you were getting the error telling you it was a directory.

Basically, instead of you executing the commands "pwd" and "cut" to find out where you were, and then to cut and paste the path, I tried to use builtin variables and manipulation.

In bash (and some other modern shells), the $PWD variable is automatically set when you change directory. It holds the current working directory. The other junk ( ##/*/ and similar...) is built in variable manipulation.
See this thread for an explanation:

(By the way, the curly braces are needed around the variables with modifiers in them, and are optional for the "normal" ones, like in the above example, the ${1}. It simply protects them, and keeps everything seperate.)

Try variations on some of the examples... it's pretty neat to see what you can do.

And as for it handling a different number of directories, you would have to rewrite it, since this is hard-coded to chop the first two directories, change the third, and chop the thrid and fourth.

If you wanted a more "dynamic", or full featured function, it might be better to write an entire script to handle different situations...

Hope this helps, and please post back with any more questions!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Urgent solution for simple sed

Hi Im running this command on AIX in ksh. My input file samp1 contains 1 2 123 12345 When I execute the following sed i dont get a matching pattern sed -n '/{1}/p' samp1 Can anyone help me with this simple thing (3 Replies)
Discussion started by: raghav288
3 Replies

2. Shell Programming and Scripting

What are the differences between 'bash' and 'sh'

Hopefully this doesn't come off as too much of a "newbie" question or a flamebait. But I have recently begun working with a Sun Solaris box after having spent the past five years working with RedHat. From what i can tell, thing look fairly similar and the 'man' command is some help. But I've... (7 Replies)
Discussion started by: deckard
7 Replies

3. UNIX for Advanced & Expert Users

How to remove a file with a leading dash '-' in it's name?

Somehow someone created a file named '-ov' in the root directory. Given the name, the how was probably the result of some cpio command they bozo'ed. I've tried a number of different ways to get rid of it using * and ? wildcards, '\' escape patterns etc.. They all fail with " illegal option --... (3 Replies)
Discussion started by: GSalisbury
3 Replies

4. UNIX for Dummies Questions & Answers

here's how i did a very simple install of invidia drivers

ok i got the nvida drivers for mandrake9 rpm there is 3 rpms you must have glx ,kernel and something else email me if you want the precompiled drivers :: email removed :: once i got the drivers in stalled then i reseted the computer then i put the 1cd of mandrake9 in and did a upgrade i... (2 Replies)
Discussion started by: amicrawler2000
2 Replies

5. Shell Programming and Scripting

Alias's & Functions - simple question

Having a memory lapse: If I redefine a command using an alias or a function - How do I access the "real" command without specifying an absoulte path: i.e. function man { /usr/bin/man |more } alias ls='/usr/bin/ls -l' How do I specify the ls or man command above without the... (6 Replies)
Discussion started by: kornshellmaven
6 Replies

6. UNIX for Dummies Questions & Answers

Changing IP's

How do you change an IP address of an interface. (2 Replies)
Discussion started by: Hordak
2 Replies

7. Shell Programming and Scripting

Clearify what it means under 'WHAT' when hit the 'w'-command

I wonder how I shall read the result below, especially 'what' shown below. The result was shown when I entered 'w'. E.g what is TOP? What is gosh ( what does selmgr mean?)? login@ idle JCPU PCPU what 6:15am 7:04 39 39 TOP 6:34am 6:45 45 45 TOP 6:41am ... (1 Reply)
Discussion started by: Aelgen
1 Replies

8. Programming

may be simple but i don't know -- Print current date from C program

How to print current date of the Unix system accessing thru C++ program ? I wrote like this #include <time.h> ....... time_t tt; struct tm *tod; .... time(&tt); tod = localtime(&tt); cout << tod->tm_mon + 1 << "/" << tod->tm_mday << "/" ... (6 Replies)
Discussion started by: ls1429
6 Replies

9. UNIX for Dummies Questions & Answers

list file's by size order in sepecfied directory and sub directories

How do I list files of type "*.file" for example by size order recursively ? (2 Replies)
Discussion started by: ferretman
2 Replies

10. Programming

a very simple question (but i don't know)

how to write a program that receive a string from keyboard and then print it out. i write a program: main() { char str; gets(str); printf(str); } but when i compile it, the system said something like "new line is not last charicter" and sometime the system said "a3.c is up to date"... (1 Reply)
Discussion started by: dell9
1 Replies
Login or Register to Ask a Question