LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 08-16-2009, 02:07 PM   #1
neha_singhal
LQ Newbie
 
Registered: Aug 2009
Posts: 1
Thanked: 0
cd command not working through variable in shell script


[Log in to get rid of this advertisement]
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...
neha_singhal is offline  
Tag This Post , , , ,
Reply With Quote
Old 08-16-2009, 03:15 PM   #2
centosboy
Member
 
Registered: May 2009
Location: london
Distribution: centos5/opensuse11
Posts: 563
Thanked: 66
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
centosboy is offline     Reply With Quote
Old 08-16-2009, 03:15 PM   #3
TB0ne
Senior Member
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, Mandrake, RedHat, Gentoo, Slack
Posts: 4,065
Thanked: 142
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.
TB0ne is offline     Reply With Quote
Old 08-16-2009, 03:52 PM   #4
catkin
Senior Member
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.0
Posts: 1,850
Blog Entries: 6
Thanked: 226
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 03:56 PM.. Reason: Sub-editing
catkin is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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


All times are GMT -5. The time now is 08:18 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration