如何创建100个文件,每个文件中有一个随机数,并根据该数授予它们权限



我需要创建一个bash脚本,该脚本可以创建100个带有随机数的文件。我试着用

for i in {1..100}; do $RANDOM > RANDOM.txt

我不知道这样做是否正确。然后我需要根据文件中的数字授予文件读写和执行权限。不知道怎么做。我尝试使用:

if [ $i%2==0 ]
then
echo chmod +rw $RANDOM.txt

但似乎不起作用

刚得到一些反馈,结果发现我做错了一切。我必须创建100个文件1.txt到100.txt我使用触摸{1..100}.txt,然后在每个文件中粘贴1个随机数。我应该使用echo还是shuf来执行此操作?

我认为使用具有八进制权限的chmod是最简单的,就像rwxrwxrwx等使用0777一样。

示例:

#!/bin/bash
for((i=0; i<100; ++i)) {
rnd=$RANDOM                     # pick a random number
(( perm = rnd % 512 ))          # make it in the range [0, 512) (0000-0777 oct)
printf -v octperm "%04o" $perm  # convert to octal
file=$rnd.txt                   # not sure if you meant to name it after the number
echo $rnd > $file
chmod $octperm $file            # chmod with octal number
}

文件摘录:

-r-xrw--wx 1 ted users   5 15 dec 17.53 6515.txt
---x-wxrwx 1 ted users   5 15 dec 17.53 6751.txt
-rwx-w--w- 1 ted users   5 15 dec 17.53 8146.txt
-rw-r----- 1 ted users   5 15 dec 17.53 8608.txt
--w--w---x 1 ted users   5 15 dec 17.53 8849.txt
--wx----wx 1 ted users   5 15 dec 17.53 8899.txt
--wxrwx-wx 1 ted users   5 15 dec 17.53 8955.txt
-rw-r-xrw- 1 ted users   5 15 dec 17.53 9134.txt
...

如果您想考虑当前的umask,您也可以这样做,通过屏蔽umask指示的权限中的位。

#!/bin/bash
(( um = ~$(umask) ))  # bitwise negated umask
for((i=0; i<100; ++i)) {
rnd=$RANDOM
(( perm = (rnd % 01000) & um ))    # [0000,0777] bitwise AND umask
printf -v octperm "%04o" $perm
file=$i.$rnd.txt                   # using $i. to make name unique
echo $rnd > $file
chmod $octperm $file
}

如果您的umask当前为0022,则上面的示例将不会为和/或其他创建任何可写入的文件,而其他(正常(权限将是随机的。

首先,您需要回显随机数,而不是将其用作命令。

其次,如果要使用与文件名和内容相同的随机数,则需要将其保存到变量中。否则,每次写$RANDOM时都会得到不同的数字。

第三,[]中的算术和条件不是这样做的,任何shell脚本教程都应该显示正确的方法。您还可以将bash算术表达式与(( expression ))一起使用。

#!/bin/bash
for i in {1..100}
do
r=$RANDOM
echo "$r" > "$r.txt"
if (( i % 2 == 0 ))
then
chmod +rw "$r.txt"
fi
done

来自Ted Lyngmo的回答

对于一些抨击,比如使用整数变量属性避免分叉。。。

declare -i um=" ~$(umask) " i rnd perm
for((i=100;i--;)){
rnd=RANDOM
perm=' ( rnd % 01000 ) & um '
printf -v file file-%03d-%04X.txt $i $rnd
printf -v octperm "%04o" "$perm"
echo "$rnd" > "$file"
chmod "$octperm" "$file"
}

(文件名是用十进制的文件号和十六进制的随机数构建的(

关于表演

也许会快一点,因为避免了分叉和使用整数。

(这里使用的for((;;)){ ;}语法不是更快,只是不同(更短(
事实上,for ((i=100;i--;)) ;do ...;done(不知不觉(比for i in {1..100};do ...;done慢!我只是想用这种不寻常的语法来形容极端抨击…;(

一些比较:

export TIMEFORMAT=$'(%U + %S) / e[1m%Re[0m : %P'

关于fork,尝试使用printf:进行1'000变量赋值以进行格式化

time for i in {1..1000};do var=$(printf "foo");done 
(0.773 + 0.347) / 1.058 : 105.93
time for i in {1..1000};do printf -v var "foo";done 
(0.006 + 0.000) / 0.006 : 99.80

在我的主机上从1.5秒到6毫秒!!!没有讨论:要避免fork(语法$(printf...)(!!

关于整数属性(使用100'000二进制运算(:

declare -i intvar
time for i in {1..100000};do var=$(( ( 431214 % 01000 ) & -19 ));done
(0.272 + 0.005) / 0.278 : 99.46  
time for i in {1..100000};do intvar=' ( 431214 % 01000 ) & -19 ';done 
(0.209 + 0.000) / 0.209 : 99.87  

从0,28秒到0.21秒,这并不显著,但是。

关于for i in {for ((i=(现在使用1'00'000个循环(:

time for i in {1..1000000};do :;done
(1.600 + 0.000) / 1.602 : 99.86
time for ((i=1000000;i--;));do :;done
(1.880 + 0.001) / 1.882 : 99.95

但这显然没有那么重要(注意内存消耗,使用大括号(。

使用awk,您可以尝试以下操作一次。这个程序还负责关闭后端打开的文件(以防我们一次打开的文件太多(。用GNUawk编写和测试。

awk -v numberOfFiles="100" '
BEGIN{
srand()
for(i=1;i<=numberOfFiles;i++){
out=(int(1 + rand() * 10000))
print out > (out".txt")
system("chmod +rw " out".txt")
close(out)
}
}'

我已经创建了一个名为numberOfFilesawk变量,其中我给出了100(根据创建100个文件的需要(,您也可以根据需要保留它,将来如果您需要更改它,并且我们不需要在这里更改程序的其余部分中的任何内容。

最新更新