-1

I have created a linux bash script directory structure which is all working fine except from the date is not showing in the logfile which is created!

Here is the code for the logfile

touch $1/logfile.log
CURRENTDATE=`date +"%Y-%m-%d %T"`
printf "Created by: $USER\nDate/Time Created: $CURRENTDATE" > $1/logfile.log

The message from the command line I receive is: CURRENTDATE: Command Not Found, then when opening the logfile.log created the correct USER appears but there is no date.

Any Advise? Thanks

Jam12345
  • 133
  • 1
  • 1
  • 7
  • 1
    check your path for the date command. type ```which date``` from the cmdline to check. You may need to provide the path with your date command e.g. /bin/date – al76 May 20 '19 at 09:46
  • 2
    Welcome to Stackoverflow. There seems to be no direct issue with the presented code-block, however I would strongly recommend to follow up with http://www.shellcheck.net which will tell you various improvements you can make to these 3 lines. Especially follow up with : https://github.com/koalaman/shellcheck/wiki/SC2059 – kvantour May 20 '19 at 09:48
  • 1
    Also see [How to use Shellcheck](http://github.com/koalaman/shellcheck), [How to debug a bash script?](http://unix.stackexchange.com/q/155551/56041) (U&L.SE), [How to debug a bash script?](http://stackoverflow.com/q/951336/608639) (SO), [How to debug bash script?](http://askubuntu.com/q/21136) (AskU), [Debugging Bash scripts](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html), etc. – jww May 20 '19 at 09:52
  • @al76 Yes your right it’s /bin/date, would this mean CURRENTDATE = /bin/date ? – Jam12345 May 20 '19 at 09:53
  • @GrantJamieson I would try /bin/date – al76 May 20 '19 at 10:49
  • See [Why does a space in a variable assignment give an error in Bash?](https://stackoverflow.com/q/41748466/4154375). – pjh May 20 '19 at 17:43

2 Answers2

1

You should use "echo" not printf if you are using linux.

talha@DESKTOP-BCDU8TA:~$ touch logfile.log
talha@DESKTOP-BCDU8TA:~$ echo -e "Created by: $USER\nDate/Time Created:$CURRENTDATE" > logfile.log
talha@DESKTOP-BCDU8TA:~$ cat *log 
Created by: talha 
Date/Time Created: 2021-10-30 20:57:48

So as you can see on the code sample it worked.

FF1ZZL3
  • 19
  • 5
  • We usually avoid repeat answers on Stack Overflow. But if you are going to repeat previous guidance, you need to offer more detail and explanation—which is exactly what you’ve done here. I appreciate both the explanation and the sample output; that’s useful. – Jeremy Caney Oct 30 '21 at 18:05
0

try with echo command:

echo -e "Created by: $USER\nDate/Time Created: $CURRENTDATE" > $1/logfile.log
fauez
  • 19
  • 2