Problems with garbled shell script editing

Source: Internet
Author: User
Tags ini printable characters sqlplus

File command to view the properties of *.sh ASCII text with the CRLF line terminators


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

. /home/oracle/.bash_profile
Echo '
Date
Echo '
Sqlplus test/test @/home/oracle/scripts/test.sql
Echo '
Date
Echo '

"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.

[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 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.

Test.sh:ASCII text, with CRLF line terminators
. /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]#

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

[Oracle@db-server myscript]$ file myshell.sh
Myshell.sh:bourne-again Shell 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.

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.