LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-17-2004, 11:22 AM   #1
trout21
LQ Newbie
 
Registered: Jun 2003
Posts: 22

Rep: Reputation: 15
script to change directory


Hiya,

I would like to be able to write a simple script that will change to another directory.

However, when I run my script it does not go anywhere - why? and how can I fix it?


#!/bin/sh
###########################
#
# Directory changing script
#
############################
#
dir=where I want to go
echo "Moving to folder. . ."
echo ""
cd $dir
exit 0




Thank for any reply.
trout
 
Old 02-17-2004, 11:45 AM   #2
linuxdev
Member
 
Registered: Feb 2004
Posts: 34

Rep: Reputation: 15
I am not sure what you really mean but if you can change your directory
through your script and wanted to come to that directory by the end of the
script then .......you have to do it using command cd dir


your diectory will remain the same as of the directory where you are running the
script.........
 
Old 02-17-2004, 11:51 AM   #3
lone_nut
Member
 
Registered: Dec 2003
Location: Denmark
Distribution: Mandrake
Posts: 179

Rep: Reputation: 30
When you execute any command, the shell creates a subshell. Now the subshell replace itselfs with the program to get executed. In this case the program changes its directory. Then it quits. And the only thing left, is the original shell. That shell did not change its directory, so it still stands it the original directory. To avoid creating a subshell, execute the above script as . ./foo
 
Old 02-17-2004, 12:23 PM   #4
trout21
LQ Newbie
 
Registered: Jun 2003
Posts: 22

Original Poster
Rep: Reputation: 15
Thank you both for your reply.

I see what you mean about the subshell - and have tried as you suggest ../ when executing the script. However, bash returns no such file or directory when I do this!

Maybe I have the wrong end of the stick?!

trout.
 
Old 02-17-2004, 12:56 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
What is the target directory? And what is the actual value of $dir in your script, rather than 'where I want to go'? And why have a directory changing script?

Quote:
Originally posted by trout21
and have tried as you suggest ../
What exactly did you type? Pasting commands and outputs is a lot clearer than paraphrases. Was the directory not found or the script itself?

Last edited by slakmagik; 02-17-2004 at 12:59 PM.
 
Old 02-17-2004, 01:06 PM   #6
trout21
LQ Newbie
 
Registered: Jun 2003
Posts: 22

Original Poster
Rep: Reputation: 15
The target directory is to be:

~/public_html/blah blah a long list. . .

same as if i had in my script:

cd ~/public_html/blah blah a long list. . .

but I went and put it in a variable.


the exact error was:

bash: ../plug: No such file or directory

so I guess it's not finding the file itself when you type ../plug as the script executes when I type ./plug and opens the subshell and closes it again leaving me where I started!

trout
 
Old 02-17-2004, 01:12 PM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Well, this would be easier from an alias. Put 'alias plug="path/to/change/to"' in your .bashrc or wherever if that's the sole purpose. But as far as the script, remove the 'exit' line. And you only need to source it as '. plug' if it's on your path and as '. /full/path/to/plug' if not.
 
Old 02-17-2004, 01:35 PM   #8
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 234Reputation: 234Reputation: 234
Why not just use the cd command? Even if your using this as learning purposes, there really is no point to me to make a script where lies a command that already does what your trying to do with more steps..
 
Old 02-17-2004, 01:45 PM   #9
trout21
LQ Newbie
 
Registered: Jun 2003
Posts: 22

Original Poster
Rep: Reputation: 15
THANK YOU

Thats the one - thank you - I am on a virtual server so cannot interfere with my .bashrc file - though the script now works fine from your instructions.


Cheers.
trout.
 
Old 02-17-2004, 01:49 PM   #10
lugoteehalt
Senior Member
 
Registered: Sep 2003
Location: UK
Distribution: Debian
Posts: 1,215
Blog Entries: 2

Rep: Reputation: 49
If you are after giving the program a directory from the command line when you start it, then do:
program_name.sh life is a continuous cycle

If the program is:
#! /bin/sh
echo $0 $1 $2 $3 $5 $4

The output will be:
program_name.sh life is a cycle continuous

May have ommited some 's and some ,s, perhaps.
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change directory in a shell script? jdupre Linux - Newbie 13 06-25-2015 06:03 AM
script to change multiple filenames in a directory jeffreybluml Linux - Newbie 8 12-06-2006 02:46 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 07:54 PM
Script to change names of files in a directory geomonap Linux - General 2 12-03-2004 04:04 PM


All times are GMT -5. The time now is 09:10 AM.

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