3

So I would like to create x amount of Directories according to the count of a variable.

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

start=1
end=countOfCaptureFiles
for((i=$start;i<=$end;i++))
do
mkdir "SnortAlert${i}"
done

In my current testing environment the value of countOfCaptureFiles is 3, so I was hoping to create 3 Directories named SnortAlert1 SnortAlert2 SnortAlert3.

I run the script like so: ./myscript.sh

However when I do this I get errors and no output and I believe it has something to do with assinging end=countOfCaptureFiles but I am not sure how to fix this, any help would be appreciated!

3
  • You are missing a $ in the end= assignment. Make it end=$countOfCaptureFiles. Commented May 17, 2017 at 3:22
  • I have tested your code on my mac. No problems. $ is optional because i<=$end is evaluated i<=countOfCaptureFiles and evaluated again to i<=3. What is the error message? Did you 'chmod +x myscript.sh'? Commented May 17, 2017 at 3:44
  • You may benefit from a sort standpoint by making all integers you append at the end a fixed width, e.g. 001, 002, 003, .... You can use printf -v name "SnortAlert%03d" "$i", then mkdir "$name" Commented May 17, 2017 at 3:55

2 Answers 2

5

Your code is working. But you can minimize using external programs (like wc, cat) like the following.

#!/bin/bash
i=1
while read line;do
    mkdir "SnortAlert${i}"
    let i=i+1
done <allCaptures.txt
Sign up to request clarification or add additional context in comments.

Comments

2

You can use for in statement to simplify the code like this"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.