[1/6] util: new function abs

Submitted by Jonatan Schlag on June 9, 2017, 10:17 a.m.

Details

Message ID 1497003452-10190-1-git-send-email-jonatan.schlag@ipfire.org
State New
Headers show

Commit Message

Jonatan Schlag June 9, 2017, 10:17 a.m.
This function return the absolute value of a given number.

Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
---
 src/functions/functions.util | 10 ++++++++++
 1 file changed, 10 insertions(+)

Patch hide | download patch | download mbox

diff --git a/src/functions/functions.util b/src/functions/functions.util
index 4b6f956..699026d 100644
--- a/src/functions/functions.util
+++ b/src/functions/functions.util
@@ -268,6 +268,16 @@  uuid() {
 	echo $(</proc/sys/kernel/random/uuid)
 }
 
+abs() {
+	local val=${1}
+
+	if [ ${val} -lt 0 ]; then
+		(( val *= -1 ))
+	fi
+
+	echo ${val}
+}
+
 rand() {
 	local uuid="$(uuid)"
 	echo "${uuid//-/}"

Comments

Michael Tremer June 14, 2017, 9:22 p.m.
Merged. :)

On Fri, 2017-06-09 at 12:17 +0200, Jonatan Schlag wrote:
> This function return the absolute value of a given number.
> 
> Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
> ---
>  src/functions/functions.util | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/src/functions/functions.util
> b/src/functions/functions.util
> index 4b6f956..699026d 100644
> --- a/src/functions/functions.util
> +++ b/src/functions/functions.util
> @@ -268,6 +268,16 @@ uuid() {
>  	echo $(</proc/sys/kernel/random/uuid)
>  }
>  
> +abs() {
> +	local val=${1}
> +
> +	if [ ${val} -lt 0 ]; then
> +		(( val *= -1 ))
> +	fi
> +
> +	echo ${val}
> +}
> +
>  rand() {
>  	local uuid="$(uuid)"
>  	echo "${uuid//-/}"