LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cd command not working through variable in shell script (http://www.linuxquestions.org/questions/linux-newbie-8/cd-command-not-working-through-variable-in-shell-script-747961/)

neha_singhal 08-16-2009 01:07 PM

cd command not working through variable in shell script
 
hi

I am trying to use cd command in shell script like this:

script trying to run:

directoryName='Test'
echo "directory to change "$directoryName
cd $directoryName

command prompt (screen shot)

Administrator@neha /cygdrive/c
$ ./file1.sh
directory to change:Test
: No such file or directoryt
/cygdrive/c

Administrator@neha /cygdrive/c
$ cd Test
Administrator@neha /cygdrive/c/Test


i have tried many options like
cd ${directoryName}
cd "$directoryName"
but options are also not working

Kindly help me out...

centosboy 08-16-2009 02:15 PM

Quote:

Originally Posted by neha_singhal (Post 3645551)
hi

I am trying to use cd command in shell script like this:

script trying to run:

directoryName='Test'
echo "directory to change "$directoryName
cd $directoryName

command prompt (screen shot)

Administrator@neha /cygdrive/c
$ ./file1.sh
directory to change:Test
: No such file or directoryt
/cygdrive/c

Administrator@neha /cygdrive/c
$ cd Test
Administrator@neha /cygdrive/c/Test


i have tried many options like
cd ${directoryName}
cd "$directoryName"
but options are also not working

Kindly help me out...

try the variable as

Code:

directoryName=Test

TB0ne 08-16-2009 02:15 PM

Quote:

Originally Posted by neha_singhal (Post 3645551)
hi

I am trying to use cd command in shell script like this:

script trying to run:

directoryName='Test'
echo "directory to change "$directoryName
cd $directoryName

command prompt (screen shot)

Administrator@neha /cygdrive/c
$ ./file1.sh
directory to change:Test
: No such file or directoryt
/cygdrive/c

Administrator@neha /cygdrive/c
$ cd Test
Administrator@neha /cygdrive/c/Test


i have tried many options like
cd ${directoryName}
cd "$directoryName"
but options are also not working

Kindly help me out...

Right. That's a shell script, it executes the commands, and leaves you in the same place you were when it started.

Try running it as "source ./file1.sh". I'd also suggest looking at some of the bash scripting tutorials that you can find on the web.

catkin 08-16-2009 02:52 PM

If those were exact copy-and-paste from your screen then they don't make sense.

BTW, it's easier to read your stuff if you put it between code tags, that's CODE and /CODE in square brackets like []. If you switch to Advanced mode, there's a # icon that automates it.

You posted that the script was
Code:

directoryName='Test'
echo "directory to change "$directoryName
cd $directoryName

and when run from the command line the output was
Code:

Administrator@neha /cygdrive/c
$ ./file1.sh
directory to change:Test
: No such file or directoryt
/cygdrive/c

First off, the script contains echo "directory to change "$directoryName and the output from it was directory to change:Test. Where did the space go and where did the ":" come from?

Secondly, the script contains cd $directoryName and the output from it was : No such file or directoryt. Here's what I get when trying to cd to a non-existant directory at the prompt
Code:

c@CW8:~$ cd kdfghjdhfg
bash: cd: kdfghjdhfg: No such file or directory

Where did your bash: cd go and where did the "t" come from on the end of directoryt?

Thirdly, where did the /cygdrive/c come from? Is it part of Administrator@neha /cygdrive/c?

If those were not exact copy-and-pastes from your system then it is difficult for us to help you!

One possibility that might cause the problem is if cd is an alias. You can get the un-aliased cd by escaping it with a "\" as shown below. I've added a couple of lines to the script to help with debugging; please post the output from running this script.
Code:

directoryName='Test'
echo "directory to change "$directoryName
\pwd
/bin/ls
\cd $directoryName


serodores 02-23-2010 09:21 PM

Avoid tee too
 
I also noticed if you instinctually added tee it can cause issues.

E.g.,

cd $DIRECTORY | tee -a $LOGDIRECTORY/out.log

causes issues. It won't show an error, but it won't change your directory either. But

cd $DIRECTORY

works fine. (Logging use of the cd command doesn't buy you much anyway.)

TB0ne 02-24-2010 09:04 AM

Quote:

Originally Posted by serodores (Post 3874525)
I also noticed if you instinctually added tee it can cause issues.

E.g.,

cd $DIRECTORY | tee -a $LOGDIRECTORY/out.log

causes issues. It won't show an error, but it won't change your directory either. But

cd $DIRECTORY

works fine. (Logging use of the cd command doesn't buy you much anyway.)

Right...but this thread was closed last year in August.


All times are GMT -5. The time now is 09:11 PM.