LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-25-2014, 03:05 AM   #1
torjemen
Member
 
Registered: Dec 2012
Posts: 37

Rep: Reputation: Disabled
cd command and shell script


Dear Sir,
I'm new using Linux.
I need to use cd command in shell script but it dosen't work.
Please help me.
best regards.
 
Old 02-25-2014, 03:27 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
Please post your script and explain how it fails. Note that "cd" works in shell script but doesn't affect the calling shell.
 
Old 02-25-2014, 03:28 AM   #3
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

what do you mean by "dosen't work"? What happens and what do you expect to happen? It may be best to post a very short script that demonstrates the problem.

Evo2.
 
Old 02-25-2014, 03:37 AM   #4
torjemen
Member
 
Registered: Dec 2012
Posts: 37

Original Poster
Rep: Reputation: Disabled
Hi for all,
It is very easy.
I'm trying to install ns3 and I'm using the following shell script called nsinstall.sh :
Quote:
cd /home/ns-allinone-3.19
./build.py --enable-examples --enable-test
when I type in the terminal
Quote:
sh nsinstall.sh
The current directory is not changed

br
 
Old 02-25-2014, 03:58 AM   #5
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware
Posts: 893

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
Your user is called ns-allinone-3.19 ?

I have a feeling that you probably want to do this:

cd /home/${USER}/ns-allineone-3.19


Of course, that assumes that ns-allinone-3.19 is located in your user folder. To confirm this, you may want to do something like:

realpath [and then drag and drop the ns-allinone-3.19 folder into your terminal]
and then press RETURN.
 
Old 02-25-2014, 04:12 AM   #6
torjemen
Member
 
Registered: Dec 2012
Posts: 37

Original Poster
Rep: Reputation: Disabled
Hi,
No, ns-allinone-3.19 is a folder in home folder.
I want to change my used directory from home to ns-allinone-3.19 and the execute the file ./build.py

If i type this command on terminal, it will be ok. however , I want to include this two command in the same shell script.

br
 
Old 02-25-2014, 04:17 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
that is ok too, it will be executed line by line. Just after the execution the shell will be exited and you will be returned to the current shell (where you started). And that shell will not changed at all (just the child)
 
Old 02-25-2014, 04:22 AM   #8
torjemen
Member
 
Registered: Dec 2012
Posts: 37

Original Poster
Rep: Reputation: Disabled
so how can I do it using shell script ???
 
Old 02-25-2014, 04:31 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
As pan64 said, you can put both lines ina script and run the script and it will do the job, but when it terminates, it is back in the original location because normally bash creates a sub-shell to run in.
However, if you use
Code:
. ./script.sh

# OR

source ./script.sh
you should end up in the target dir because those techniques don't create a sub-shell, but use the current shell.

Last edited by chrism01; 02-25-2014 at 04:35 AM.
 
Old 02-25-2014, 04:43 AM   #10
torjemen
Member
 
Registered: Dec 2012
Posts: 37

Original Poster
Rep: Reputation: Disabled
it is not working !!!
Quote:
bash: ./build.py : no such file or directory
Br
 
Old 02-25-2014, 04:50 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
that is not enough information to help further. What have you tried to execute in which directory? ...
 
Old 02-25-2014, 04:53 AM   #12
torjemen
Member
 
Registered: Dec 2012
Posts: 37

Original Poster
Rep: Reputation: Disabled
I'm in the home directroy and I want to change firstly to ns-allineone-3.19 directory and then execute the file ./build.py file under ns-allineone-3.19 directory
br
 
Old 02-25-2014, 05:01 AM   #13
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
that is even not an answer to that question (in #11). See #9
 
Old 02-25-2014, 10:12 AM   #14
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,649

Rep: Reputation: 391Reputation: 391Reputation: 391Reputation: 391
Quote:
Originally Posted by torjemen View Post
I'm in the home directroy and I want to change firstly to ns-allineone-3.19 directory and then execute the file ./build.py file under ns-allineone-3.19 directory
br
What says:

Code:
ls -l /home/ns-allineone-3.19/build.py
?
 
Old 02-25-2014, 01:50 PM   #15
torjemen
Member
 
Registered: Dec 2012
Posts: 37

Original Poster
Rep: Reputation: Disabled
I get the following message:
Quote:
[root@localhost home]# ls -l /home/ns-allinone-3.19/build.py
-rwxr-x---. 1 root root 5618 25 févr. 11:39 /home/ns-allinone-3.19/build.py
 
  


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
[SOLVED] nc command in script vs. in shell noguru Linux - Newbie 6 09-22-2012 10:10 AM
Shell script, Perl script, command or utility to convert Binary to text Perseus Programming 26 07-12-2012 07:00 AM
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 10:12 PM
Using shell command output as input in shell script - how to do? EnderX Linux - Newbie 2 06-30-2010 01:46 PM


All times are GMT -5. The time now is 04:44 PM.

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