LinuxQuestions.org
Visit Jeremy's Blog.
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 05-28-2013, 06:12 PM   #1
jdgustaf
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
"pwd" doesn't work in csh script


I'm not a newbie, exactly, but this is a very simple problem for which I haven't been able to google a solution. I think this is the right forum for it.

I have a csh script in which I'm trying to perform some very basic tasks with a set of files, and the first command is setting the directory environment variable. I'd like to just use "pwd", but this does not work. The problem can be illustrated with the simplest of scripts:

#!/bin/csh

setenv DIR `pwd`
echo 'the current directory is' $DIR

end


The pwd command invariably prints out my home directory, not the current directory. It's a minor problem - I can always skirt the issue by just writing out the full directory path, but I'd like to not have to redo this every time I want to modify the script in a different directory. And I don't understand why pwd doesn't work.

I'm running Mac OS X Mountain Lion (version 10.8.3). The default shell for Mac is bash, although I typically switch to tcsh whenever I open a terminal. As far as I know, this doesn't have anything to do with my problem, though.

Any help is appreciated. Thanks!

Last edited by jdgustaf; 05-28-2013 at 06:14 PM.
 
Old 05-28-2013, 07:05 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,561

Rep: Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127
Code:
$ pwd
/home/user/temp
$ cat script.sh 
#!/bin/bash

export DIR=`pwd`
echo "The current directory is $DIR"

$ cat script.csh 
#!/bin/csh

setenv DIR `pwd`
echo 'The current directory is' $DIR

$ ./script.sh 
The current directory is /home/user/temp
$ ./script.csh 
The current directory is /home/user/temp
Works for me. Maybe this is a Mac issue?
 
Old 05-28-2013, 07:06 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
There's a reason the default is bash.
Is there any particular reason you use csh/tcsh?
http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
 
Old 05-28-2013, 07:31 PM   #4
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,898

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
A quick look at the csh man page tells me that pwd will show your home directory, while cwd will show the current working directory.
 
Old 05-28-2013, 08:39 PM   #5
jdgustaf
LQ Newbie
 
Registered: May 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hmm, cwd has the same issue.

chrism01, I use (t)csh primarily because I know it best. My advisor recommended it when I started research with him a year and a half ago, and a lot of shell scripts on my work's computer system use csh. On my work system, this pwd issue doesn't happen; it's only on my home computer. Anyways, I've definitely seen criticisms of csh before, similar to those in your link.
 
Old 05-28-2013, 08:46 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,714

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
You can always try using /bin/pwd instead. This will not be based on the shell.
 
Old 05-29-2013, 01:12 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Fair enough... You don't see it used very often these days, so just thought I'd ask.
 
Old 05-29-2013, 05:51 AM   #8
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,898

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
On my work system, this pwd issue doesn't happen; it's only on my home computer
Have you compared csh version numbers between your work and home systems? You may only need an upgrade to fix the problem.
 
Old 05-30-2013, 05:23 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
Here are more good reasons not to use the c-shell:

http://www.grymoire.com/Unix/CshTop10.txt

Just reading about it gives me the willies.

If you have to deal with it for external reasons, then at least be sure you understand its weaknesses. But I'd really recommend not using it at all in your own scripts, if at all possible.

Take a bit of time to learn bash, and you'll probably never want to go back. The Bash Guide is a very good place to start.
 
  


Reply


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
[SOLVED] "ifconfig eth0 up" doesn't work, cause of "no such file or directory" Sebi94 Linux - Networking 6 08-21-2012 11:39 PM
C++ - "snprintf" inside "for" doesn't work as expected. (int to char*) Repgahroll Programming 14 08-31-2010 09:27 AM
Any way to get "Alice"; "Call of Duty" series and "Descent 3" to work? JBailey742 Linux - Games 13 06-23-2006 02:34 PM
Compromised? Files "/usr/lib.hwm", "/usr/lib.pwd", "/usr/lib.pwi" Klaus Pforte Linux - Security 4 09-29-2004 12:33 AM
An existing file in the PWD isn't displayed with "cat".. linuxharsha Linux - General 2 02-17-2004 08:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:14 PM.

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