How to work command 'cd' in shell script?


 
Thread Tools Search this Thread
# 1  
How to work command 'cd' in shell script?

I have simple script. like that, I am working on /usr/local/src and also under src folder there is a ft folder

Code:
#!/bin/ksh
#!/bin/bash
dirpath="/usr/local/src/ft"
echo $dirpath
cd $dirpath

echo displays ok "/usr/local/src/ft"

but that doesn't enter "ft" folder. stays in current folder (/usr/local/src)

how can I using cd in the script?

thanks

Last edited by pludi; 03-16-2012 at 09:00 AM..
# 2  
How do you invoke your script? The script actually enters that folder, but starts a subshell if you invoke it like ./yourScriptName.sh. After completion the subshell terminates and leaves you in your normal shell which was not changed. You have to source the script to execute it it your current shell. The syntax for that is dot blank script:
Code:
. ./yourScriptName.sh

# 3  
Quote:
Originally Posted by cero
How do you invoke your script? The script actually enters that folder, but starts a subshell if you invoke it like ./yourScriptName.sh. After completion the subshell terminates and leaves you in your normal shell which was not changed. You have to source the script to execute it it your current shell. The syntax for that is dot blank script:
Code:
. ./yourScriptName.sh

I invoked ./dnm.sh like that in the current shell.

---------- Post updated at 05:33 AM ---------- Previous update was at 05:24 AM ----------

thank you
# 4  
@ F@NTOM
Please re-read cero's post.
When you invoke a script with dot-space-dot-slash-scriptname the scriptname executes in the current environment and not in a subshell. Try it.



Quote:
#!/bin/ksh
#!/bin/bash
Only the FIRST line is effective as a she-bang line. The second line is just a comment line. This script will use /bin/ksh .
# 5  
Quote:
Originally Posted by methyl
@ F@NTOM
Please re-read cero's post.
When you invoke a script with dot-space-dot-slash-scriptname the scriptname executes in the current environment and not in a subshell. Try it.




Only the FIRST line is effective as a she-bang line. The second line is just a comment line. This script will use /bin/ksh .
works fine ok thank you
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Medium
Zero factorial is equal to zero.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Shell copy command don't work

I want copy Files: But cp don't work: shell_exec("cp ".$pfad." ".$genzone); shell_exec("cp ../Chrysanthemum.jpg ../test"); Whats wrong? (3 Replies)
Discussion started by: Linuxmann
3 Replies

2. Shell Programming and Scripting

Shell script to work on dates

Hi Sir/Madam I have a file data.txt like below file_name date_of_creation x 2/10/2012 y 8/11/2010 z 11/3/2013 a 2/10/2013 b 3/10/2013 c ... (4 Replies)
Discussion started by: kumar85shiv
4 Replies

3. UNIX for Dummies Questions & Answers

[SOLVED] Mv command doesnt work in shell script

Hi All, i created the below script to move file with xml extension from one directory to another,but the mv command is not working inside the shell script, #!/us/bin/ksh filepath="/apps/extract" filename="*.xml" foldername=2191POB000_$(date +%Y%m%d%H%M%S) mkdir -p "$filepath/$foldername"... (3 Replies)
Discussion started by: Radhas
3 Replies

4. Shell Programming and Scripting

Shell running with nohup not work, but with sh command yes..

why, when i run my shell with sh command it run without problem, but when run with nohup nombreshell.sh $ command not run correctly... values ​​obtained from database are not displayed in the csv file. My shell do that: 1 data base access with sqlplus: read_sql_stmt() { typeset stmt=$1... (3 Replies)
Discussion started by: joaco
3 Replies

5. UNIX for Dummies Questions & Answers

find command in shell script doesn't work

Hello all, Something strange going on with a shell script I'm writing. It's trying to write a list of files that it finds in a given directory to another file. But I also have a skip list so matching files that are in that skip list should be, well uhm, skipped :) Here's the code of my... (2 Replies)
Discussion started by: StijnV
2 Replies

6. AIX

AIX Shell script does not work

Hi. I created schell script for sending messages to some processes in AIX: #!/bin/sh BSE=/infor/ERPLN/bse BSE_TMP=/infor/ERPLN/bse/tmp export BSE BSE_TMP for i in `ps -eo pid,comm | grep bshell | cut -f 1 -d " "` do /something $i done Unfortunatelly this script does not work on... (6 Replies)
Discussion started by: giovanni
6 Replies

7. Shell Programming and Scripting

SSH shell script does not work

Hello I have a Zabbix Server were Linux kernel version 2.6.26-2-amd64 is running. now my Question how can i make a script that does: - connect with ssh to a device - get the systeminfo (but only the interfaces, mac adresses, serialnumber and Software version) - write the output in a file ... (18 Replies)
Discussion started by: Fraggy
18 Replies

8. Shell Programming and Scripting

I need to do a work to my job, but i m new in script shell, someone can help with this..

I need to do a work to my job, but i m new in script shell, someone can help with this.. :confused: Description Bsafe The command creates a backup directory of each month at the command line (arguments of the script). The names of directories to copy will always be specified for the... (4 Replies)
Discussion started by: strshel
4 Replies

9. UNIX for Dummies Questions & Answers

more command does not work in bash shell

is there a different command to display contents of a file on the output in bash shell? i tried more and it does not work. (7 Replies)
Discussion started by: npatwardhan
7 Replies

10. UNIX for Dummies Questions & Answers

Command work but not in SH script

Command works but not in SH At terminal if i type: scp test.tar.gz user1@server2:/home/user Everything run smoothly (keyed, no password need) At script , test.sh #!/bin/sh scp test.tar.gz user1@server2:/home/user Nothing happen and clue ? ? ? ? ? (3 Replies)
Discussion started by: cititester
3 Replies

Featured Tech Videos