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

我的梦境 提交于 2021-02-20 05:00:49

问题


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.


回答1:


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.



回答2:


If you escape the percent '%' in your date command with a second percent symbol '%%' as described at the following link, that should correct the behavior you're seeing with expanding %S to "OURCE" as you're seeing in your output.

    stamp=`date +%%Y%%m%%d%%H%%M%%S`

See section "Writing a Macro" here http://rpm.org/user_doc/macros.html



来源:https://stackoverflow.com/questions/40396945/date-command-is-giving-erroneous-output-while-using-inside-rpm-spec-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!