The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. October 14, 2019, 07:16:50 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Set new directory  (Read 6042 times)
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« on: April 25, 2007, 07:37:01 PM »

I change my working directory dependant on what project I'm working on, I came up with this script;

Code:
#!/bin/bash

WD=`/usr/local/src/currentproject/`

cd $WD
exec /bin/bash

Problem is this creates another shell. Does anyone know how to set the current working directory without opening another shell?

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
cdc
Expert
****
Offline Offline

Posts: 105


View Profile
« Reply #1 on: April 26, 2007, 03:25:03 AM »

I could be wrong but I think you're making this more difficult than need be.

What's creating your other shell is the exec command you're running. So don't run it. Smiley

What I'm saying is that just doing:

cd /my/new/dir

will change your current working directory and you can access files using relative path names after you execute that line.
Logged

Will code for food.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #2 on: April 26, 2007, 03:33:19 AM »

Nah dude, I want a script that changes directory to the target directory and then exits leaving me in that directory.

Basically, instead of typing;
cd /usr/local/src/C++/HTML/linkchecker/
or
cd /var/www/html/dev/working/htmlentities/selectmodule/

I can just type;
workdir

and be in the target directory ready to work.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Dbyt3r
Rookie
**
Offline Offline

Posts: 19


View Profile
« Reply #3 on: April 26, 2007, 05:29:53 AM »

Just take out the line d00d.. Its just the 'cd' that matters  Smiley
Logged

No links in signatures please
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #4 on: April 26, 2007, 05:53:03 AM »

No, you guys just aren't getting it.

Write a program that changes directory and leaves you in that directory when it exits, try it, sounds easy, but its not. What happens is that whenever you run a script it creates a new shell, you change directory in that shell (process) and then when the script ends that process dies and you are left in the directory where you called the script from. My OP script works, but it starts an additional shell so when I want to log out I have to log out of two shells. just wondered if anyone knew a way to do it without the additional shell.

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
cdc
Expert
****
Offline Offline

Posts: 105


View Profile
« Reply #5 on: April 26, 2007, 06:42:01 AM »

Ah...ok.

I don't know if this will work in linux or not, but at my old job we used Solaris and if we "sourced" the script than any changes to the environment would be reflected in our shell.

So if your script is

cd /your/new/dir

You should just be able to run

'source myscript.sh'

and you would be in /your/new/dir

Too lazy to try this on my linux box...but if it doesn't work it will hopefully lead you down the path to what will work in your environment.
Logged

Will code for food.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #6 on: April 26, 2007, 01:48:35 PM »

TD this one has buggered me for quite a while. I've tried to do exactly the same thing and never had success. You and I probably do a lot of similar things... I might want to be in my apache config dir for a minute and then back to a dev dir... and have tried to write little shell scripts to do it and gave up out of frustration. Non-*nix ers won't understand it as well because DOS/Windoz DOES do what they're saying - the batch file operates WITHIN the context of the current user whereas a shell script runs in its own space... in fact, that very exercise demonstrates the profound difference between windows and *n[i|u]x quite handily.

I'm glad you started this thread because I'd just love to hear from the real shell smarties about how to pull this off...

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #7 on: April 26, 2007, 03:05:14 PM »

Dude, cdc just gave us the answer. Source is a shell builtin in bash;

Quote
        .  filename [arguments]
       source filename [arguments]
              Read and execute commands from filename  in  the  current  shell
              environment  and return the exit status of the last command exe-
              cuted from filename.  If filename does not contain a slash, file
              names  in  PATH  are used to find the directory containing file-
              name.  The file searched for in PATH  need  not  be  executable.
              When  bash  is  not  in  posix  mode,  the  current directory is
              searched if no file is found in PATH.  If the sourcepath  option
              to  the  shopt  builtin  command  is turned off, the PATH is not
              searched.  If any arguments are supplied, they become the  posi-
              tional  parameters  when  filename  is  executed.  Otherwise the
              positional parameters are unchanged.  The return status  is  the
              status  of  the  last  command exited within the script (0 if no
              commands are executed), and false if filename is  not  found  or
              cannot be read.

Looks like it does exactly what I need. You rock cdc! I've worked on HP-UX, SCO, Solaris, BSD, and Linux extensively and never come across this. Always said I should read the bash man page from top to bottom  Wink

I'll give this a try and report back.

Thanks again cdc,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #8 on: April 26, 2007, 04:07:29 PM »

fucking *x rookies grumble snort.  Beginners luck Wink
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Fatty
Rookie
**
Offline Offline

Posts: 11


View Profile
« Reply #9 on: May 04, 2007, 01:23:33 PM »

I know sourcing a script solves the original question, but what ever happened to the good old alias?

$ alias workdir="cd /tmp"
$ workdir
$ pwd
/tmp

Also look at the CDPATH environment variable.. 

export CDPATH="/home/me/projects"

from anywhere, cd proj1

will get you into /home/me/projects/proj1 if it exists

Fatty
Logged

No links in signatures please
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #10 on: May 04, 2007, 01:47:28 PM »

Hey fatty - welcome to the board!

/p
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« Reply #11 on: May 04, 2007, 06:03:04 PM »

@fatty: Top stuff dude, hadn't thought of the alias and never looked at the CDPATH env variable.... LOL, computing is so vast... ROFLMAO

Cheers dude,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!