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:
- #!/bin/sh
- echo "test shell "
The specific error message is as follows:
- [root@localhost test]# ./test.sh
- -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
- [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
- [root@localhost test]# dos2unix test.sh
- dos2unix: converting file test.sh to UNIX format ...
Reprinted from: http://chenzhou123520.iteye.com/blog/1832890