Have following line in bash script, parsing input arguments:
((10#$2 > 0)) 2>/dev/null && shift 2 || shift
Basically it helps handling parameters with optional integer subparameter. Like:
-x 100 -y
-x -y
Could you explain how it works.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHave following line in bash script, parsing input arguments:
((10#$2 > 0)) 2>/dev/null && shift 2 || shift
Basically it helps handling parameters with optional integer subparameter. Like:
-x 100 -y
-x -y
Could you explain how it works.
The line checks whether the second positional parameter is greater than 0
. If the condition is true then it shifts the positional parameters 3, 4, ... to 1, 2, ... If the condition is false then it shifts the positional parameters 2, 3, ... to 1, 2, ...
Constants with leading zero are interpreted as octal numbers. Saying 10#$2
causes the positional parameter $2
to be interpreted as a decimal. You might also want to refer to Shell Arithmetic.
As such, ((10#$2 > 0))
checks if the second positional parameter represented in base 10 is greater than 0. 2>/dev/null
causes any errors resulting from this test to be redirected to /dev/null
. See Bash Arithmetic Expressions for more on the #
operator.
&&
and ||
are conditional constructs. So if the condition is true then shift 2
is executed else shift
is executed.
expression1 && expression2
True if both expression1 and expression2 are true.
expression1 || expression2
True if either expression1 or expression2 is true.
As an example, refer to the following:
$ ((10>42)) && echo greater || echo smaller # Condition is false so the `echo smaller` expression is evaluated
smaller
$ ((100>42)) && echo greater || echo smaller # Condition is true so the `echo greater` expression is evaluated
greater
Quoting from the manual:
((...))
(( expression ))
The arithmetic expression is evaluated according to the rules described below (see Shell Arithmetic). If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to
let "expression"
10#$2
since that's the most magical part.