-bash: .sh : /bin/sh^M: bad interpreter: No such file or directory Troubleshooting

-bash: .sh : /bin/sh^M: bad interpreter: No such file or directory Troubleshooting

        Today, I deployed the newly written program to centos linux. As a result, I kept reporting this error when executing the script. I found a lot of information and finally solved it. Write it down here for future reference.

        This error is caused by the format system incompatibility of the script file. If we edit the shell file in the window environment and execute it under linux, this error will occur, because the newline characters of the file are different. There is a \r\n under the window, which is displayed as ^M under Linux, and \r under Linux, so it needs to be replaced.

        There are two solutions, if the file name is test.sh:

       (1) Execute  dos2unix    ( recommended )

             dos2unix   test.sh

       (2) vim test.sh -- open the file with vim

           :set ff=unix -- after execution, save and exit.

        Execute the script again and the problem is solved.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327000035&siteId=291194637