在BASH中使用mv向文件名添加时间戳 [英] Adding timestamp to a filename with mv in BASH

查看:553
本文介绍了在BASH中使用mv向文件名添加时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我是linux新手,我在使用简单的bash脚本时遇到了问题.

Well, I'm a linux newbie, and I'm having an issue with a simple bash script.

我有一个程序可以在运行时添加到日志文件中.随着时间的流逝,该日志文件将变得越来越大.我想创建一个启动脚本,该脚本将在每次运行前重命名并移动日志文件,从而为程序的每次运行有效地创建单独的日志文件.这是到目前为止我得到的:

I've got a program that adds to a log file while it's running. Over time that log file gets huge. I'd like to create a startup script which will rename and move the log file before each run, effectively creating separate log files for each run of the program. Here's what I've got so far:

pastebin

DATE=$(date +"%Y%m%d%H%M")
mv server.log logs/$DATE.log
echo program

运行时,我看到以下内容:

When run, I see this:

: command not found
program

当我进入日志目录并运行dir时,会看到以下内容:

When I cd to the logs directory and run dir, I see this:

201111211437\r.log\r

这是怎么回事?我以为我缺少一些语法问题,但似乎无法弄清楚.

What's going on? I'm assuming there's some syntax issue I'm missing, but I can't seem to figure it out.

更新:由于下面Shellter的评论,我发现问题是由于我正在Windows的Notepad ++中编辑.sh文件,然后通过ftp发送到服务器,然后在其中运行通过ssh文件.在文件上运行dos2unix后,它可以工作.

UPDATE: Thanks to shellter's comment below, I've found the problem to be due to the fact that I'm editing the .sh file in Notepad++ in windows, and then sending via ftp to the server, where I run the file via ssh. After running dos2unix on the file, it works.

新问题:首先如何正确保存文件,以避免每次重新发送文件时都必须执行此修复程序?

New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?

推荐答案

您从脚本中发布的几行对我来说还不错.可能更深一些.

The few lines you posted from your script look okay to me. It's probably something a bit deeper.

您需要找到哪一行导致了此错误.将set -xv添加到脚本的顶部.这将打印出行号和正在执行的 STDERR 命令.这将帮助您确定在脚本中哪里出现此特定错误.

You need to find which line is giving you this error. Add set -xv to the top of your script. This will print out the line number and the command that's being executed to STDERR. This will help you identify where in your script you're getting this particular error.

顺便说一句,脚本顶部是否有 shebang ?当我看到这样的东西时,通常我希望它与Shebang有关.例如,如果顶部有#! /bin/bash,但是bash解释器位于/usr/bin/bash中,则会看到此错误.

BTW, do you have a shebang at the top of your script? When I see something like this, I normally expect its an issue with the Shebang. For example, if you had #! /bin/bash on top, but your bash interpreter is located in /usr/bin/bash, you'll see this error.

新问题:首先如何正确保存文件,以避免每次重新发送文件时都必须执行此修复程序?

New question: How can I save the file correctly in the first place, to avoid having to perform this fix every time I resend the file?

两种方式:

  1. 编辑文件时,选择 Edit-> EOL Conversion-> Unix Format 菜单项.一旦具有正确的行尾,Notepad ++就会保留它们.
  2. 要确保所有新文件都具有正确的行尾,请转到"设置->首选项"菜单项,并弹出"首选项"对话框.选择新文档/默认目录标签.在新文档格式下,选择 Unix 单选按钮.点击关闭按钮.
  1. Select the Edit->EOL Conversion->Unix Format menu item when you edit a file. Once it has the correct line endings, Notepad++ will keep them.
  2. To make sure all new files have the correct line endings, go to the Settings->Preferences menu item, and pull up the Preferences dialog box. Select the New Document/Default Directory tab. Under New Document and Format, select the Unix radio button. Click the Close button.

这篇关于在BASH中使用mv向文件名添加时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆