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?
# 1  
Old 12-12-2012
AIX's "bc" Command: Doing a Conditional Expression?

Hello All,

I'm trying to issue a conditional expression using "bc" on floating point numbers. I had this working on my linux box,
but as soon as I transferred the file over to an AIX Server, it would not work anymore...

This is the "WORKING" command on linux (below), which I can't seem to figure out how to do on AIX's Version of "bc". I've read
the manpage which didn't have a very good explaination for it, except that, I believe the ONLY way you can use those
relational operators is in an if|for|while statements/loops...
Code:
condition=$(echo "$size == 0" | bc 2>/dev/null)

So I think I have to somehow convert this to a "bc" If Statement.
Anyone know how this is done?


Thanks in Advance,
Matt
# 2  
Old 12-12-2012
Matt, can you explain your requirement? What input do you have and what output do you expect?

Are you trying to check if a variable value is equal to 0?
# 3  
Old 12-12-2012
Hey bipinajith, thanks for the reply!

Ok, basically yes...
For the expression below, $size COULD possibly be a floating point number, OR it could be a '0'. And since I'm writing this in a
shell script I can't exactly use a float in a Shell If Statement.

Code:
condition=$(echo "$size == 0" | bc 2>/dev/null

And for what I'm looking for as output, I really just need 0|1 (i.e. TRUE or FALSE) result.

Does that make sense?


Thanks Again,
Matt
# 4  
Old 12-12-2012
Yes.

You can use awk for floating point comparison. Here is an example:-
Code:
echo | awk -v size1=5.65 -v size2=3.14e-22  '{if (size1<size2) printf ("%s\n", "Less-than"); else printf ("%s\n", "Greater-than"); }

I hope this helps.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 12-12-2012
Oh, cool. That should work too! Thanks...

Any idea how something like that would look for "bc" though?
I'm kinda curious now how that would work....

Thanks Again,
Matt
# 6  
Old 12-12-2012
Can you replace letter E or e in the exponential notation with ^ sign and re-try:-
Code:
export size1=5.65
export size2=3.14^22
echo "$size1 < $size2" | bc

# 7  
Old 12-12-2012
Hey, thanks again for the reply.

Ok, tried your "bc" example, but I got an error...
Code:
# export SIZE_1=5.65            
# export SIZE_2=3.14
# echo "$SIZE_1 < $SIZE_2" | bc
syntax error on line 1 stdin

I'm pretty sure I need to somehow do this in a "If Statement" in the bc command. I just can't figure out the syntax.

Thanks Again,
Matt
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