AIX's "bc" Command: Doing a Conditional Expression?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AIX's "bc" Command: Doing a Conditional Expression?
# 8  
Old 12-12-2012
Code:
#!/bin/ksh

a=1.72
b=1.71

if [ "$(echo "if (${a} > ${b}) 1" | bc)" -eq 1 ] ; then
   echo ">"
else
   echo "<"
fi;

These 2 Users Gave Thanks to vgersh99 For This Post:
# 9  
Old 12-13-2012
Here is a snippet from IBM.com's AIX Documentation Section under the "bc Command".

You can find it here --> pSeries and AIX Information Center
Quote:
Unlike all other operators, the following relational operators are only valid as the object of an if or while statement or inside a for statement:
  • < (less than)
  • > (greater than)
  • <= (less than, equal sign)
  • >= (greater than, equal sign)
  • == (double equal sign)
  • != (exclamation, equal sign)
Thanks,
Matt

---------- Post updated at 05:00 PM ---------- Previous update was at 04:59 PM ----------

Hey vgersh99, thanks for the reply!!

Cool... I'll give that a try and post back.

Thanks Again,
Matt

---------- Post updated at 05:05 PM ---------- Previous update was at 05:00 PM ----------

Hey vgersh99, I haven't gotten a chance to try that yet. I have to deal with another issue here at work, and it's almost closing time!!!

So I'll post back first thing tomorrow with my results. Thanks!


Thanks Again,
Matt

---------- Post updated 12-13-12 at 10:26 AM ---------- Previous update was 12-12-12 at 05:05 PM ----------

Hey vgersh99 thanks, that did it!

I'm using this in a function that I will call in my script in order to do a Relational Expression (i.e. >, <, <=, >=, ==) on
floating point numbers
Code:
#!/bin/bash

### This function will check a conditional expression of floating point numbers, which will return either TRUE or FALSE (i.e. 0 or 1)
function float_condition()
{
    local cond=0
    opt="$*"

    if [ $# -gt 0 ]
     then
        cond=$(echo "if ($opt) 1" | bc 2>/dev/null)

        if [[ -z "$cond" ]]
         then
            cond=0
        fi

        if [[ "$cond" != 0  &&  "$cond" != 1 ]]
         then
                cond=0
        fi
    fi

    local stat=$((cond == 0))
    return $stat
}

Thanks guys for all your help!!


Thanks Again,
Matt
This User Gave Thanks to mrm5102 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. AIX

How to enable "TCP MD5 Signatures" and "https" on AIX?

I have searched many times but nothing found. Somebody help please :(:(:( (1 Reply)
Discussion started by: bobochacha29
1 Replies

3. UNIX for Advanced & Expert Users

AIX - io info get from "libperfstat" not match "iostat"

Hi, everyone. I need to write a program to get io info based on libperfstat. But the "write time" of a disk is just half of the value get from iostat. I'm confused and can't explain. Help please. How I calculate "write service time per sec": In iostat: write service... (0 Replies)
Discussion started by: jackliang
0 Replies

4. Shell Programming and Scripting

AIX function example with "shift" command

Hello, I am reading one of the AIX manuals about shell scripting and (AIX 5) and I found this example when introducing to functions: function usage { prog="$1"; shift print -u2 "$prog: usage: $prog $@" exit 1 } This example is meant to be easy but I don't understand what it is... (5 Replies)
Discussion started by: Kibou
5 Replies

5. Shell Programming and Scripting

Source command returns error when it strikes conditional statement "ifeq"

Hello All, I am running source command on my project configuration file app.cfg which has conditional statements with make file systax E.g ifeq ($(APP_CMP_DIR),trunk). When I source this file it throws error: syntax error near unexpected token... (1 Reply)
Discussion started by: anand.shah
1 Replies

6. Shell Programming and Scripting

sed returns error "sed: -e expression #1, char 18: unterminated `s' command"

Hello All, I have something like below LDC100/rel/prod/libinactrl.a LAA2000/rel/prod/libinactrl.a I want to remove till first forward slash that is outputshould be as below rel/prod/libinactrl.a rel/prod/libinactrl.a How can I do that ??? (8 Replies)
Discussion started by: anand.shah
8 Replies

7. Shell Programming and Scripting

why "aab" matchs "ab" when using reglar expression ?

Please see the following code, why "aab" matchs "ab" when using reglar expression ? $ ] && echo "ok" || echo "error"; ok $ ] && echo "ok" || echo "error"; error $ ] && echo "ok" || echo "error"; error $ ] && echo... (8 Replies)
Discussion started by: 915086731
8 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. Shell Programming and Scripting

cshell integer expression from "0000" to "1999"

I have 2000 files named like "file-fr0000.log", "file-fr1999.log"... I wanna generate the file names automatically in the following c shell script: set fr = 0 while ($fr <= 1999) grep "ENERGY" file-fr$fr.log > data.dat @ fr = ( $fr + 1 ) end The above will generate file names... (3 Replies)
Discussion started by: rockytodd
3 Replies

10. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies
Login or Register to Ask a Question