在生成文件中使用$ RANDOM

编程入门 行业动态 更新时间:2024-10-24 01:57:09
本文介绍了在生成文件中使用$ 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

推荐答案

  • 使用日期/时间戳记以便按日期顺序列出重命名的文件会更容易/更好吗?

  • 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

    本文发布于:2023-11-30 00:41:05,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1648152.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:文件   RANDOM

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!