Home >Operation and Maintenance >Linux Operation and Maintenance >What to do if Linux cannot execute sh script

What to do if Linux cannot execute sh script

WBOY
WBOYOriginal
2022-07-20 14:53:125204browse

There are three solutions: 1. Use the "dos2unix filename" command to directly convert the specified file into unix format; 2. Use the sed command to directly replace the ending character with unix format, and the syntax is "sed - i "s/\r//" filename" or "sed -i "s/^M//" filename"; 3. After using vi to open the file, use "set ff=unix" to set the file to unix, and use ": wq" and save it.

What to do if Linux cannot execute sh script

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

What should I do if Linux cannot execute the sh script?

The reason why such an error is prompted when executing the shell script is mainly because the shell script file is in dos format, that is, the end of each line is identified by \r\n. The end of the line in Unix format files is marked by \n.

Several ways to check whether a script file is in dos format or unix format.

(1) cat -A filename It can be judged from the display results that the file line ending in dos format is ^M, the file line ending in u n i x format is, and the file line ending in unix format is, unix format The file line ends with .

(2) od -t x1 filename If you see the characters 0d 0a in the output content, then the file is in dos format. If there is only 0a, it is in unix format.

(3) Open the file with vi filename and execute: set ff. If the file is in dos format, it will be displayed as fileformat=dos. If it is unxi, it will be displayed as fileformat=unix.

Solution:

(1) Use the linux command dos2unix filename to directly convert the file to unix format

dos2unix filename

(2) Use the sed command sed -i "s/\r//" filename or sed -i "s/^M//" filename directly replaces the ending character with the unix format

sed -i “s/\r//” filename 
sed -i “s/^M//” filename

(3) vi filename opens the file and executes: set ff=unix Set the file to unix, then execute: wq and save it in unix format

set ff=unix

Recommended learning: Linux video tutorial

The above is the detailed content of What to do if Linux cannot execute sh script. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn