简体   繁体   中英

Bash: nested loop one way comparison

I have one queston about nested loop with bash.

I have an input files with one file name per line (full path) I read this file and then i make a nest loop:

    for i in $filelines ; do
    echo $i
    for j in $filelines ; do
         ./program $i $j
    done
done

The program I within the loop is pretty low. Basically it compare the file A with the file B.

I want to skip A vs A comparison (ie comparing one file with itslef) AND I want to avoid permutation (ie for file A and B, only perform A against B and not B against A).

What is the simplest to perform this?

Version 2: this one takes care of permutations

#!/bin/bash

tmpunsorted="/tmp/compare_unsorted"
tmpsorted="/tmp/compare_sorted"

>$tmpunsorted

while read linei
do
    while read linej
    do
        if [ $linei != $linej ]
        then
            echo $linei $linej | tr " " "\n" | sort | tr "\n" " " >>$tmpunsorted
            echo >>$tmpunsorted
        fi
    done <filelines
done <filelines

sort $tmpunsorted | uniq > $tmpsorted

while read linecompare
do
    echo "./program $linecompare"
done <$tmpsorted

# Cleanup
rm -f $tmpunsorted
rm -f $tmpsorted

What is done here:

  • I use the while loop to read each line, twice, i and j
  • if the value of the lines is the same, forget them, no use to consider them
  • if they are different, output them into a file ($tmpunsorted). And they are sorted in alphebetical order before going tothe $tmpunsorted file. This way the arguments are always in the same order. So ab and ba will be same in the unsorted file.
  • I then apply sort | uniq sort | uniq on $tmpunsorted, so the result is a list of individual argument pairs.
  • finally loop on the $tmpsorted file, and call the program on each individual pair.
  • Since I do not have your program, I did an echo, which you should remove to use the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM