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