CRLF line terminators causes shell script errors: command not found, crlfterminators

Source: Internet
Author: User

CRLF line terminators causes shell script errors: command not found, crlfterminators

The line ending mark of Linux and Windows text files is different. In Linux, text files use "/n" to indicate line breaks, while Windows uses "/r/n" to indicate line breaks. Sometimes you need to pay attention to this when writing shell scripts in Windows. Otherwise, the shell script reports errors such as "No such file or directory" or "command not found line x". If you do not know the cause and effect, it will surely be quite depressing. Test. sh

[root@DB-Server myscript]# more test.sh 
. /home/oracle/.bash_profile
echo ' '
date
echo ' '
 
sqlplus test/test @/home/oracle/scripts/test.sql
 
echo ' '
date
echo ' '

Run test. the command "No such file or directory/home/oracle /. bash_profile "and" command not found line xx ". if you check the shell script syntax, there is no problem at all. I do not know the specific reason is definitely quite depressing.

[oracle@DB-Server myscript]$ ./test.sh
 
: No such file or directory /home/oracle/.bash_profile
 
: command not found line 2: 
 
: command not found line 4: date
 
: command not found line 6: 
 
: command not found line 7: 

If you use the file command to view test. sh, you will find that the file is encoded as ASCII, with CRLF line terminators. We can use cat-v test. sh to see ^ M (The hexadecimal System of \ r is 0D, and the corresponding controller is ^ M ). Cat-v can see non-printable characters in the file.

[root@DB-Server myscript]# 
[root@DB-Server myscript]# file test.sh 
test.sh: ASCII text, with CRLF line terminators
[root@DB-Server myscript]# cat -v test.sh 
. /home/oracle/.bash_profile^M
echo ' '^M
date^M
echo ' '^M
^M
sqlplus test/test @/home/oracle/scripts/test.sql^M
^M
echo ' '^M
date^M
echo ' '^M
[root@DB-Server myscript]# 

The vi command to view the test. sh file does not see ^ M. In fact, this is related to the vi version. In some versions, vi displays ^ M at the end of the line (^ M is displayed under AIX ). When some vi are opened, you can see the [dos] format prompts below.

We can use the file command to compare the format and encoding of normal shell scripts. As shown below

[oracle@DB-Server myscript]$ file myshell.sh
 
myshell.sh: Bourne-Again shell script text executable

It is not difficult to solve this problem. There are many methods, such as writing shell scripts directly using the vi editor; using the cat command to create shell scripts, and then copying the scripts from the Windows text tool; it is most convenient to use dos2unix to convert a DOS text file to a Unix or Linux format.

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.