Click to See Complete Forum and Search --> : "cd" command not working in .sh
Jonboy_us
02-28-2006, 12:11 AM
I'm trying to get AWStats setup and everything is in place except the first part of my shell script. I can type in a prompt to "cd /www/logs" and that works, but when I put it in my shell script, it does not change locations. It ends up seemingly skipping over it and builds the stats pages in the directory I run the script from.
Any ideas why "cd" is not working but the rest of the commands that I run are?
Thanks,
Jonboy
voidinit
02-28-2006, 03:13 AM
I'm pretty sure the script does in fact change locations in it's shell which is a subshell of yours. You won't see a change in directory but the pwd of the script will change after call cd /some/path within the scripts.
do this:
echo `pwd`
cd /some/path
echo `pwd`
You will see something like
/home/you
/some/path
echoed to the screen.
There might be another reason the files aren't being written where they are supposed to go.
Jonboy_us
03-01-2006, 12:16 AM
The echo helped to see what was going on...sort of. :confused: The error it gave me was:
/www
: No such file or directory2: cd: /www/logs
/www
...which is wrong, the dir is there. I created a new sh and it switched between the folders just fine. I added the awstats command under the cd command in the new script and now that new scrip works fine. If you look at the two side by side, they are identical. I don't get it but I'm just happy that works. :D
Thanks man!
bwkaz
03-01-2006, 09:05 PM
I've got a sneaking suspicion that you wrote this script on Windows. Right?
The reason I think that is because of the strange line-breaks in the error message. Perhaps you have a CR character after the directory name that you're passing to "cd" (at the end of the line); if you do, the shell may not treat it as a word separator. Windows's end-of-line sequence is CR/LF; Linux's is LF.
See if it helps to run the script through dos2unix.
Jonboy_us
03-01-2006, 11:19 PM
You know, now that you say that, yes that file was made in Window$. My uncle made it for his site and emailed it to me. I just tweaked it and made it match my system.
Cool, now I know. Thanks guys!!! :) I love this place!