Search code examples
linuxrpmspecificationsrpmbuildrpm-spec

date command is giving erroneous output while using inside rpm spec file


I have to perform some necessary steps before installing my package, such as taking back up of previous datastore snapshot. For that purpose I'm using a %pre script as follows.

%pre
#!/bin/sh
--------
--------
stamp=`date +%Y%m%d%H%M%S`
echo ${stamp}
-------------
-------------

The output is as follows: 20161103123325OURCE It is printing some random characters along with date. "OURCE" is not present anywhere in my spec file.

The same script works perfectly as standalone. The platform is CentOS7.


Solution

  • rpmbuild knows a whole set of macros. Apparently a certain macro is defined as:

    %S = %SOURCE
    

    I didn't manage to find something that tells rpmbuild not to expand that macro; but there is a way in tricking him not to do so. I know this is a little workaround, but it's the best I could come up with:

    stamp=$(date '+%Y%m%d%H%M%''S')
    
    • note that I replaced the backticks with the recommanded $() invocation
    • I just inserted two '' to split the string in two parts; this avoids macro replacement.