how to evaluate number between a range of numbers using different variables in bash

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I have many pairs of integers like (a,b),(c,d),(e,f) .. so on. I need to check if the value of 1st element lies between a range of 90% to 150% of the 2nd element and print the value as Pass.



The values (a,b) are from different columns of a file for two different times respectively.



File1:1 12 123 1234-text-1 12345



File2:2 23 234 1239-text-1 12347



a="$(find . -name *abc.gz* -mtime 1 | xargs zcat | awk -F"," 'print $4' | grep "text-1" | awk '$1=$1;print' | awk 'print $1')"

b="$(find . -name *abc.gz* -mtime 10 | xargs zcat | awk -F"," 'print $4' | | grep "text-1"| awk '$1=$1;print' | awk 'print $1')"

#a=1234
#b=1239

b1="$(bc <<<"$b*9/10")"
b2="$(bc <<<"$b*15/10")"

if [ $a -ge $b1 -a $a -le $b2 ];
then
echo "PASS"
else
echo "FAIL"
fi


I need to do it for different text values like 'text-2', 'text-3' etc for different columns of a file $5, $6 etc.



Please guide me a single iterative solution in Bash.







share|improve this question

















  • 1




    Are these from different files? Your question is really unclear: please simplify it.
    – glenn jackman
    Jun 21 at 15:36










  • @Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column using cut instead of awk. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.
    – user1934428
    Jun 22 at 5:09











  • @Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that $1=$1 is a no-op: You just assign the first field to itself. What's the purpose of this?
    – user1934428
    Jun 22 at 5:12














up vote
0
down vote

favorite












I have many pairs of integers like (a,b),(c,d),(e,f) .. so on. I need to check if the value of 1st element lies between a range of 90% to 150% of the 2nd element and print the value as Pass.



The values (a,b) are from different columns of a file for two different times respectively.



File1:1 12 123 1234-text-1 12345



File2:2 23 234 1239-text-1 12347



a="$(find . -name *abc.gz* -mtime 1 | xargs zcat | awk -F"," 'print $4' | grep "text-1" | awk '$1=$1;print' | awk 'print $1')"

b="$(find . -name *abc.gz* -mtime 10 | xargs zcat | awk -F"," 'print $4' | | grep "text-1"| awk '$1=$1;print' | awk 'print $1')"

#a=1234
#b=1239

b1="$(bc <<<"$b*9/10")"
b2="$(bc <<<"$b*15/10")"

if [ $a -ge $b1 -a $a -le $b2 ];
then
echo "PASS"
else
echo "FAIL"
fi


I need to do it for different text values like 'text-2', 'text-3' etc for different columns of a file $5, $6 etc.



Please guide me a single iterative solution in Bash.







share|improve this question

















  • 1




    Are these from different files? Your question is really unclear: please simplify it.
    – glenn jackman
    Jun 21 at 15:36










  • @Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column using cut instead of awk. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.
    – user1934428
    Jun 22 at 5:09











  • @Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that $1=$1 is a no-op: You just assign the first field to itself. What's the purpose of this?
    – user1934428
    Jun 22 at 5:12












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have many pairs of integers like (a,b),(c,d),(e,f) .. so on. I need to check if the value of 1st element lies between a range of 90% to 150% of the 2nd element and print the value as Pass.



The values (a,b) are from different columns of a file for two different times respectively.



File1:1 12 123 1234-text-1 12345



File2:2 23 234 1239-text-1 12347



a="$(find . -name *abc.gz* -mtime 1 | xargs zcat | awk -F"," 'print $4' | grep "text-1" | awk '$1=$1;print' | awk 'print $1')"

b="$(find . -name *abc.gz* -mtime 10 | xargs zcat | awk -F"," 'print $4' | | grep "text-1"| awk '$1=$1;print' | awk 'print $1')"

#a=1234
#b=1239

b1="$(bc <<<"$b*9/10")"
b2="$(bc <<<"$b*15/10")"

if [ $a -ge $b1 -a $a -le $b2 ];
then
echo "PASS"
else
echo "FAIL"
fi


I need to do it for different text values like 'text-2', 'text-3' etc for different columns of a file $5, $6 etc.



Please guide me a single iterative solution in Bash.







share|improve this question













I have many pairs of integers like (a,b),(c,d),(e,f) .. so on. I need to check if the value of 1st element lies between a range of 90% to 150% of the 2nd element and print the value as Pass.



The values (a,b) are from different columns of a file for two different times respectively.



File1:1 12 123 1234-text-1 12345



File2:2 23 234 1239-text-1 12347



a="$(find . -name *abc.gz* -mtime 1 | xargs zcat | awk -F"," 'print $4' | grep "text-1" | awk '$1=$1;print' | awk 'print $1')"

b="$(find . -name *abc.gz* -mtime 10 | xargs zcat | awk -F"," 'print $4' | | grep "text-1"| awk '$1=$1;print' | awk 'print $1')"

#a=1234
#b=1239

b1="$(bc <<<"$b*9/10")"
b2="$(bc <<<"$b*15/10")"

if [ $a -ge $b1 -a $a -le $b2 ];
then
echo "PASS"
else
echo "FAIL"
fi


I need to do it for different text values like 'text-2', 'text-3' etc for different columns of a file $5, $6 etc.



Please guide me a single iterative solution in Bash.









share|improve this question












share|improve this question




share|improve this question








edited Jun 21 at 9:56
























asked Jun 21 at 9:31









Varun

11




11







  • 1




    Are these from different files? Your question is really unclear: please simplify it.
    – glenn jackman
    Jun 21 at 15:36










  • @Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column using cut instead of awk. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.
    – user1934428
    Jun 22 at 5:09











  • @Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that $1=$1 is a no-op: You just assign the first field to itself. What's the purpose of this?
    – user1934428
    Jun 22 at 5:12












  • 1




    Are these from different files? Your question is really unclear: please simplify it.
    – glenn jackman
    Jun 21 at 15:36










  • @Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column using cut instead of awk. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.
    – user1934428
    Jun 22 at 5:09











  • @Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that $1=$1 is a no-op: You just assign the first field to itself. What's the purpose of this?
    – user1934428
    Jun 22 at 5:12







1




1




Are these from different files? Your question is really unclear: please simplify it.
– glenn jackman
Jun 21 at 15:36




Are these from different files? Your question is really unclear: please simplify it.
– glenn jackman
Jun 21 at 15:36












@Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column using cut instead of awk. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.
– user1934428
Jun 22 at 5:09





@Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column using cut instead of awk. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.
– user1934428
Jun 22 at 5:09













@Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that $1=$1 is a no-op: You just assign the first field to itself. What's the purpose of this?
– user1934428
Jun 22 at 5:12




@Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that $1=$1 is a no-op: You just assign the first field to itself. What's the purpose of this?
– user1934428
Jun 22 at 5:12










1 Answer
1






active

oldest

votes

















up vote
0
down vote













This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:



$ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
bash: [: 1115.1: integer expression expected
nope


You can have bc do the comparisons:



check() 
check 1234 1239 # => OK
check 1234 12390 # => NO





share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451055%2fhow-to-evaluate-number-between-a-range-of-numbers-using-different-variables-in-b%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:



    $ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
    bash: [: 1115.1: integer expression expected
    nope


    You can have bc do the comparisons:



    check() 
    check 1234 1239 # => OK
    check 1234 12390 # => NO





    share|improve this answer

























      up vote
      0
      down vote













      This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:



      $ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
      bash: [: 1115.1: integer expression expected
      nope


      You can have bc do the comparisons:



      check() 
      check 1234 1239 # => OK
      check 1234 12390 # => NO





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:



        $ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
        bash: [: 1115.1: integer expression expected
        nope


        You can have bc do the comparisons:



        check() 
        check 1234 1239 # => OK
        check 1234 12390 # => NO





        share|improve this answer













        This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:



        $ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
        bash: [: 1115.1: integer expression expected
        nope


        You can have bc do the comparisons:



        check() 
        check 1234 1239 # => OK
        check 1234 12390 # => NO






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jun 21 at 15:44









        glenn jackman

        45.6k265100




        45.6k265100






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451055%2fhow-to-evaluate-number-between-a-range-of-numbers-using-different-variables-in-b%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay