我定义了一个函数来获得10个随机数,其值为[0..9]。
define write_file
for i in $$( seq 1 10 );\
do\
RANDOM := $(shell bash -c 'echo $$RANDOM')%10;\
echo -n $$RANDOM >> test.txt;\
done
endef
但是当我打电话给它时,输出是:
for i in $( seq 1 10 ); do RANDOM := 25401%10; echo $RANDOM; done
/bin/sh: 1: RANDOM: not found
/bin/sh: 1: RANDOM: not found
/bin/sh: 1: RANDOM: not found
/bin/sh: 1: RANDOM: not found
/bin/sh: 1: RANDOM: not found
/bin/sh: 1: RANDOM: not found
/bin/sh: 1: RANDOM: not found
/bin/sh: 1: RANDOM: not found
/bin/sh: 1: RANDOM: not found
/bin/sh: 1: RANDOM: not found
我也尝试使用echo $RANDOM
而不是echo $$RANDOM
,但它仍无效。
我也尝试在bash shell脚本中使用echo RANDOM。
#!/bin/sh
for i in $( seq 1 10 )
do
echo ${RANDOM}
done
但是终点出现了。
发表评论时,#!/bin/sh
应更改为#!/bin/bash
。
#!/bin/bash
for i in $( seq 1 10 )
do
echo ${RANDOM}
done
答案 0 :(得分:0)
awk
救援!
seq 10 | awk 'BEGIN{srand()} {print(int(rand()*10))}' > test.txt
或
awk -v n=10 'BEGIN{srand(); for(i=1;i<=n;i++) print(int(rand()*10))}' > test.txt
要使数字相邻,你可以这样做
awk -v n=10 'BEGIN{srand();
for(i=1;i<=n;i++)
printf "%d",(int(rand()*10));
print ""}'
如上所述重定向到文件。