LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 08-16-2009, 01:07 PM   #1
neha_singhal
LQ Newbie
 
Registered: Aug 2009
Posts: 1

Rep: Reputation: 0
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...
 
Old 08-16-2009, 02:15 PM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by neha_singhal View Post
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
 
Old 08-16-2009, 02:15 PM   #3
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,209

Rep: Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473
Quote:
Originally Posted by neha_singhal View Post
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.
 
Old 08-16-2009, 02:52 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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

Last edited by catkin; 08-16-2009 at 02:56 PM. Reason: Sub-editing
 
Old 02-23-2010, 09:21 PM   #5
serodores
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Rep: Reputation: 0
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.)
 
Old 02-24-2010, 09:04 AM   #6
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,209

Rep: Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473Reputation: 2473
Quote:
Originally Posted by serodores View Post
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.
 
  


Reply

Tags
cd, command, script, shell


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
csh Shell Script: Getting wc command output to variable to do math on it, how? vxc69 Programming 5 05-04-2009 04:31 PM
Grab results of GRUB's find command into a shell script variable kushalkoolwal Programming 6 02-03-2009 05:04 PM
Shell script: How to include a variable between apostrophes within a command guarriman Programming 3 02-23-2007 03:12 AM
Problem with shell script using a variable from a unix command. abefroman Programming 1 05-10-2006 02:14 PM
Odd problem with making a variable the output of a command in a shell script linux=future Programming 3 12-13-2005 09:45 PM


All times are GMT -5. The time now is 11:42 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration