hostcolors.blogg.se

Mac bash for loop
Mac bash for loop










mac bash for loop
  1. #MAC BASH FOR LOOP HOW TO#
  2. #MAC BASH FOR LOOP CODE#

The two statements that allow to do that are break and continue: There are ways to alter the normal flow of a for loop in Bash. Then at each iteration I print the value of the counter together with the line from the file.Īfter doing that I use the Bash arithmetic operator to increase the value of the variable COUNTER by 1.Īnd here is the output of the script: Counter 0: Istanbul:15,067,724Ĭounter 9: Bucharest:2,106,144 Break and Continue in a Bash For Loop Let’s modify the previous program and define a counter whose value is printed at every iteration: #!/bin/bashĪs you can see I have defined a variable called COUNTER outside of the for loop with its initial value set to 0. It can also be used to access the elements of a data structure inside the loop (this is not the case for our example). The use of a counter is very common in all programming languages. You can use a counter to track each iteration of the loop. In a for loop you can also define a variable called counter. This means we can use any commands we want to generate the LIST to be passed to the for loop.ĭo you have in mind any other possible commands?Īlso, the for loop is not the only option to create a loop in a Bash script, another option is a while loop. We are passing the list to the for loop using the cat command.

mac bash for loop

The echo command can be replaced by any sequence of commands based on what you want to do with each line in the file.Īnd the output of the script is…. Here we are using command substitution to assign the output of the cat command to the LINES variables.įinally the for loop allows to go through each line of the file: for LINE in $LINESĭo and done are used to define the commands to be executed at each iteration of the for loop.įor example, if you have a file with 10 lines the for loop will go through 10 iterations and at each iteration it will read one line of the file. So, how can we use a Bash for loop to go through the content of this file?įirst we will store the name of the file in a variable FILENAME="european-cities.txt"Īfter that, we will use another variable and the cat command to get all the lines in the file: LINES=$(cat $FILENAME)

mac bash for loop

the number of people who live in that city.īelow you can see the format of the text file, a colon is used to separate each city from the number of people who live in that city: Istanbul:15,067,724.

#MAC BASH FOR LOOP HOW TO#

In this article you will learn how to use the for loop in Bash and specifically to go through the lines of a file.īut why would you do that? Going through the lines of a file?įor instance, you might need to do that if you have exported data from an application into a file and you want to elaborate that data somehow. The N commands between do and done are executed for each item in the list. a sequence of strings separated by spaces.The logic executed is every time the same and the only thing that changes is the city.īelow you can see the generic syntax for a Bash for loop: for item in The program can use a for loop to go through each city in the list and print the number of people for that city. For instance, let’s say you want to write a program that prints the number of people who live in the 10 biggest european cities.

#MAC BASH FOR LOOP CODE#

I want to loop through the lines of a file with a Bash script and one of the ways to do it is using a for loop.Ī for loop is one of the most common programming constructs and it’s used to execute a given block of code given a set of items in a list.












Mac bash for loop