-1

Dears, I have a code to generate many text files, and I faced a problem that the code takes time as a result of repetitions so, how "simply" I add a command in bash .sh script and exclude the repeating values e.g. (0.45, 0.45,0.50) is used but (0.45,0.50,0.45) and (0.50,0.45,0.45) ... are excluded

i = 0

for VX1 in 0.45 0.50 0.55
 do

for VX2 in 0.45 0.50 0.55
 do

for VX3 in 0.45 0.50 0.55
 do
let "i++"
  • `(0.45, 0.45,0.50)` has repeating values `0.45`, why is it used? – Barmar Feb 24 '21 at 21:04
  • @CharlesDuffy What array is in this question>? – Barmar Feb 24 '21 at 21:06
  • @Barmar Ahh -- I read VX1, VX2 and VX3 as three separate arrays we wanted to iterate over (where they just happened to be using the same sample data for each). Without that assumption... honestly, I'm having a lot more trouble making heads or tails of what the OP actually wants to accomplish. – Charles Duffy Feb 24 '21 at 21:07
  • I don't really understand the criteria, but you basically just need an `if` statement in the inner loop that checks whether the values meet your criteria for inclusion before generating the filename. – Barmar Feb 24 '21 at 21:10
  • 1
    Could you please post some complete, runnable code sample instead of just some partial snippet. – daniu Feb 24 '21 at 21:11
  • 1
    How is it that `(0.45, 0.45,0.50)` is taken as *not* containing repeated values? – John Bollinger Feb 24 '21 at 21:15
  • @MuradAlDamen : Your variables (VX1 etc.) are not arrays, but scalars. Also, your posted code is lacking the `done` statements inside the loop. – user1934428 Feb 25 '21 at 07:31

1 Answers1

1

You can check whether the values are equal right after starting an inner loop. Use continue to skip an iteration.

To test against two values, you can use an extglob pattern in bash.

#! /bin/bash
i=0
shopt -s extglob
for VX1 in 0.45 0.50 0.55 ; do
    for VX2 in 0.45 0.50 0.55 ; do
        if [[ "$VX2" == "$VX1" ]] ; then
            continue
        fi
        for VX3 in 0.45 0.50 0.55 ; do
            if [[ "$VX3" == @("$VX2"|"$VX1") ]] ; then
                continue
            fi
            let "i++"
        done
    done
done
echo $i
choroba
  • 231,213
  • 25
  • 204
  • 289