Makefile回声随机

时间:2017-05-19 01:18:47

标签: bash shell makefile gnu-make

我定义了一个函数来获得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

但是终点出现了。

正确的bash脚本

发表评论时,#!/bin/sh应更改为#!/bin/bash

#!/bin/bash
for i in $( seq 1 10 )
do
    echo ${RANDOM}
done

1 个答案:

答案 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 ""}'

如上所述重定向到文件。