如何使用脚本创建多个文件(100 个文件)并且每个文件可以包含 30 个随机字符

如何使用脚本创建多个文件(100 个文件)并且每个文件可以包含 30 个随机字符

我需要使用 Linux 脚本创建 100 个文件,其中包含 30 个随机字符密码,并且该密码仅包含字符串、小写字母和大写字母。每个文件的名称均为“Business.txt”

#!/bin/bash
for n in {1..100}; do
{ < /dev/urandom tr -dc A-Za-z0-9 | head -c${1:-30};echo; } > /mnt/mymnt/passwords/$n done

答案1

当实际上只需要一次打开文件一百次时,这并不是一个好习惯,那么如何:

#!/bin/bash
random=$(</dev/urandom tr -dc A-Za-z0-9 | head -c3000)
for i in {1..100}; do
  echo ${random:(i-1)*30:30} >/mnt/mymnt/passwords/Business$i.txt
done

A-Za-z0-9这首先保存与变量匹配的 3000 个随机字符$random,然后循环遍历数字 1-100,切出 30 个字符的部分并保存它们。

答案2

在写这个答案的时候,我以为您可以通过以下方式获得您想要的内容,但目前尚不清楚您想如何存储密码。

  1. 将目录更改为您想要写入文件的位置Business.txt

  2. 测试您是否可以手动创建文件,

    echo 'testing' > Business.txt
    cat Business.txt
    
  3. 如果需要,修改所有权和权限。方法取决于文件系统。

  4. 创建密码,

    for n in {1..100}; do { < /dev/urandom tr -dc A-Za-z0-9 | head -c${1:-30};echo; } ;done > Business.txt
    
  5. 检查密码Business.txt

    $ cat Business.txt 
    7rmJFCq2CZ9azpuxywFLwbjhmL2dD4
    dtLSVAEtDmyLUglkYFgUeGc9PDKBPb
    E3bnJ8WF4qoyS1Tokp6reAcpIkuLUt
    Y5whhtbJn1KfAccp85547gNDji2xLY
    ...
    

    应该有 100 行

    $ wc -l Business.txt 
    100 Business.txt
    

编辑:

您需要 100 个具有不同名称的文件(每个文件一个密码)。因此,可以根据以下列表修改步骤。

2.1.删除测试文件。

    rm Business.txt
  1. 修改的命令行:在循环内写入目标文件。

    for n in {1..100}; do { < /dev/urandom tr -dc A-Za-z0-9 | head -c${1:-30};echo; } > Business"$n".txt; done
    
  2. 检查文件是否已写入

    $ ls
    Business100.txt  Business28.txt  Business46.txt  Business64.txt  Business82.txt
    Business10.txt   Business29.txt  Business47.txt  Business65.txt  Business83.txt
    Business11.txt   Business2.txt   Business48.txt  Business66.txt  Business84.txt
    Business12.txt   Business30.txt  Business49.txt  Business67.txt  Business85.txt
    Business13.txt   Business31.txt  Business4.txt   Business68.txt  Business86.txt
    Business14.txt   Business32.txt  Business50.txt  Business69.txt  Business87.txt
    Business15.txt   Business33.txt  Business51.txt  Business6.txt   Business88.txt
    Business16.txt   Business34.txt  Business52.txt  Business70.txt  Business89.txt
    Business17.txt   Business35.txt  Business53.txt  Business71.txt  Business8.txt
    Business18.txt   Business36.txt  Business54.txt  Business72.txt  Business90.txt
    Business19.txt   Business37.txt  Business55.txt  Business73.txt  Business91.txt
    Business1.txt    Business38.txt  Business56.txt  Business74.txt  Business92.txt
    Business20.txt   Business39.txt  Business57.txt  Business75.txt  Business93.txt
    Business21.txt   Business3.txt   Business58.txt  Business76.txt  Business94.txt
    Business22.txt   Business40.txt  Business59.txt  Business77.txt  Business95.txt
    Business23.txt   Business41.txt  Business5.txt   Business78.txt  Business96.txt
    Business24.txt   Business42.txt  Business60.txt  Business79.txt  Business97.txt
    Business25.txt   Business43.txt  Business61.txt  Business7.txt   Business98.txt
    Business26.txt   Business44.txt  Business62.txt  Business80.txt  Business99.txt
    Business27.txt   Business45.txt  Business63.txt  Business81.txt  Business9.txt
    

    应该有 100 个文件(每个文件一个密码)

    $ ls -1 Business*|wc -l
    100
    
  3. 检查中的密码Business1.txt...Business2.txt

    $ cat Business*
    43xx3zUEJ5wCPLzhagmQJcWHP2cvW1
    GCRZ8uJdxQEKXRBc2hoZREpiWseFll
    CKwYTghXjJOcBuufODKWnFohG1TKel
    ZMXPyNTxIENfoWlF7cfkPxCBkQpLt1
    ...
    

答案3

我能找到的唯一从 /dev/urandom 获取所需内容的方法是使用 dd 命令。由于 /dev/urandom 输出原始位,因此使用 tr 命令将获取可打印字符,但如果您想要 30 个可打印字符,则需要从 dd 获取超过 30 个字节。因此,我选择了 300。您可以使用 cut 命令获取前 30 个可打印字符。

脚本如下:

#!/bin/bash

for n in {1..100}; do
  p=$(dd if=/dev/urandom bs=1 count=300 2>/dev/null | tr -dc A-Za-z0-9 | cut -c 1-30)
  echo "${p}" >${n}
done

exit 0

希望这可以帮助。

相关内容