Shell parameter definition

Hi Folks,

I have a script called program.sh which was written by someone. I am supposed to edit it for my necessities.

There is a line in the script that is as follows

if [[ ${ABCDJ} -le 0 || ${GHIJJ} -ge 1 ]]; then
        echo -e "Option limit should be positive number and less than 1. Program aborts!"
        exit 1

What does -le and -ge means in the above code?

The main problem here is, I have to declare some parameters in decimal format but this particular format of the code doesn't let me do it and is asking for absolute values.

All helps appreciated.

Thanks

from the "test" command man page (note it operates on integers):

 n1 -le n2
                True if the integer n1 is algebraically less than
                or equal to the integer n2.


 n1 -ge n2
                True if the integer n1 is  algebraically  greater
                than or equal to the integer n2.

You may need to use the "bc" command as shown here: Floating point math operations in bash – Phoxis

1 Like

You need to use ksh93 to run this code, that is the only shell that this will work in...

1 Like

But, how do I set the parameter to take decimal values?

Any thoughts?

In ksh93 this is automatic.

$ ABCDJ=0.5
$ GHIJJ=0.75
$ if [[ ${ABCDJ} -le 0 || ${GHIJJ} -ge 1 ]]; then
> echo hello
> fi
$ 
1 Like

I tried giving the value to be 1.3.

It says

Thanks for your time #Scrutinizer

If you are using bash, or ksh93, nothing special is required:

$ cat x

#!/bin/bash

a=1.3
b=2.4

echo "$a+$b" | bc


exit 0

$ x
3.7
$

---------- Post updated at 03:08 PM ---------- Previous update was at 03:03 PM ----------

Scrutinizer's solution worked for me on ksh93. What shell are you using?

1 Like

What Operating System and version are you running?

uname -a # Blotting anything confidential with X characters
1 Like

I am using GNU Linux.

The error I am seeeing is

/path/program.sh: line 357: [[: 1.3: syntax error: invalid arithmetic operator (error token is ".3")

And as other's have asked, what Shell are you using? If it is not bash, is bash available?

BASH, and bourne shells in general, do not support floating point numbers. The only shell which does is ksh.

Interesting, it even seems to work with the bourne shell on my Solaris system. I bet there is a typo or other code validating those variables.

#!/bin/sh

a=1.3
b=-0.5

if [ $a -le 0 ] || [ $b -ge 1 ]; then
  echo Hello
else
  echo Goodbye
fi

exit 0

$ x
Goodbye
$ ## 
$ ## switch the numbers around:
$ ##
$ cat x
#!/bin/sh

a=-0.5
b=1.3

if [ $a -le 0 ] || [ $b -ge 1 ]; then
  echo Hello
else
  echo Goodbye
fi

exit 0
$ x
Hello
$
$ cat floattest2
#!/bin/sh
a=-0.5
b=1.3

if [ $a -le 0 ] || [ $b -ge 1 ]; then
  echo Hello
else
  echo Goodbye
fi


a=-0.5
b=1.3

if [ $a -lt 0 ] || [ $b -gt 1 ]; then
  echo Hello 2
else
  echo Goodbye 2
fi
$ ./floattest2
Hello
Goodbye 2

So it only seemed to working in Bourne shell on Solaris, but that is because for this shell -0.5 is equal to 0 and 1.3 is equal to 1...

1 Like

/bin/sh isn't necessarily pure bourne shell, that's all you can depend on it being if you want it to be portable though. Might it actually end up being ksh here?

No, the test was on Solaris 10, where /bin/sh is Bourne shell

2 Likes

Anybody managed to work out what Shell @jacobs.smith is using?
I can't re-create the exact error message posted.

1 Like

I tried this in bash 3:

$ [[ 1.3 -eq 1 ]]
-bash: [[: 1.3: syntax error: invalid arithmetic operator (error token is ".3")
1 Like

Just for completeness, besides ksh93 there is one more shell that can do floating point: zsh

% [[ 1.3 -gt 1.2345 ]] && echo "hello"
hello
1 Like

Doh, good catch! Good lesson! :o

1 Like

Hello Scrutinizer, thanks for your solutions.

But, how do I switch to zsh or ksh to make my code work?

I tried making the #!/bin/bash to #!/bin/sh, and still no luck. :wall::wall::wall::wall::wall::wall::wall::wall::wall::wall: