Time and Date

Hi! I have a sh file and a log file and I need to add the time and date of my computer to the log file every time I execute the sh file.

I found this code:

echo %time% %date% >> file.log

but it doesn't work.

What do I do?

Hello demas,

As a starting point you could do as follows into your script.

cat script.ksh    ####Where cat command is for reading a file, so here I am letting you know your script's content which are as follows. 
                     You could create this script.ksh by vi/nano etc editors by putting following command into it and saving it then with proper permissions then.
date >> file.txt

If you have more requirements then request you to please be more clear in them and let us know more detailed request, I hope this helps.

Thanks,
R. Singh

1 Like

Thank you

What I'm asking is: when I execute the sh file which associates a person with a tv channel it writes in the log file something like this:

2016-09-26 14:00:01 Mary CBS

Hello Demas,

Sorry, I didn't get it properly. So you need the date and time of a O.S(*NIX etc) with the user information whoever has logged into your box/server? If this is the case then following script could help you in same.

cat script.sh
DATE=`date`
WHOAMI=`users`
echo $DATE $WHOAMI > file

If above is the not meeting your requirements then could you please explain your requirement in more details.

Thanks,
R. Singh

1 Like

Sorry, I'm not explaining myself properly

I have a sh file with this:

echo "Name user:"
                read user
            echo "Name channel:"
                read channel
                    if [[ $(grep -c $user registrations.txt | grep $channel registrations.txt) -eq 0 ]]; then
                        echo date and time  $user $channel >> registrations.txt
                else
                    "Association already exists"
                fi;;

When I execute it I need it to look like this in the registrations.txt file

I think this is Microsoft speak.

date >> file.log

should come close.

1 Like

Hello Demas,

I am not sure what you are actually trying to do with following condition.

if [[ $(grep -c $user registrations.txt | grep $channel registrations.txt) -eq 0 ]];

Because what | (pipe) does is, it gets the output from previous command and push it as standard input for the next command, so in above condition you are trying to check the search count of variable named user 's value into file named registrations.txt and then whatever count(number) comes you are then piping it to next command but here again grep is searching/looking for variable named channel in file registrations.txt and then you are comparing it's value to 0.

IMHO it doesn't look correct. So could you please let us know sample Input_file and expected output with all conditions so that we could try to help you in solving this problem.

Thanks,
R. Singh

Moderator comments were removed during original forum migration.