For loop statements order of operations


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
For loop statements order of operations

Say I have a for loop that parse through a file....Say it look for the colors red and blue in sections of the file. Say it find red before it find blue in the file.

Say I have two if statements in the for loop

Something like if blue is found print blue is my favorite color is the first if statement.

A second if statement if red is found print red is my favorite color.

Considering my if statements are in the opposite order of the colors found in the file which if statement get printed first and why?
# 2  
You're reading the file in sequential order. The order of your if statements is unimportant for the requirement you specified.
# 3  
Quote:
Originally Posted by Scott
You're reading the file in sequential order. The order of your if statements is unimportant for the requirement you specified.
Why is that? I thought code was read from top to bottom. So why doesn't hitting the first if statement first doesn't matter?

So basically what you are saying is all the code in the for loop code block is essentially executed at the same time?
# 4  
I don't think that's what I said at all. If you are reading the file sequentially, you'll always get whatever color comes first, regardless of the order in which you process it.

Or maybe I'm not grasping your question. It seems straight forward, but it is late, and I maybe need some sleep!

Code:
$ cat file
red
green
blue

Code:
for color in line; do
  [ "$color" = red ] && echo "I found red"
  [ "$color" = blue ] && echo "I found blue"
done < file

Code:
I found red
I found blue

Code:
for color in line; do
  [ "$color" = blue ] && echo "I found blue"
  [ "$color" = red ] && echo "I found red"
done < file

Code:
I found red
I found blue

# 5  
Quote:
Originally Posted by Scott
I don't think that's what I said at all. If you are reading the file sequentially, you'll always get whatever color comes first, regardless of the order in which you process it.

Or maybe I'm not grasping your question. It seems fairly simple, but it is late, and I maybe need some sleep!

Code:
$ cat file
red
green
blue

Code:
for color in line; do
  [ "$color" = red ] && echo "I found red"
  [ "$color" = blue ] && echo "I found blue"
done < file

Code:
I found red
I found blue

Code:
for color in line; do
  [ "$color" = blue ] && echo "I found blue"
  [ "$color" = red ] && echo "I found red"
done < file

Code:
I found red
I found blue

Scott,

Is the reason this work this way is because the for loop is processing one color at a time before moving to the next color?
# 6  
Yes. It works this way because you are reading a file (sequentially) and processing it as it comes.

if you find red, process that
if you find blue, process that

Yields the same results as

if you find blue, process that
if you find red, process that

if you're processing the same file.

If you were to read the entire file first, and store it somewhere, then the order of the processing would matter.

e.g.
Code:
for color in line; do
  [ "$color" = red ] && foundred=y
  [ "$color" = blue ] && foundblue=y
done < file

[ "$foundred" = y ] && echo found red
[ "$foundblue" = y ] && echo found blue

Code:
found red
found blue

Would be different with...
Code:
for color in line; do
  [ "$color" = red ] && foundred=y
  [ "$color" = blue ] && foundblue=y
done < file

[ "$foundblue" = y ] && echo found blue
[ "$foundred" = y ] && echo found red

Code:
found blue
found red

This User Gave Thanks to Scott For This Post:
# 7  
I think what is meant in this thread is to use a while read loop rather than a for loop:
Code:
while read color
do
  ....
done < file

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #113
Difficulty: Easy
Linux is not Unix per se, but Linux is a Unix-like operating system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Operations

Hi Folks, Below is example of an Input data which is used, based on the last 2, 3 & 4 column, I want my first column data to be collated as shown in the output section. a,ac,tc,ic b,ac,tc,ic c,ac,tc,ic d,ac,tc,ic b,bc,tc,ic d,bc,tc,ic e,bc,tc,ic I want my output to be ... (2 Replies)
Discussion started by: nikhil jain
2 Replies

2. Shell Programming and Scripting

Bash directory loop and order by creation date?

Hello, how in bash i can get directory loop and order by creation date? THX! :) #!/bin/bash for folder in /home/test/* do if ; then echo $folder; fi (12 Replies)
Discussion started by: ZerO13
12 Replies

3. Shell Programming and Scripting

2 statements in for loop

Bash shell, variables i and rem are working fine in 2 separate for loops, but I'd like to consolidate them like this: for && This gives syntax error on &&. Thanks in advance for direction. (5 Replies)
Discussion started by: p1ne
5 Replies

4. UNIX for Dummies Questions & Answers

Loop Through Files in Order and mapping

Hi there, How can I loop through files with order. I tried using ls .html | sort -v However the filename is with spaces:1233.61.47.0 - 121.61.123.112 nexpose.html Here is the original code that is working well, but I need to sort through the filename.echo "<center>" for file in *.html... (2 Replies)
Discussion started by: alvinoo
2 Replies

5. Shell Programming and Scripting

Shell operations from C++

Hi everyone, I need little help in shell operations from C++ program. Here I furnish the details of problem: 1. Lets say my current working path is myWorkingPath. 2. In my working path I have list of name directories and each name directory has two more sub directories say A/B. (now path to... (5 Replies)
Discussion started by: linuxUser_
5 Replies

6. Shell Programming and Scripting

String operations

Can you give me some suggestions to split below string into three parts using shell scripts.. Script has to print all alphabets before the number, then number and then all alphabets after the number.. input: chris martin 200173 845747 mech engineer output: chris martin 200173 845747 mech... (6 Replies)
Discussion started by: nram_krishna@ya
6 Replies

7. Shell Programming and Scripting

How to execute a no of SELECT COUNT(*) statements using a loop

HI Unix Gurus, I have a number of SELECT count(*) statements in an input file and I want to execute it using a shell script but one by one using loop in script.... How can I do this..... (7 Replies)
Discussion started by: ustechie
7 Replies

8. Shell Programming and Scripting

is that possible to keep statements in any loop??

Hi, Actually i stored all validdisks in one array and corresponding partitions required for all individual disks in other array.. Example: Validdisks=dsk2 dsk3 dsk5 ValidPartition=4 4 3 Now i have to create domain.. Domain creation can be done by below commands: fs_setup -d... (1 Reply)
Discussion started by: mansa
1 Replies

9. Shell Programming and Scripting

for i loop with conditional statements?

New to scripting in general, so patience plz. If I ask a stupid question or don't get it, I thank you for your kindness in advance. That said, did a for i loops checks to see if a PB* file is there but I need to know two things before I copy the file. I need to know if the file's create date... (2 Replies)
Discussion started by: xgringo
2 Replies

10. Shell Programming and Scripting

File operations

Hi there, I want some help on scripting regarding file processing. I have a scenario in which I have 10 files. (file1.txt, file2.txt....) and they are in paricular format. I want to read these files and append some text lines at the begining of each file and write this updated contents of... (2 Replies)
Discussion started by: chiragmistry21
2 Replies

Featured Tech Videos