问题描述
如何生成此code随机数?
我尝试使用$ RANDOM,但数量仍然是相同的。如何提高呢?
找到。型的F -exec的sed -i的/<字段名= \\测试/<字段名= \\试$ RANDOM / G'{} \\;
这是因为,当您运行<$ c中的 $ RANDOM
函数调用与它的结果取代$ C>找到命令,而不是当找到
运行 SED
命令。
您可以做的是把 SED -i ...
部分的脚本,并通过此脚本找到
代替。
例如,如果 subst.sh
包含:
#!/斌/庆典
R = $ RANDOM
SED -iS /&LT;字段名= \\测试/&LT;字段名= \\试$ {R} / G$ @
然后
找到。型的F -exec ./subst.sh {} \\;
应该做的伎俩,因为 $ RANDOM
将被评估为有文件多次。
(注意,随机数还是将跨越一个特定的文件是相同的,但是这将是明显的为不同的文件。)
how to generate random number in this code?I try use $RANDOM, but the number is still the same. How to improve it?
find . -type f -exec sed -i 's/<field name=\"test/<field name=\"test$RANDOM/g' {} \;
That happens because the $RANDOM
function invocation is substituted with its result when you run the find
command, not when find
runs the sed
command.
What you can do is put the sed -i ...
part in a script, and pass this script to find
instead.
For instance, if subst.sh
contains:
#!/bin/bash
r=$RANDOM
sed -i "s/<field name=\"test/<field name=\"test${r}/g" $@
Then
find . -type f -exec ./subst.sh {} \;
should do the trick, because $RANDOM
is going to be evaluated as many times as there are files.
(Note that the random number will still be the same across one particular file, but it will be distinct for different files.)
这篇关于如何生成使用find和sed的随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!