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/
#!/usr/bin/sh
#goto
current_directory=`pwd | cut -f 4 -d "/"`
new_directory=`pwd | sed s=$current_directory=$1=`
cd $new_directory
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.
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...
It may work if you source it, but that might now always be a solution.
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?
Quote:
What about using a function?
Put something like this in your .profile or .bash_profile:
goto () {
${PWD%/*/*/*}/${1}/${PWD#/*/*/*/}
}
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?
Oops, this was my silly error. The function should read:
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: https://www.unix.com/unix-for-advanced-and-expert-users/4542-what-does-means.html?s=
(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!
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)
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)
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)
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)
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)
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)
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)
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)