tru*_*phs -1 shell shell-scripting
试图执行这个 shell 脚本。
#!/bin/bash
# Proper header for a Bash script.
# Cleanup, version 2
# Run as root, of course.
# Insert code here to print error message and exit if not root.
LOG_DIR=/var/log
# Variables are better than hard-coded values.
cd $LOG_DIR
cat /dev/null > messages
cat /dev/null > wtmp
echo "Logs cleaned up."
exit # The right and proper method of "exiting" from a script.
# A bare "exit" (no parameter) returns the exit status
#+ of the preceding command.
~
Run Code Online (Sandbox Code Playgroud)
但是收到这条消息
[root@localhost ~]# ./clean.sh
-bash: ./clean.sh: /bin/bash^M: bad interpreter: No such file or directory
Run Code Online (Sandbox Code Playgroud)
知道发生了什么吗?
你的脚本有 DOS 行结尾。在编辑器中或使用 dos2unix、recode 等工具将它们转换为 Linux 行尾。
DOS/Windows 通常以 CR+LF 结束行,而 Linux 只使用 LF。shell 不知道如何利用额外的 CR 字符并将其显示为^M
.