maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Maemo 5 / Fremantle (https://talk.maemo.org/forumdisplay.php?f=40)
-   -   Can't cd in bash script (https://talk.maemo.org/showthread.php?t=67836)

FRuMMaGe 2011-01-04 02:35

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?

kureyon 2011-01-04 10:20

Re: Can't cd in bash script
 
Show some example code that illustrates your problem?

paai 2011-01-04 11:13

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

littlebadger 2011-01-04 11:23

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...)

FRuMMaGe 2011-01-04 13:41

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

fgevaerts 2011-01-04 15:14

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.

FRuMMaGe 2011-01-04 15:45

Re: Can't cd in bash script
 
Quote:

Originally Posted by fgevaerts (Post 911502)
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

paai 2011-01-04 15:46

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

fgevaerts 2011-01-04 16:02

Re: Can't cd in bash script
 
Quote:

Originally Posted by paai (Post 911522)
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.

FRuMMaGe 2011-01-04 17:01

Re: Can't cd in bash script
 
Quote:

Originally Posted by fgevaerts (Post 911536)
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?

fgevaerts 2011-01-04 17:14

Re: Can't cd in bash script
 
One way would be
tr -d '\015' < yourfile.sh > yournewfile.sh

(\015 means "octal 13", which is the CR or \r character)

Or use an editor that can control this. I can't really recommend one though (I know vim can do it, but vim isn't really a good editor to recommend to someone who likes leafpad...)

kureyon 2011-01-04 17:25

Re: Can't cd in bash script
 
Quote:

Originally Posted by FRuMMaGe (Post 911569)
How do I correct this?

You can remove the CRs (\r):
Code:

tr -d "\r" < oldname.sh > newname.sh
Or configure leafpad to save in unix format?

gregoranderson 2011-01-04 17:25

Re: Can't cd in bash script
 
Quote:

Originally Posted by fgevaerts (Post 911574)
One way would be
Or use an editor that can control this. I can't really recommend one though (I know vim can do it, but vim isn't really a good editor to recommend to someone who likes leafpad...)

Just for completeness, in vim you would use the set fileformat instruction:

Code:

:se ff=unix
Vim on the old N900 keyboard is tricky, but it's been your main tool for year then you get used to it (especially if you have gathered together a hefty vimrc file in that time).

http://vim.wikia.com/wiki/File_format

gregoranderson 2011-01-04 17:40

Re: Can't cd in bash script
 
1 Attachment(s)
I've compiled flip for you (see https://ccrma.stanford.edu/~craig/utility/flip/). Dead easy to check for file formats and flip between them.

Code:

[sbox-FREMANTLE_ARMEL: ~] > ./flip

Usage: ./flip [-t|-u|-d|-m] filename[s]
  Converts ASCII files between Unix, MS-DOS/Windows, or Macintosh newline formats

  Options:
      -u  =  convert file(s) to Unix newline format (newline)
      -d  =  convert file(s) to MS-DOS/Windows newline format (linefeed + newline)
      -m  =  convert file(s) to Macintosh newline format (linefeed)
      -t  =  display current file type, no file modifications

[sbox-FREMANTLE_ARMEL: ~] >


FRuMMaGe 2011-01-04 19:27

Re: Can't cd in bash script
 
Quote:

Originally Posted by kureyon (Post 911582)
You can remove the CRs (\r):
Code:

tr -d "\r" < oldname.sh > newname.sh
Or configure leafpad to save in unix format?

Thanks to everyone, this method worked.

Just out of interest, wtf? :p

Late to the party 2011-01-06 09:38

Re: Can't cd in bash script
 
Quote:

Originally Posted by FRuMMaGe (Post 911652)
Thanks to everyone, this method worked.

Just out of interest, wtf? :p

Historical reasons:

http://en.wikipedia.org/wiki/Newline#History

FRuMMaGe 2011-01-06 17:30

Re: Can't cd in bash script
 
Quote:

Originally Posted by Late to the party (Post 913036)

Thanks. Very informative


All times are GMT. The time now is 08:05.

vBulletin® Version 3.8.8