One line command / usr / bin / Perl ^ m: bad interpreter

Knowledge map advanced must read: read how large-scale map data efficient storage and retrieval>>>

When using Perl script in * Nix system, we sometimes encounter the following errors:

/usr/bin/perl^M:badinterpreter:Nosuchfileordirectory

The most common reason is that the script is edited in Windows system. The newline character in Windows system is “R” while it is only “n” in UNIX system. If you want to solve this problem, just remove the

The first solution is to use sed (assuming that the script in question is named file name)

sed-i's/\r$//'filename

This solution is suitable for simple ASCII file format. If the situation is more complicated, for example, if the file name is a Unicode file, some new line breaks in Unicode may be introduced; In addition, sometimes the character codes of the two systems are not consistent. The vertical tab character 0x0b and the paper feed character 0x0c are even more troublesome. Fortunately, Perl provides another solution:

perl-p-i-e"s/\R/\n/g"filename

Here, we use the character group “R” introduced from Perl 5.10 to match various newline characters. We just need to replace it with “n”. At the same time, there is no need to use a script file to implement, just execute such a command in the shell. Where – P means to operate on the file name line by line, – I means to operate in place, to cover the original file, – e means to execute the following statements

Similar Posts: