Menu

Main Menu
Talk Get Daily Search

Member's Online

    User Name
    Password

    Can't cd in bash script

    Reply
    Page 1 of 2 | 1   2   | Next
    FRuMMaGe | # 1 | 2011-01-04, 02:35 | Report

    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?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    kureyon | # 2 | 2011-01-04, 10:20 | Report

    Show some example code that illustrates your problem?

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to kureyon For This Useful Post:
    codeMonkey

     
    paai | # 3 | 2011-01-04, 11:13 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to paai For This Useful Post:
    Mentalist Traceur

     
    littlebadger | # 4 | 2011-01-04, 11:23 | Report

    another speculation...
    is the script in fat partition? is so, try putting it in ext2 partition ( /home/user/scripts or whatever you want...)

    Edit | Forward | Quote | Quick Reply | Thanks
    The Following User Says Thank You to littlebadger For This Useful Post:
    Mentalist Traceur

     
    FRuMMaGe | # 5 | 2011-01-04, 13:41 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    fgevaerts | # 6 | 2011-01-04, 15:14 | Report

    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.

    Edit | Forward | Quote | Quick Reply | Thanks

     
    FRuMMaGe | # 7 | 2011-01-04, 15:45 | Report

    Originally Posted by fgevaerts View Post
    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.
    Nope. made it on leafpad with my n900

    Edit | Forward | Quote | Quick Reply | Thanks

     
    paai | # 8 | 2011-01-04, 15:46 | Report

    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

    Edit | Forward | Quote | Quick Reply | Thanks

     
    fgevaerts | # 9 | 2011-01-04, 16:02 | Report

    Originally Posted by paai View Post
    it executes 'cd' all right..
    It wouldn't say "can't cd to /whatever/directory" then.

    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.

    Edit | Forward | Quote | Quick Reply | Thanks

    Last edited by fgevaerts; 2011-01-04 at 16:11.
    The Following 2 Users Say Thank You to fgevaerts For This Useful Post:
    FRuMMaGe, littlebadger

     
    FRuMMaGe | # 10 | 2011-01-04, 17:01 | Report

    Originally Posted by fgevaerts View Post
    It wouldn't say "can't cd to /whatever/directory" then.

    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.
    I ran your command and it does indeed end in an /r then an /n

    How do I correct this?

    Edit | Forward | Quote | Quick Reply | Thanks

     
    Page 1 of 2 | 1   2   | Next
vBulletin® Version 3.8.8
Normal Logout