Sometimes an error will be reported when executing the script:
[root@host1 shell]# sh -x test. sh + $ ' \r ' : command not found test. sh : line 37 : syntax error: unexpected end of file
reasons may be:
The text editor is the file written by Notepad or other editors under windows, and it runs in the cygwin simulation LINUX software environment.
Solutions:
The file format difference between DOS and Linux is caused.
The text file under DOS uses \r\n as the line break mark, expressed in hexadecimal is 0D 0A. The text file under Unix uses \n as a line break mark, which is 0A in hexadecimal.
[In windows, the two symbols used for line feed, carriage return \r, line feed symbol \n, under linux, only one symbol \n is enough .]
A text file in DOS format under Linux will display ^M at the end of the line when opened with a lower version of vi. Of course, you may not be able to see it, but in vi, the format of the file will be displayed below, “M.txt “[dos] 8L, 72C means a dos file format.
solution:
Use the following command to set the file format to unix format to solve the above error
vi test. sh :set fileformat = unix :wq
Why can’t the script edited under windows be directly copied to the unix system and run directly?
Linux executes SHELL script error “syntax error near unexpected token `in”
long long ago….. Old teletypewriters used two characters to start a new line. One character moves the carriage back to the first position (called carriage return, <CR>, ASCII code 0D), and another character moves the paper up one line (called line feed, <LF>, ASCII code 0A). When computers came out, memory used to be very expensive. Some people think it is unnecessary to use two characters to indicate the end of a line. Unix developers decided that they could use a character to indicate the end of a line, Linux followed Unix, and it was also <LF>. Apple developers specified the use of <CR>. The guys who developed MS-DOS and Windows decided to use the old <CR><LF>.
Because MS-DOS and Windows use carriage return + line feed to represent line feed, so under Linux, use Vim to view the code written in VC under Windows. The “^M” symbol at the end of the line indicates a symbol.
Read More:
- Running shell script reports an error: “syntax error near unexpected token solution ‘”
- Shell Script syntax error near unexpected token `done’
- [Solved] Linux shell Script Error: syntax error near unexpected token `do
- Ubuntu shell Script syntax error near unexpected token `$‘{\r‘‘
- [Solved] shell Error: Syntax error: “(“ unexpected (expecting “}“)
- Windows writes shell script and executes shell error [Solved]
- Ubuntu: How to deal with the fatal: the remote end hung up unexpected error of GIT clone Android kernel
- [Solved] bash: /etc/vimrc: line 15: syntax error near unexpected token `“autocmd“‘
- [Solved] Bash: Syntax error: redirection unexpected
- [Solved] ImportError:lib***.so–cannot open shared object file: No such…(pycharm/clion Error but shell No Error)
- [Solved] ubuntu Run jupyter Error: print(‘Error in generated code:‘, file=sys.stderr) SyntaxError: invalid syntax
- [Solved] yum command Error: “except keyboardinterrupt, e: syntax error: invalid syntax”“
- Shell: How to Get System Current Tme and Format it
- [Solved] Linux SSH Login Terminal Error: shell request failed on channel 0
- [Solved] Ubuntu tab Error: _complete:96: bad math expression: operand expected at end of string
- Centos7 Start Error: Entering emergency mode.Exit the shell to continue
- MAC Adb Shell Error: -bash: adb: command not found, adb
- Prompt “entering emergency mode. Exit the shell to continue” if the Linux operating system does not start normally
- [Solved] Module yaml error: Unexpected key in data: static_context
- [Solved] Ubuntu Server 18.4 System /etc/sudoers: syntax error near line 32