need linux equivalent to windows "echo %date% %time% %COMPUTERNAME%"
In Linux,
"echo %date% %time% %COMPUTERNAME%"
returns
%date% %time% %COMPUTERNAME%
not
Fri 09/24/2010 10:46:25.42 WX开发者_JAVA百科P2010043001
as Windows does. I need to be able to do this for the logs I'm setting up.
Use the date command with a format like this:
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!
You can do:
dt=$(date)
echo $dt $HOSTNAME
echo $(date '+%Y %b %d %H:%M') Your output $HOSTNAME
Outputs:
2013 Nov 01 09:11 Your output PEGASUS-SYDNEY-CL2
it is also possible to use backtiks caracters for this:
echo `date` `hostname`
or with (localised) date formating:
echo `date +"%a %x %X"` `hostname`
As a complement: percentage character is not used to reference variables on any Linux shell. You should use the dollar sign for this.
You should probably read an introduction to Bash (here)
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.
To get this, you will probably want to include %a
in the format string:
date "+%a %m/%d/%Y %H:%M:%S $HOSTNAME"
In Linux, there is the date command. If you don't like the default format, it can be modified. See the manpage of date
For hostname, you can use hostname command, or $HOSTNAME environment variable, if it is set.
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.
精彩评论