For Loops in Bash

What is a For Loop?

  • A for loop allows you to perform a task repeatedly for every item in a set.
  • An if statement performs a task once if a condition is true.
  • A while loop performs a task repeatedly until a state is reached.
  • A for loop executes a command or set of commands against each item in a set.

Example of a For Loop:

  • The script sets up a list to iterate through. When it iterates against the final element in the set, the loop is done, and it exits.
  • for current_number in 1 2 3 4 5 6 7 8 9 10
  • do
  • echo $current_number
  • sleep 1
  • done
  • The variable current_number is declared as soon as the for loop starts running.
  • It's common to see just one character for the name of the variable.
  • The first time it runs, current_number will be set to one.
  • Each time through the loop, current_number will be set to the next value, until the final value.

Simplify the Script

  • Simplifying a script whenever it makes sense to do so is always a great idea.
  • The fewer characters within a script, the easier the script is to maintain.
  • for n in {1..10}
  • do
  • echo $n
  • sleep 1
  • done
  • echo "This is outside of the for loop"
  • n being a variable that just equals whatever the current item in the set is at the time it's running
  • Once it reaches 10 in the set, it's going to break out of the for loop and then echo the statement that you see right here.

Script Example:

  • Create some test files that will iterate through, but essentially what this is going to do is look in the log files directory for any log file or any file that has a name that matches *.log, which is going to be anything that has a file name that ends in .log.
  • For every file in the log files directory that ends in .log, it's going to run the tar command, and the options that it's going to run are czvf.
  • It's going to create a zipped file; verbose is what v stands for.
  • We want to see what it's doing, and we're going to basically create a tarball or a compressed file for every file that's in the log files directory.
  • for file in /logsfiles/*.log
  • do
  • tar czvf $file.tar.gz $file
  • done
  • We need to give the archived files a file name, then we'll add the file variable again because we want to reference the file that we're actually going to archive.
  • What we could do is remove the uncompressed files to get them off our system.
  • It's very common if we have a Linux server that is nearing full capacity of the storage.
  • So what we could do is start tarring up some files and then remove the original files to reclaim some space.
  • Now, obviously, you'd probably want to use something like logrotate; that would be a much better way to do this, but that's outside of the scope of this series. But either way, we have an example of a for loop that actually gives us value.
  • file in this case, file is a variable. We're creating a variable; this is a variable that the for loop will use to iterate over the contents of the log files directory, where it's looking for any file that ends in .log inside that directory.
  • For every file that it finds, what it's going to do is run the tar command against that file.
  • It's going to create a tarball or a compressed file that is called file.tar.gz, but file in this case is part of that file name; it's actually the name of a file that we want to compress.
  • When Bash runs this, it's going to replace $file in that command with the actual file name .log that is currently iterating over within the log files directory. The file that is going to compress is that file called file, and then the for loop exits like you see right here.

Conclusion

  • Considering how a lot of companies out there have deliverables that they have to get sent out, this logic could actually automate an entire delivery system.
  • Now, you might not use for loops quite as often as other features within Bash, but it's yet another component of Bash that you can either use or at least add to your toolbox in case a use case comes up for it later.