在bash中使用for循环创建目录

在bash中使用for循环创建目录,bash,for-loop,directory,Bash,For Loop,Directory,所以我想根据一个变量的计数创建x个目录 #!/bin/bash countOfCaptureFiles=$(wc -l < allCaptures.txt) echo $countOfCaptureFiles start=1 end=countOfCaptureFiles for((i=$start;i<=$end;i++)) do mkdir "SnortAlert${i}" done 在我当前的测试环境中,countOfCaptureFiles的值是3,因此我希望创建3个名为

所以我想根据一个变量的计数创建x个目录

#!/bin/bash
countOfCaptureFiles=$(wc -l < allCaptures.txt)
echo $countOfCaptureFiles

start=1
end=countOfCaptureFiles
for((i=$start;i<=$end;i++))
do
mkdir "SnortAlert${i}"
done
在我当前的测试环境中,countOfCaptureFiles的值是3,因此我希望创建3个名为SnortAlert1 SnortAlert2 SnortAlert3的目录

我像这样运行脚本:./myscript.sh


然而,当我这样做时,我得到了错误和没有输出,我相信这与assingend=countOfCaptureFiles有关,但我不知道如何修复这个问题,任何帮助都将不胜感激

您的代码正在运行。但是,您可以使用诸如wc、cat之类的外部程序来最小化,如下所示

#!/bin/bash
i=1
while read line;do
    mkdir "SnortAlert${i}"
    let i=i+1
done <allCaptures.txt

你的代码正在运行。但是,您可以使用诸如wc、cat之类的外部程序来最小化,如下所示

#!/bin/bash
i=1
while read line;do
    mkdir "SnortAlert${i}"
    let i=i+1
done <allCaptures.txt

您可以使用for in语句简化如下代码

#!/bin/bash
var=$(cat allCaptures.txt)
for line in $var
do
   mkdir "SnowAlert${line}"
done

您可以使用for in语句简化如下代码

#!/bin/bash
var=$(cat allCaptures.txt)
for line in $var
do
   mkdir "SnowAlert${line}"
done

您在end=作业中缺少$。Make it end=$countOfCaptureFiles。我已经在我的mac上测试了你的代码。没问题是可选的,因为从排序的角度来看,iYou可能会受益于将所有附加在末尾的整数设置为固定宽度,例如001、002、003等。。。。您可以使用printf-v name SnortAlert%03d$i,然后使用mkdir$name如果您在end=assignment中缺少$。Make it end=$countOfCaptureFiles。我已经在我的mac上测试了你的代码。没问题是可选的,因为从排序的角度来看,iYou可能会受益于将所有附加在末尾的整数设置为固定宽度,例如001、002、003等。。。。您可以使用printf-v name SnortAlert%03d$i,然后使用mkdir$name