myapp_monday.logHow do I create log filename with day of the week in it under Linux or Unix operating systems?
myapp_tuesday.log
....
...
myapp_sunday.log
You can use the date command to display current date and time. You can format a date provide a string beginning with + to get day of the week as follows:
$ date +"%A"
Where,
- +"%A" - Get weekday in full format i.e. as Tuesday
- +"%a" - Get weekday in abbreviated format i.e. as Tue
- +"%u" - Get day of week starting with Monday (1), i.e. mtwtfss
- +"%w" - Get day of week starting with Sunday (0), i.e. smtwtfs
To create a filename with day of the week in it:
_dow="$(date +'%A')"
echo "$_dow"
## Get day of week starting with Monday (1), i.e. mtwtfss (see above for syntax) ##
_dow="$(date +'%u')"
echo "$_dow"
Sample outputs:
#!/bin/bash
_dow="$(date +'%A')"
_log="myapp_${_dow}.log"
echo "My log filename: ${_log}"
My log filename: myapp_Wednesday.log
No comments:
Post a Comment