在生成文件中使用$ RANDOM [英] Use $RANDOM in a makefile

查看:223
本文介绍了在生成文件中使用$ RANDOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个makefile以重命名其中带有随机数的文件(我是Shell脚本中的新手).我不明白为什么,但是当我运行文件$ rand时,它的值是'ANDOM'.当我在makefile之外运行它时,它会起作用.

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.

如果有帮助,我会在Mac os终端中运行它.

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?

对于要让shell看到的每个$,您需要在makefile中使用两个$符号.

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

因此:

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

这篇关于在生成文件中使用$ RANDOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆