sh Linux.Bash.Numbers.Mathematics
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh Linux.Bash.Numbers.Mathematics相关的知识,希望对你有一定的参考价值。
#!/bin/bash
trim () {
str="$1"
match="$2"
# trim spaces by default
if [ -z "$match" ]; then
match=" "
fi
# trim leading
while [ "${str:0:${#match}}" == "$match" ];
do
str="${str:${#match}:${#str}}"
done
# trim tailing
while [ "${str:$((${#str}-${#match}))}" == "$match" ];
do
str="${str:0:$((${#str} - ${#match}))}"
done
echo "$str"
}
#Remove leading and trailing spaces
example=" Hello "
echo "`trim "$example" " "`"
#The second parameter is optional when removing spaces
example=" Hello "
echo "`trim "$example"`"
#Remove leading and trailing occurences of "word"
example="wordHelloword"
echo "`trim "$example" "word"`"
_float2fraction() {
[ -z "${1}" ] && return 1
_float2fraction__num="${1%%.*}"
_float2fraction__den="${1##*.}"
[ "${_float2fraction__num}" ] && [ "${_float2fraction__num}" -eq "0" ] && _float2fraction__num=""
_float2fraction__num="${_float2fraction__num}${_float2fraction__den}"
_float2fraction__zero="${#_float2fraction__den}"
_float2fraction__den="1"
#checking if denominator or numerator is greater
while [ "${_float2fraction__zero}" -gt "0" ]; do
_float2fraction__den="${_float2fraction__den}0"
_float2fraction__zero="$((${_float2fraction__zero}-1))"
done
if [ "${_float2fraction__num}" -gt "${_float2fraction__den}" ];then
_float2fraction__greater="${_float2fraction__num}"
_float2fraction__lower="${_float2fraction__den}"
else
_float2fraction__greater="${_float2fraction__den}"
_float2fraction__lower="${_float2fraction__num}"
fi #finding hcf
while [ "${_float2fraction__lower}" -ne "0" ];do
_float2fraction__hcf="${_float2fraction__lower}"
_float2fraction__lower=$((${_float2fraction__greater}%${_float2fraction__lower}))
_float2fraction__greater="${_float2fraction__hcf}"
done #dividing numerator and denominator by hcf
_float2fraction__num="$((${_float2fraction__num}/${_float2fraction__hcf}))"
_float2fraction__den="$((${_float2fraction__den}/${_float2fraction__hcf}))" #answer
printf "%s\\n" "${_float2fraction__num}/${_float2fraction__den}"
}
#==================================================== Example ====
#_float2fraction "0.5" #prints "1/2"
#_float2fraction "0.75" #prints "3/4"
#=================================================================
_float2fraction "0.5"
_float2fraction "0.75"
#!/bin/bash
# Find the greater from numbers given as arguments
FindGreater() {
#FindGreater 0 1 0 2 3 444 333 100 0 0
local numbers=( "${@}" ) # Initialize the array with given arguments
local lenght="${#numbers[@]}" # Get the number of elem in array
local num="0"
while (( num < "${lenght}" )) ; do
if [[ "${numbers[$num]}" -ge "${greater:-0}" ]] ; then
local greater="${numbers[$num]}"
fi
((num++))
done
echo "Greater is : \"${greater}\""
}
bash_factorial()
{
# Function : bash_factorial
# Description : Calculates factorial up to counter
# Input : Counter, max factor
# Output : Factorial
# Usage : bash_factorial $x
counter=$1
factorial=1
while [ $counter -gt 0 ]
do
factorial=$(($factorial * $counter))
counter=$(( $counter - 1 ))
done
echo "The factorial is $factorial"
}
bash_sumofparts()
{
# Function : bash_sumofparts
# Description : Calculates sum of integers up to counter
# Input : Counter
# Output : Sum of parts
# Usage : bash_sumofparts $x
counter=$1
sumofparts=0
while [ $counter -gt 0 ]
do
sumofparts=$(($sumofparts + $counter))
counter=$(( $counter - 1 ))
done
echo "The sumofparts is $sumofparts"
}
1. bash_factorial : Calculates facotiral up to counter
2. bash_sumofparts : Calculates sum of parts up to counter
3. float_to_fraction : Converts a float number into a fraction
4. _random() : Prints a random number between 2 limits
5. Find_Greater_Number : Finds the greater number in the array
6. String_trim : Trims specified characters from a string
7. absolute_value : Absolute value of a number
abs () # Absolute value.
{ # Caution: Max return value = 255.
E_ARGERR=-999999
if [ -z "$1" ] # Need arg passed.
then
return $E_ARGERR # Obvious error value returned.
fi
if [ "$1" -ge 0 ] # If non-negative,
then #
absval=$1 # stays as-is.
else # Otherwise,
let "absval = (( 0 - $1 ))" # change sign.
fi
return $absval
}
_random() { #return a random number between two limits
#default to numbers between 1-10
_random__min="${1:-1}"
_random__max="${2:-10}"
_random__awk_prog='BEGIN {srand(); print int(MIN+rand()*(MAX-MIN+1))}'
awk -v MIN="${_random__min}" -v MAX="${_random__max}" "${_random__awk_prog}" < /dev/null
}
#==================================================== Example ====
printf "%s\\n" "$(_random)" "prints random numbers between 1 and 10"
printf "%s\\n" "$(_random 1 5)" "prints random numbers between 1 and 5"
#=================================================================
616 _is_int() { #look for an integer, returns 0 on success, 1 otherwise
617 #http://www.unix.com/shell-programming-and-scripting/172070-help-scripting-command.html
618 case "${1}" in
619 *[!0-9]*|"") return 1 ;;
620 esac
621 }
622 #==================================================== Example ====
623 #printf "%s" "Enter input: "
624 #read input
625 #if ! _is_int "${input}" ; then
626 #printf "%s\\n" "Your input must consist of only numbers." >&2
627 #exit 1
628 #else
629 #printf "%s\\n" "Input is valid."
630 #fi
631 #exit 0
632 #=================================================================
633
947 _seq() { #portable seq
948 for _seq__arg; do
949 case "${_seq__arg}" in
950 *[!0-9-]*|"") return 1 ;;
951 esac
952 done
953
954 [ -z "${1}" ] && return 1
955
956 _seq__first="1"; _seq__increment="1"; _seq__last="${1}"
957
958 if [ "${#2}" -gt "0" ]; then
959 _seq__first="${1}"
960 _seq__last="${2}"
961 fi
962
963 if [ "${#3}" -gt "0" ]; then
964 _seq__first="${1}"
965 _seq__increment="${2}"
966 _seq__last="${3}"
967 fi
968
969 while [ "$_seq__first" -le "${_seq__last}" ]; do
970 printf "%s\\n" "${_seq__first}"
971 _seq__first="$((_seq__first + _seq__increment))"
972 done
973 }
974 #==================================================== Example ====
975 #_seq 3
976 #1
977 #2
978 #3
979 #=================================================================
980
以上是关于sh Linux.Bash.Numbers.Mathematics的主要内容,如果未能解决你的问题,请参考以下文章
配置告警系统主脚本main.sh mon.sh load.sh 502.sh disk.sh