-2

Possible Duplicate:
How do I test if a variable is a number in bash?

I am new to bash scripts. Needed a code to check whether a variable is a number, if YES i have to keep it as it, if NO i ll have to reassign it to some other value. How do I get this done?

Community
  • 1
  • 1
user1343585
  • 649
  • 1
  • 5
  • 7

1 Answers1

0

Your basic if () then ... fi. Here's the format:

#!/bin/bash

var=2

if [[ $var != 1 ]]
then
    var=5;
fi

echo $var

Note that the [[...]] format can actually be one of many things.

As with everything, if you want to get good at it, you'll have to work for it. Type

  • man bash
  • man test

to get the manuals on the tools you're using. Read them top to bottom, again, again, and once more. And then tomorrow the same thing.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96