views:

75

answers:

5

In 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.

+2  A: 

You can do:

dt=$(date)
echo $dt $HOSTNAME
codaddict
+4  A: 

Use the date command with a format like this:

date +"%m/%d/%Y %H:%M:%S $HOSTNAME"

To get hundreds 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!

Matt Kane
exactly what I needed. Thanks!
Captain Claptrap
@micah: If this answer solved it for you, accept it by hitting the check mark to the left
Daenyth
+2  A: 

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)

Benoit
stack overflow makes me lazy!
Captain Claptrap
+1  A: 

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.

jedi_coder
+1  A: 

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"
Bob Sammers