paul paul - 3 months ago 10
Bash Question

Pass condition into a function

I´m trying to pass to a function a condition to be applied in a while loop, and if it´s true increase a variable of that condition.
If that´s possible on bash?. Here an example code

a(){
condition=$1
echo "condition:$condition"
while [[ $condition ]] # --> run the condition
do
num1=num1+1
echo "increase num1:$num1" # num1 is a var of the condition
done
}


b(){
num1=1
num2=3
`a $num1 < $num2`
}

b

Answer

You can also use as below;

#! /bin/bash

a(){
    condition="(( \$num $2 $3 ))"
    num=$1
    while eval $condition
      do
        (( num++ ))
        echo "increase num1:$num" 
      done   
}


b(){
    num1=1
    num2=5
    a $num1 '<' $num2
}

b
Comments