[Switch] shell script error: -bash: xxx: /bin/sh^M: bad interpreter: No such file or directory

I wrote a shell script today, and then reported an error during execution. The content of the script is very simple and is only for testing:

Shell code   Favorite code
  1. #!/bin/sh  
  2.   
  3. echo "test shell "  

The specific error message is as follows:

Shell code   Favorite code
  1. [root@localhost test]# ./test.sh   
  2. -bash: ./test.sh: /bin/sh^M: bad interpreter: No such file or directory  

Since I was not familiar with the shell before, I found out the whole story after talking to my colleagues. The main reason is that I edited test.sh under Windows and uploaded it to the Linux system for execution. The format of the .sh file is dos format. And linux can only execute scripts in unix format.

 

We can view the format of the file through the vi editor. Proceed as follows:

1. First open the file with the vi command

Shell code   Favorite code
  1. [root@localhost test]# vi test.sh   

2. Use the :set ff command in vi command mode

You can see that the format of the changed file is dos

3. Modify the file format to unix

Method 1: Use vi to modify the file format

Command: set ff=unix

After execution, check the file format through the set ff command. The results are as follows:

Method 2: Modify directly using the dos2unix command

Shell code   Favorite code
  1. [root@localhost test]# dos2unix test.sh   
  2. dos2unix: converting file test.sh to UNIX format ...  

 Reprinted from: http://chenzhou123520.iteye.com/blog/1832890

Guess you like

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