Click to See Complete Forum and Search --> : Why isn't this working? Shell Script - "cd"
pwharff
10-23-2003, 09:08 PM
Ok, I have been working a shell script that searches for particular folders for deletion. My script is almost done, but I ran into a problem and I have no ideo why it isn't working!!! I have a file called "path" that contains the path to a folder that I need to "cd" to. So I thought this would be easy:
cd < path
This doesn't work and I don't understand why!!! The paths contained in this file are full paths and are quoted also. WHY oh WHY. I just don't get it.
bryan.6
10-23-2003, 09:28 PM
does the file just contain one path?
if so, try:
cd `cat path`
evac-q8r
10-23-2003, 09:29 PM
I think you may be better off by saving the path to an environment variable say PATH and then executing the command cd $PATH or something similar. That would be my first suggestion.
EVAC
bryan.6
10-23-2003, 10:30 PM
a good suggestion, however don't use PATH, use something else, as PATH is usually already in use.
AdaHacker
10-23-2003, 10:39 PM
Originally posted by pwharff
So I thought this would be easy:
cd < path
This doesn't work and I don't understand why!!! The paths contained in this file are full paths and are quoted also. WHY oh WHY. I just don't get it. This doesn't work because you're trying to use input redirection with a command that doesn't take any input. The command '<' operator tells a command, 'cd' in this case, to read its standard input stream from a file instead of the console. However, 'cd' does not use a standard input stream - it reads directory lists from command line parameters, which are not the same thing. In other words, you're trying to send input to a command that doesn't need or understand any input, which obviously doesn't work. Use one of the above suggestions instead.
pwharff
10-24-2003, 12:39 AM
So far:
cd `cat path`
did not work. Here is my the path in the file path:
/Users/paul/Pictures/Test Library/Albums/Cool
And I have tried different variations, such as:
"/Users/paul/Pictures/Test Library/Albums/Cool"
/Users/paul/Pictures/Test\ Library/Albums/Cool
"$HOME/Pictures/Test Library/Albums/Cool"
$HOME/Pictures/Test\ Library/Albums/Cool
And these do not work either. I have also tried to set an env. variable:
librarypath="/Users/paul/Pictures/Test\ Library/Albums/Cool"
export librarypath
Nothing has worked. I am just stumped!!! I was so excited because my script was almost finished and now I run into something so trivial as this. I'm just bumbed! :(
terribleRobbo
10-24-2003, 03:53 AM
Well... You can always try the 'manual' method:
export OLDPWD=$PWD
export PWD="your/dir/here"
Strictly, you don't need the OLDPWD bit, it's just there for the sake of completeness.
Let me know how it works. :)
Ja,
Rob H
MadNewbie
10-24-2003, 07:42 AM
Are you sure there is not a typo in your path or anything? Because cd `cat path` will work as long as the path file is in the working directory.
Likewise using an env. variable like that will work.(Tested it on my box) Are you sure you are referring to it rigth? ie.
cd $librarypath
So I really dont get why you dont get it to work. You are using bash right?
MadNewbie
pwharff
10-24-2003, 10:55 AM
Originally posted by MadNewbie
Are you sure there is not a typo in your path or anything? Because cd `cat path` will work as long as the path file is in the working directory.
Likewise using an env. variable like that will work.(Tested it on my box) Are you sure you are referring to it rigth? ie.
cd $librarypath
So I really dont get why you dont get it to work. You are using bash right?
MadNewbie
Yes I am using bash. I CAN get this to work if the path doesn't contain any spaces or meta-characters. However as you can see above, my path does include meta-characters and spaces. Basically it all comes down to quoting, and no matter how I quote it, it doesn't work! In fact why don't you try to get it work when the path includes a space or meta-character.
pwharff
10-24-2003, 10:58 AM
Originally posted by terribleRobbo
Well... You can always try the 'manual' method:
export OLDPWD=$PWD
export PWD="your/dir/here"
Strictly, you don't need the OLDPWD bit, it's just there for the sake of completeness.
Let me know how it works. :)
Ja,
Rob H
Tried it and it didn't work either, however it does work when no spaces or meta-characters are included in the path.
evac-q8r
10-24-2003, 02:30 PM
Honestly, I suggest you move your stuff to a different directory without the space in it and get a working version of it going. In the meantime you can fiddle around with getting the old directory to work. But I can tell you it is something very subtle with quotes and spaces and I read about it somewhere but have no idea where. I think it was an O'Reilly Book called "Learning the BASH Shell"
EVAC
pwharff
10-24-2003, 05:24 PM
Thank you guys for all the help. I had the quoting wrong or should I say, the wrong combination of quoting. This has taught me a lesson that I will not soon forget!!! Here's the syntax:
librarypath=`cat path`
cd "$librarypath"
While the path inside the file "path" was:
/home/paul/Pictures/Test Library/
A newbie learning! :) BTW, the great users at MacOSXHints.com helped me out.