1

I'm trying to do a for loop with multiple conditions and I didn't find any information about how to do it on the web

I'm sorry for the stupid questions, but I just started programming in linux

what am I doing wrong here?

    #!/bin/bash

j=0

for line in `cat .temp_` || j in `seq 0 19`
do
    ...

done

the error says wrong syntax and that I can't use ||

Thanks a lot

Epi
  • 682
  • 3
  • 10
  • 16

4 Answers4

7
for line in `cat .temp_`
do
    if ! grep -Fxq $line $dictionary && [ $j -lt 20 ]
    then
        echo $line >> $dictionary
        j=$((j+1))
    fi
    [ $j -gt 20 ] && break
done

You can't check a condition in a for loop in shell. You must do it in a extra statement. In this case so:

[ $j -gt 20 ] && break

Another solution, without break:

while read line && [ $j -lt 20 ]
do
    if ! grep -Fxq $line $dictionary && [ $j -lt 20 ]
    then
        echo $line >> $dictionary
        j=$((j+1))
    fi
done < .temp_
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
5

You are trying to combine two different styles of for-loops into one. Instead, just break if the value of j becomes too large:

j=0
while read -r line; do
    [[ j >= 20 ]] && break
    ...
done < ._temp

(This, by the way, is the preferred way to iterate over a file in bash. Using a for-loop runs into problems if the file is too large, as you essentially building a command line using the entire contents of the file.)

[UPDATE: the following is based on my conjecture as to the purpose of the loop. See Calculate Word occurrences from file in bash for the real context.]

Actually, you can dispense with the loop. You are looking for at most 20 lines from .temp_ that do not already appear in the file whose name is in dictionary, right?

sort -u .temp_ | grep -f $dictionary -Fx -v -m 20 >> $dictionary

This will call grep just once, instead of once per line in .temp_.

Community
  • 1
  • 1
chepner
  • 497,756
  • 71
  • 530
  • 681
  • The last solution (with sort -u and head) is incorrect. It can make wrong result if you have unsorted lines in .temp_ – Igor Chubin Aug 07 '12 at 16:54
  • Right. I realized there was a possibility for duplicates in the output, but didn't think through the best way to eliminate them. I'll update. – chepner Aug 07 '12 at 17:01
  • that is better, but now you lose the order of lines in `.temp_`. May be you could write both variants and explain the difference between them? (anyway +1) – Igor Chubin Aug 07 '12 at 17:21
  • Given Epi's more recent question (http://stackoverflow.com/questions/11850823/calculate-word-occurrences-from-file-in-bash), I'm going to stop second-guessing the loop :) – chepner Aug 07 '12 at 17:27
  • @chepner: wow, I haven't seen this question yet. Looks pretty interesting :) – Igor Chubin Aug 07 '12 at 17:53
0

A simple nested for-loop?

for line in `cat file`; do for j in {0..19}; do
     your_commands
done; done

If I understand you correctly (you want to run a command on every line 20 times).

uzsolt
  • 5,832
  • 2
  • 20
  • 32
0

Once I need to process all files inside a directory, but only until nth file or until all is processed:

count=1
nth=10
for f in folder/*; do
  ((count++))
  # process $f
  if [[ $count -gt $nth ]] 
  then
    break
  fi
done
Ardhi
  • 2,855
  • 1
  • 22
  • 31