CRLF line terminators lead to Shell foot report error: Command not found

Source: Internet
Author: User
Tags printable characters

The line end flags for Linux and Windows text files are different. In Linux, the text file uses "/n" for carriage return, while Windows uses "/r/n" to indicate carriage return line breaks. Sometimes you need to be aware of this when writing shell scripts in Windows, otherwise the shell script will report "No such file or directory" or "command not found line X" error, if you do not know the cause and consequences, Will surely be quite depressed by this toss. As shown below test.sh

[[email protected] myscript]
. /home/oracle/.bash_profile
‘ ‘
Date
‘ ‘
Sqlplus test/test @/home/oracle/scripts/test.sql
‘ ‘
Date
‘ ‘

"No such file or directory/home/oracle/.bash_profile" and "Command not found line xx" are reported during test.sh script execution. If you check the syntax of the shell script, there is no problem at all. I'm sure it's pretty depressing to know for certain reasons.

[Email protected] myscript]$./test.sh
: No such file or Directory/home/oracle/.bash_profile
: Command not found line 4:date

If you use the file command to view the test.sh, you will find the files encoded as Ascii,with CRLF line Terminators. We can see ^m with cat-v test.sh (\ r's hex is 0 d and the corresponding control is ^m). Cat-v can see non-printable characters in the file.

[[email protected] myscript]
[[email protected] myscript]
Test.sh:ASCII text, with CRLF line terminators
[Email Protected]db-server MyScript]
. /home/oracle/.bash_profile^m
"'^m
Date^m
"'^m
^m
Sqlplus test/test @/home/oracle/scripts/test.sql^m
^m
"'^m
Date^m
"'^m

VI command to view the test.sh file can not see ^m, in fact, this is related to the version of VI. Some version vi shows that the end of the line is ^m (Aix is displayed under ^m). Some vi open can see the "DOS" in the bottom of the format hint.

We can compare the format and encoding of the normal shell script with the file command. As shown below

[[Email protected] myscript]$ file myshell.sh
Script Text executable

There are many ways to solve this problem, such as writing shell scripts directly using the VI editor, creating shell scripts with the cat command, and copying scripts from the Windows Text tool. The most convenient thing is to use Dos2unix to convert the DOS format text file to UNIX format or to the Linux format.

CRLF line terminators lead to Shell foot report error: Command not found

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.