![]() |
Can't cd in bash script
I've come across a ridiculously infuriating problem.
I'm trying to load a simple bash script that cd's to a directory and executes a function, but whenever I sh the script, it ALWAYS tells me "can't cd /whatever/directory" (obviously this is not the actual directory) For debugging purposes, I have set the directory name to things that DEFINITELY exist, such as /home/user/MyDocs/ I have run the command by itself in xterm with no problems, but it gets picky when I run it from a script. I have chmod +x it and everything is as it should be. What the hell is wrong? |
Re: Can't cd in bash script
Show some example code that illustrates your problem?
|
Re: Can't cd in bash script
wildly speculating... cd is not a program but part of the shell, isn't it? Does it make a difference if you execute your script using 'sh scriptname' or by chmodding it tox x and then execute it directly?
paai |
Re: Can't cd in bash script
another speculation...
is the script in fat partition? is so, try putting it in ext2 partition ( /home/user/scripts or whatever you want...) |
Re: Can't cd in bash script
The script is basically just:
cd /home/user/MyDocs/ sh config.sh Tried chmodding it and running it directly but it just says "permission denied". Its now in my opt diretory The commands run fine when copied to the terminal, that's why this is so irritating |
Re: Can't cd in bash script
Are line endings in the script correct? If you created the script on windows, chances are lines end on CR-LF instead of LF only, and the shell will include that CR character at the end of the command it parses. I'm pretty sure the directory you want to cd to doesn't have a CR character in its name.
|
Re: Can't cd in bash script
Quote:
|
Re: Can't cd in bash script
I think your script works just fine :-) Considerthe following variant:
cd /home/user/MyDocs/ pwd where pwd of course displays the current working directory. You will see that the script prints '/home/user/MyDocs/', so it executes 'cd' all right.. The unexpected part is that after execution, control returns to the original situation. Paai |
Re: Can't cd in bash script
Quote:
FRuMMaGe: Can you run "od -c yourscript.sh", and check that there are indeed only \n characters for line ending, and no \r? The "can't cd to /whatever/directory" error is printed if you do have this wrong, which is why I wouldn't rule out this possibility without seriously checking. |
Re: Can't cd in bash script
Quote:
How do I correct this? |
All times are GMT. The time now is 02:31. |
vBulletin® Version 3.8.8