perl cron 脚本中不需要的 \r
我一直在互联网的边缘和支持热线试图解决这个问题,但没有成功:
我正在尝试设置一个 cron 作业,以便我的 RackSpace Cloud 站点得到备份。有很多例子可以使用,但我似乎无法让它们中的任何一个工作,因为它将 '\r' 附加到我所有行的末尾。我尝试过几个编辑器,例如记事本和记事本++,但没有任何变化。这是我的 backup.sh 的示例:
#!/bin/sh
webroot='/mnt/example/123456/www.mysite.com/web/content/'
backuproot='/mnt/example/123456/www.mysite.com/backup'
###################
# Backup Database #
###################
mysqldump -h my.database.host -u my_username -p'my_password' my_database_name > $backuproot/db_backup.sql
###############
# Backup Site #
###############
tar -czpvf $backuproot/site_backup.tar.gz $webroot
我收到的错误示例:
tar: /mnt/example/123456/www.mysite.com/web/content/\r\r: Cannot stat: No such file or directory
tar: /mnt/example/123456/www.mysite.com/backup\r/site_backup.tar.gz: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error exit delayed from previous errors
出于某种原因,脚本将 '\r' 视为命令的一部分,但我不确定如何避免这种情况。任何帮助将不胜感激。
I've been to the edge of the internet and on support lines trying to figure this out to no avail:
I'm trying to set up a cron job so my RackSpace Cloud site gets backup up. There are plenty of examples to use but I can't seem to get any of them to work because it appends '\r' to the end of all my lines. I've tried a couple of editors like notepad and notepad++ but nothing changes. Here is an example of my backup.sh:
#!/bin/sh
webroot='/mnt/example/123456/www.mysite.com/web/content/'
backuproot='/mnt/example/123456/www.mysite.com/backup'
###################
# Backup Database #
###################
mysqldump -h my.database.host -u my_username -p'my_password' my_database_name > $backuproot/db_backup.sql
###############
# Backup Site #
###############
tar -czpvf $backuproot/site_backup.tar.gz $webroot
An example of the errors I get:
tar: /mnt/example/123456/www.mysite.com/web/content/\r\r: Cannot stat: No such file or directory
tar: /mnt/example/123456/www.mysite.com/backup\r/site_backup.tar.gz: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error exit delayed from previous errors
For some reason the script is considering the '\r' as part of the command and I'm not sure how to avoid that. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有什么特别的原因将其标记为 Perl 吗?
无论如何,我猜测您正在 Windows 上进行编辑,然后尝试在 Linux 计算机上执行。命令
dos2unix
应该转换行结尾,这样就不会出现此问题。Any reason in particular this is tagged Perl?
At any rate, I'm guessing that you're editing on Windows, then trying to execute on a Linux machine. The command
dos2unix
should convert line endings so that you don't have this problem.Windows 上有一些编辑器可以处理 Linux 风格的行结尾,例如 EditPad。
There are editors for Windows which can handle Linux-style line endings, eg EditPad.
问题出在文件中的行结尾。
在 Windows 下,文件中的行以字符 CR+LF (\r\n) 结束。在unix下,不使用回车符,因此它们仅以LF(\n)结尾。
您需要更改正在使用的行结尾类型 - 使用 Notepad++,您可以通过“编辑”菜单更改文件的格式(查找 Unix 格式、Unix 行结尾等内容)。
如果您想了解有关行结尾的所有信息,请参阅 http://en.wikipedia.org/wiki/Newline< /a>.
The problem is with the line endings in your file.
Under Windows, lines in a file are ended with the characters CR+LF (\r\n). Under unix, the carriage return isn't used, so they just end with the LF (\n).
You'll need to change the type of line ending you're using - with Notepad++, you can change the format of the file via the Edit menu (look for something like Unix Format, Unix line endings, etc).
If you want to learn ALL about line endings, see http://en.wikipedia.org/wiki/Newline.