cd command not executing in if else loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cd command not executing in if else loop
# 1  
Old 01-20-2011
cd command not executing in if else loop

Hi,

I am executing below script

Code:
s1=`pwd`
s2=/space
if [ $s1 == $s2 ]
then
echo "done"
else
echo "mistake"
cd /tmp
fi

I am not able to migrate to /tmp directory if the condition is not true.However mistake is being printed.Means cd command is not working here.All other commands except cd are working.
Please help
# 2  
Old 01-20-2011
It is working inside your script, but as soon as the script is finished, the sub shell in which the script runs closes and you are back to the parent shell and there the working directory remains unchanged.

It would change if you called the script like this:
Code:
. /path/to/script

But this is not usually done from an interactive shell.
# 3  
Old 01-20-2011
cd command might well work, but once you quit your script you will find yourself in the directory you were in when executing the script...
Try to add after echo mistake:
Code:
cd /tmp
pwd
read keyb
fi

it will display where you are after cd /tmp, and wait you type a char on the keyboard to exit...
# 4  
Old 01-20-2011
Script becomes own environment

When you execute a script, it is its own entity. A script execution creates its own space to do what it needs to do, and when done, returns keyboard control to you. So 'cd' commands and other commands have no effect on you once the script is done executing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Warn Before Executing Particular Command

I'm running CentOS 6.8 and use bash. I would like a warning to appear to the user who runs the command "service httpd restart" E.g. # service httpd restart are you sure y/n n # (or if y, the command executes). I looked into it a little but am not sure of the best approach. Aliases I... (2 Replies)
Discussion started by: spacegoose
2 Replies

2. Shell Programming and Scripting

Storing passing and executing select statement in loop

Hi, i want to do the following: Grep the following kind of strings for the 15digit ID which is stored in filename1: "14:06:51.396 INFO BMCREMEDYSD INPUT-ACTION Failed to retrieve Remedy Incident Modification record: 000000000039047 org.apache.axis2.AxisFault: Read timed out - complete... (9 Replies)
Discussion started by: Khushbu
9 Replies

3. Shell Programming and Scripting

Help with executing awk and While loop

Hi All, I have a file say, sample.txt Source Name: xxx Department|Revenue 1001|3252 1002|3345 I am using the above file in one of my script. I need to read from Line 3 of the above the file and do some process. My script has a code: awk 'NR > 2' sample.txt | while read Dep; do... (9 Replies)
Discussion started by: machomaddy
9 Replies

4. UNIX for Dummies Questions & Answers

executing a command using ssh

Hi All, I am trying to execute a command using ssh as below. ssh user123@servername "which ctmcontb" It is gving the error as below no ctmcontb in /usr/bin /usr/sbin /opt/sysadm/bin Not sure from where the PATH is getting picked up. But When I login direclty to the server I am... (5 Replies)
Discussion started by: anilreddy103
5 Replies

5. Shell Programming and Scripting

Problem in executing For loop....

Hi frnds I trying to execute the following ksh, #!/bin/ksh file=$OBS_APP_PATH/config/com/uhg/obs/inbound/configs/ServiceFeeDetail.xml inputFileDir=$OBS_APP_PATH/config/com/uhg/obs/inbound/configs/ echo $file echo $cntWrd if then echo "Inside IF" for i in 'ls -t1... (7 Replies)
Discussion started by: balesh
7 Replies

6. Shell Programming and Scripting

error executing script in a while loop

Im unable to run scripts when i read each script thru a while loop. Is this way of execution thru while loop is wrong or is there any error in the script. I get the following error msg and i use ksh. ./vftest.ksh: ./add.ksh -customer 4875 -dim RD,TRND,TT,HS,MRKT,PRDC,ACV,CV,FCT: not found ... (7 Replies)
Discussion started by: michaelrozar17
7 Replies

7. Shell Programming and Scripting

ssh and executing a for loop

Hi all, I am trying to run a script which is expected to do: on the remote machine, There are two directories /export/home/abc1,/export/home/abc2 i am trying to do, ssh SERVERNAME "for i in `ls -l /export/home/abc*|awk '{print $9}'`; do cd $i; ls -l; done" But its not working ,iam... (11 Replies)
Discussion started by: Jartan
11 Replies

8. Shell Programming and Scripting

Script not executing second loop

I have a server that receives backup files from several servers. Each server has its own directory to scp their files into, some of the files are received as .tar files and need to be compressed before being dumped. When the scp of the tar file is complete a file named 'flag' is also sent to... (2 Replies)
Discussion started by: thumper
2 Replies

9. Shell Programming and Scripting

Print out loop index on the console after executing each sybase DB query

Hello Guys, Well, using shell script, I'm doing loop on DB query as below: isql -Usa -Ptest -I /opt/sybase/interfaces << EOF use testdb go declare @i int select @i = 1 while(@i <= 5) begin Insert into TEST values (@i,"Test","TestDesc") select @i = @i + 1 end go EOF The Issue... (2 Replies)
Discussion started by: Alaeddin
2 Replies

10. UNIX for Dummies Questions & Answers

Executing a unix command

Hi, I need to execute the following unix command through my java code - zip -e When i execute this command from the command prompt, i am prompted for a password in the following manner - Enter password: Verify password: Is it possible to provide the password inthe first command itself... (5 Replies)
Discussion started by: jacob23
5 Replies
Login or Register to Ask a Question