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

时间:2017-05-17 03:11:05

标签: 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个名为SnortAlert1 SnortAlert2 SnortAlert3的目录。

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

然而,当我这样做时,我得到错误,没有输出,我相信它与ass = endOfCaptureFiles有关,但我不知道如何解决这个问题,任何帮助都将不胜感激!

2 个答案:

答案 0 :(得分:2)

您的代码正常运行。但是你可以尽量减少使用外部程序(如wc,cat),如下所示。

.rotating-item-wrapper {
  position: relative;
  width: 320px;
  height: 240px;
  /* background-color: red; */
}

.rotating-item {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
}

答案 1 :(得分:0)

您可以使用in语句来简化这样的代码&#34;

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