Search code examples
bashdateprocmail

Setting the current date into a variable in a Script in bash


So for the life of me I cannot figure out why my script will not take my date command as a variable. I have a script that is run every time a message is received and procmail filters specific messages by their subject line. The script looks like this:

d=$(date +%Y%m%d)

:0 wc
* ^(From|subject).*xxx
| cat&>/xx/xx/xx/xx/MSG:$d && \
chmod xxx /xx/xx/xx/xx/MSG:$d && \
/xx/xx/xx/otherscript.sh /xx/xx/xx/xx/MSG:$d

I have run the date command plenty of times in other scripts and to stdout without any issue, so I am wondering if this is a procmail issue? I have looked at a few different sites about this but still have not found a solution. My end goal is to create unique file names as well as for organization purposes each time a new email comes in.

The other reason for me believing it has something to do with procmail is that it was working fine just 3 months ago (didn't change any files or permissions). I have even tried several variations (only showing a few examples):

 $'date +"%Y%m%d"'
 $(date)
 echo $(date)

I get a variety of files created ranging with it printing MSG:(date), MSG:(date ,etc. MSG:(date appears to like it tries to read the variable but is getting cut off or the space between date and + is causing an issue.

And at the end of my script I send it to another script which also creates a new file with the date appended and it works just fine:

fileOut="/xxx/xxx/xxx/xxx.$v.$(date +"%Y%m%d-%H%M%S").xxx"
prints: xxx.YH8AcFV9.20160628-090506.txt

Thanks for your time :-)


Solution

  • Procmail does not support the modern POSIX shell command substitution syntax; you need to use backticks.

    d=`date +%Y%m%d`  # or just date +%F
    

    If you want to avoid invoking an external process, the From_ pseudo-header contains a fresh date stamp on many architectures.