Search code examples
linuxbashshellcentoscd

shell script cd fails even though the path is correct


I need to do a script to extract a tar at a specified location.

I did something simple like:

cp test.tar /var/www/html
cd /var/www/html
tar xvf test.tar

If I execute the commands by hand everything is OK. If I save them in a .sh then use #bash script.sh, I get the following error ": Not a directory cd: /var/www/html". Any ideea why?

Ty for your time.

Notes: I tried the script version on a virtual machine (CentOS 5.5) and the script worked fine, the problem occurs on the real machine where I want to use it (I used same OS disk image, same configurations as on the virtual machine... this makes it really really odd for me).

Added: Also I try invoking something like service mysqld start... this also fails saying that a dir doesn't exist (still if I run by hand it works.).


Solution

  • I solved the problem - it is quite interesting).

    I created the script on a virtual machine running on windows with a centos os, the enter in windows is "\r\n" while in linux is "\n".

    The script worked on the vm because the code for enter was correct, while on the second computer, with native linux it was incorrect. I created exactly the same script on linux and everything went back 2 normal ;).

    Note... the mkdir part worked because I used another, simplified script written on linux.