需要相当于 windows "echo %date% %time% %COMPUTERNAME%" 的 linux
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3788274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
need linux equivalent to windows "echo %date% %time% %COMPUTERNAME%"
提问by Captain Claptrap
In Linux,
在 Linux 中,
"echo %date% %time% %COMPUTERNAME%"
returns
返回
%date% %time% %COMPUTERNAME%
not
不是
Fri 09/24/2010 10:46:25.42 WXP2010043001
as Windows does. I need to be able to do this for the logs I'm setting up.
就像 Windows 一样。我需要能够为我正在设置的日志执行此操作。
采纳答案by mkb
Use the datecommand with a format like this:
使用具有如下格式的date命令:
date +"%m/%d/%Y %H:%M:%S $HOSTNAME"
To get hundredths of seconds, you may need to do some text processing like this:
要获得百分之几秒,您可能需要像这样进行一些文本处理:
DATE=date +'%m/%d/%Y %H:%M:%S.%N'
DATE=${DATE%???????}
DATE="$DATE $HOSTNAME"
This is because date offers seconds, nanoseconds, and nothing in between!
这是因为 date 提供秒、纳秒以及介于两者之间的任何内容!
回答by codaddict
You can do:
你可以做:
dt=$(date)
echo $dt $HOSTNAME
回答by Benoit
回答by jedi_coder
In Linux, there is the datecommand. If you don't like the default format, it can be modified. See the manpage of date
在 Linux 中,有date命令。如果您不喜欢默认格式,可以对其进行修改。请参阅日期的联机帮助页
For hostname, you can use hostnamecommand, or $HOSTNAMEenvironment variable, if it is set.
对于主机名,您可以使用主机名命令或$HOSTNAME环境变量(如果已设置)。
With system name, it is more complicated. You can use uname -a, sometimes it contains the OS name. Some distributions also have lsb-release, but not all of them.
有了系统名,就更复杂了。您可以使用uname -a,有时它包含操作系统名称。一些发行版也有lsb-release,但不是全部。
回答by Bob Sammers
Several people have provided answers based on date
, but your question requires the short day name (although my UK Win 7 installation doesn't provide this with the ECHO command you specified), which no one has (so far) addressed.
有几个人提供了基于 的答案date
,但是您的问题需要简短的日期名称(尽管我的 UK Win 7 安装没有通过您指定的 ECHO 命令提供此名称),但没有人(到目前为止)解决过这个问题。
To get this, you will probably want to include %a
in the format string:
为此,您可能希望%a
在格式字符串中包含:
date "+%a %m/%d/%Y %H:%M:%S $HOSTNAME"
回答by miniwark
it is also possible to use backtiks caracters for this:
也可以为此使用 backtiks 字符:
echo `date` `hostname`
or with (localised) date formating:
或使用(本地化)日期格式:
echo `date +"%a %x %X"` `hostname`
回答by Mark Casey
echo $(date '+%Y %b %d %H:%M') Your output $HOSTNAME
Outputs:
输出:
2013 Nov 01 09:11 Your output PEGASUS-SYDNEY-CL2