Use $RANDOM in a makefile

别说谁变了你拦得住时间么 提交于 2019-12-10 13:46:21

问题


I am making a makefile to rename files with a random number in it (I am a newbie in shell script). I don't understand why, but when I run the file $rand is given the value 'ANDOM'. When I run this outside of the makefile it works.

I run this in the Mac os terminal, in case it's helpful.

all: renamefiles

renamefiles:
    rand=$RANDOM && mv myfile.css $rand-myfile.css && mv myotherfile.css $rand-myotherfile.css

回答1:


  1. Wouldn't it be easier/better to use a date/time stamp so that the renamed files are listed in date order?

  2. You need to use two $ signs in the makefile for each $ that you want the shell to see.

Thus:

all: renamefiles

renamefiles:
    rand=$$RANDOM && \
    mv myfile.css      $$rand-myfile.css && \
    mv myotherfile.css $$rand-myotherfile.css

Or, with date/time stamps:

all: renamefiles

renamefiles:
    time=$$(date +'%Y%m%d-%H%M%S') && \
    mv myfile.css      $$time-myfile.css && \
    mv myotherfile.css $$time-myotherfile.css



回答2:


You might need to surround a multi-letter macro name with braces (or parentheses), for example

${RANDOM}
$(RANDOM)

ref




回答3:


To use a random number within one or multiple make variables, the following works fine for me:

FOO="some string with \"$$rand\" in it"
BAR=" you may use it $$rand times."
foobar:
    rand=$$$$ && \
    echo $(FOO) $(BAR)


来源:https://stackoverflow.com/questions/14408648/use-random-in-a-makefile

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