Numeric computations in bash

Thread Tools Search this Thread
# 1  
Numeric computations in bash

I am on Ubuntu 11.10 using bash.

I want to perform some numeric computations and have the following options:

# Setting verbosity levels
none=0; low=1; medium=2; high=3; debug=4

(( vbLevel=medium ))
if (( vbLevel > low )); then
  echo "Test 1, $vbLevel"

if [[ vbLevel -gt low ]]; then
  echo "Test 2"

if [ $vbLevel -gt $low ]; then
  echo "Test 3"

I am wondering what is the best way to:

1. Set the number
vbLevel=$medium     or     ((vbLevel=medium))

2. Doing the comparison
(())     or     [[]]     or     []

# 2  
In shell code you are concerned about: efficiency and readability
The former because you want to minimize the impact of your code on the system, the latter because you and others will have to change this stuff X years from now when you have completely forgotten about it, and it no longer works the way you need.

Either of these is equally fast:

The first one is more conventional, and is more likely to port.

[ is a bash builtin, [[ is a keyword so they both run as part of the shell, as does (( )).
So it boils down to which one is the easiest to for another person to interpret, which is a personal matter.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
This one is best (and the most portable):
if [ $vbLevel -gt $low ]; then
  echo "Test 3"

This User Gave Thanks to methyl For This Post:
# 4  
I am thinking of setting the verbosity level using a call to a function.

In the bash script I have two variables

hasArgument_vbLevel       # set to "true" if user specified vbLevel
 value_vbLevel                  # contains the verbosity level of the user

I would want to call the function and pass it value_vbLevel. The function setVerbosity
will set vbLevel which will be used in the script.

In the script I will have

value_vbLevel=$1   # User inputs " none", "low", "medium", "high" or "debug"
none=0; low=1; medium=2; high=3; debug=4

vbLevel=$medium                        # Set default verbosity
if [ "$hasArgument_vbLevel" == "true" ]; then
  setVerbosity $value_vbLevel     # In the program I would use vbLevel. Need to set vbLevel somehow

if [ $vbLevel -gt $low ]; then  # Example of use of the vbLevel as number 0-4

Here I have started doing some code, but need some help and suggestions on the way forward.

function setVerbosity() {

  # -- Check for valid verbosity level -------------------------------------------------------------

  local locl_vbLevelStr=$1

  for vb in "none" "low" "medium" "high" "debug"
    echo $vb
    if [ "$locl_vbLevelStr" == "$vb" ]; then

  # -- Set verbosity level -------------------------------------------------------------------------


  if [ "$isVbLevel" == "false" ]; then
    echo "EXIT"
    exit 1


Last edited by kristinu; 04-12-2012 at 11:45 AM..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Easy
An equilateral triangle always has every angle measuring 60°.
True or False?

10 More Discussions You Might Find Interesting

1. Programming

Computations using minimum values

I have the following code and into into trying to simplifying it. Any suggestions please? pmin = min (p(1), p(2), p(3), p(4), p(5), p(6)) ni = 0 xint = 0.0 yint = 0.0 zint = 0.0 !--------------------------------------------- ! if ((0.99999 * p(1)) <= pmin) then ... (3 Replies)
Discussion started by: kristinu
3 Replies

2. Shell Programming and Scripting

bash : Adding numeric order to blank field

Hi All, I have a file menu_opts is as follows # cat menu_opts Please select the options qqqq_usb STD RETAIL Progs (Prime time cue advertisements) qqqq_onc STD Dealer Progs (Prime time cue advertisements) qqqq_zt_usb STD RETAIL Progs (Long Format cue... (2 Replies)
Discussion started by: maverick_here
2 Replies

3. Shell Programming and Scripting

Computations on a text file

hey, I have text file which has some data for some device Id's. i want to perform AVG, MAX,2nd MAX on these device ids. The .txt file looks like below. please help me in finding the computations per Device id. My output file should contatin with DeviceID,Avg, max and 2nd max of the device ID.... (8 Replies)
Discussion started by: mahi_mayu069
8 Replies

4. Shell Programming and Scripting

Use AWK to check for numeric values? (BASH script)

Hi, I'm quite new to scripting, but know a few AWK statements. I have the following line in my script: hostname=`echo $file | awk 'BEGIN{FS=OFS="."}{$NF=""; NF--; print}'` I use this in my script to rename files, which are similar to this: name.mvkf.mkvfm.mkfvm.1 To the... (4 Replies)
Discussion started by: TauntaunHerder
4 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace random numeric value with non-numeric value

Can someone tell me how to change the first column in a very large 17k line file from a random 10 digit numeric value to a non numeric value. The format of lines in the file is: 1702938475,SNU022,201004 the first 10 numbers always begin with 170 (6 Replies)
Discussion started by: Bahf1s
6 Replies

6. Shell Programming and Scripting

Numeric or not?

Is there an easy way using a command or other routine for testing whether an argument on the command line is numeric? Just want to validate... I ran a search and didn't find a similar question.... Thx (3 Replies)
Discussion started by: harrisjl
3 Replies

7. Shell Programming and Scripting

help newb at linux and bash need numeric script sort

I am trying to setup to automatically import a series of mysql database files. I am doing manually now and its a royal pain. All the sql files are sequentially numbered in a format of 4 numbers underscore text with spaces replaced by underscores. example: There are 3 databases each setup... (1 Reply)
Discussion started by: dlm1065
1 Replies

8. Shell Programming and Scripting

Insert rows with computations of next row

Hello folks, I have data collected in every 3 hours. But, I would like to expand this to 1 hour interval by equally dividing with next row. For example, I want to keep the first value 1987-01-01-00z 2.0, but following all record should be re-written as follow. 1987-01-01-03z 5.0 becomes... (11 Replies)
Discussion started by: Jae
11 Replies

9. Shell Programming and Scripting

Perl code to differentiate numeric and non-numeric input

Hi All, Is there any code in Perl which can differentiate between numeric and non-numeric input? (11 Replies)
Discussion started by: Raynon
11 Replies

10. Shell Programming and Scripting

Floating point numeric comparisions in bash

Hi, I am trying to compare 2 floating point numbers 0.8 and 0.15 using bash and get the largest of the two. Can anyone advise. (2 Replies)
Discussion started by: borncrazy
2 Replies

Featured Tech Videos