as root I execute the fs.sh script located in / var /

script:

#!/bin/bash -x echo 1 cd / 

I get the error:

 root@bill2:/var# bash fs.sh 1 : No such file or directory root@bill2:/var# 

Help me figure out why the script can’t get into the directory, and if I type cd / in the console, I’m fine.

File contents:

 $ od -bc fs.sh 0000000 043 041 057 142 151 156 057 142 141 163 150 040 055 170 015 012 # ! / bin / bash - x \r \n 0000020 145 143 150 157 040 061 015 012 143 144 040 057 015 012 echo 1 \r \ncd / \r \n 0000036 
  • Look at the script text with some hd (or od -bc ) - avp
  • root @ bill2: / var # od -bc fs.sh 0000000 043 041 057 142 151 156 057 142 141 163 150 040 055 170 015 012 #! / bin / bash - x \ r \ n 0000020 145 143 150 157 040 061 015 012 143 144 040 057 015 012 043 126 echo 1 \ r \ ncd / \ r \ n - Sebulba Kastorsky
  • And if to recreate it through the same nano or gedit , kate ? Here is direct from the question to copy and paste already in the editor? - don Rumata
  • 2
    \r at the end of the lines, remove (i.e., the lines should end with \n ) (well, at the very least, if you don’t cope, then after / in cd / type a space (several)) - avp

1 answer 1

the contents of your script:

 $ od -bc файл 0000000 043 041 057 142 151 156 057 142 141 163 150 040 055 170 015 012 # ! / bin / bash - x \r \n 0000020 145 143 150 157 040 061 015 012 143 144 040 057 015 012 echo 1 \r \ncd / \r \n 0000036 

you can see that the lines end with two characters: \r\n ( cr + lf ).

and must end with a single \r (cr).

you can convert the file, for example, using the dos2unix program (in popular distributions of the gnu / linux operating system, it is usually included in the package of the same name):

 $ dos2unix /путь/к/файлу 

inverse transform:

 $ unix2dos /путь/к/файлу 

For help, see the man dos2unix .

  • Thank! Rewrote in nano and it all worked - Sebulba Kastorsky