我正在制作一个 makefile 来重命名其中包含随机数的文件(我是 shell 脚本的新手)。我不明白为什么,但是当我运行文件 $rand 时,它的值被赋予了“ANDOM”。当我在 makefile 之外运行它时,它可以工作。
我在 Mac os 终端中运行它,以防它有帮助。
all: renamefiles
renamefiles:
rand=$RANDOM && mv myfile.css $rand-myfile.css && mv myotherfile.css $rand-myotherfile.css
最佳答案
$
签到 makefile
每个$
你想让 shell 看到。 因此:
all: renamefiles
renamefiles:
rand=$$RANDOM && \
mv myfile.css $$rand-myfile.css && \
mv myotherfile.css $$rand-myotherfile.css
或者,使用日期/时间戳:
all: renamefiles
renamefiles:
time=$$(date +'%Y%m%d-%H%M%S') && \
mv myfile.css $$time-myfile.css && \
mv myotherfile.css $$time-myotherfile.css
关于shell - 在 makefile 中使用 $RANDOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14408648/