make local variables local

.. to not interfere with variables in the scripts that source these utils.
This commit is contained in:
root
2025-02-03 11:12:05 +01:00
parent 204162a747
commit 4c60127786
5 changed files with 18 additions and 13 deletions

View File

@@ -13,13 +13,13 @@ loggerseparator () {
} }
saferun () { saferun () {
allcommands="" local allcommands=""
for var in "$@"; do for var in "$@"; do
allcommands="$allcommands$var /// " allcommands="$allcommands$var /// "
done done
logger "Enter to run the following command(s): $allcommands" logger "Enter to run the following command(s): $allcommands"
read ok read ok
ret=0 local ret=0
for var in "$@"; do for var in "$@"; do
if [[ $ret -eq 0 ]]; then if [[ $ret -eq 0 ]]; then
$var $var

View File

@@ -2,12 +2,12 @@
mailer () { mailer () {
if ! [[ -z "$3" ]]; then if ! [[ -z "$3" ]]; then
mailfrom="From: $3 <christian@betz.cc>" local mailfrom="From: $3 <christian@betz.cc>"
fi fi
if ! [[ -z "$4" ]]; then if ! [[ -z "$4" ]]; then
mailto="$4" local mailto="$4"
elif [[ -z "$mailto" ]]; then elif [[ -z "$mailto" ]]; then
mailto="christian@betz.cc" local mailto="christian@betz.cc"
fi fi
echo -e "$1" | mailx -s "$2" --append="$mailfrom" $mailto &>/dev/null echo -e "$1" | mailx -s "$2" --append="$mailfrom" $mailto &>/dev/null

17
math.sh
View File

@@ -8,6 +8,11 @@ SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# https://unix.stackexchange.com/questions/89712/how-to-convert-floating-point-number-to-integer # https://unix.stackexchange.com/questions/89712/how-to-convert-floating-point-number-to-integer
# xargs: https://stackoverflow.com/questions/19148370/piping-seq-to-printf-for-number-formatting # xargs: https://stackoverflow.com/questions/19148370/piping-seq-to-printf-for-number-formatting
divideint () { divideint () {
if [ -z $3 ]; then
local scale=2
else
local scale=$3
fi
bc <<< "scale=$scale; $1/$2" | xargs printf "%.0f\n" bc <<< "scale=$scale; $1/$2" | xargs printf "%.0f\n"
} }
@@ -17,25 +22,25 @@ multiint () {
divide () { divide () {
if [ -z $3 ]; then if [ -z $3 ]; then
scale=2 local scale=2
else else
scale=$3 local scale=$3
fi fi
bc <<< "scale=$scale; $1/$2" bc <<< "scale=$scale; $1/$2"
} }
multiply () { multiply () {
if [ -z $3 ]; then if [ -z $3 ]; then
scale=2 local scale=2
else else
scale=$3 local scale=$3
fi fi
bc <<< "scale=$scale; $1*$2" bc <<< "scale=$scale; $1*$2"
} }
avgoftwo () { avgoftwo () {
sum=$(bc <<< "$1+$2") local sum=$(bc <<< "$1+$2")
divideint $sum 2 divide $sum 2
} }
max () { max () {

View File

@@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
isarray() { isarray() {
var=$1 local var=$1
declare -p $1 2> /dev/null | grep -q '^declare \-a' && return 0 declare -p $1 2> /dev/null | grep -q '^declare \-a' && return 0
[[ ${var[0]} -eq ${var[0]} ]] && [[ ${var[1]} -eq ${var[1]} ]] && return 0 || return 1 [[ ${var[0]} -eq ${var[0]} ]] && [[ ${var[1]} -eq ${var[1]} ]] && return 0 || return 1
} }

View File

@@ -2,7 +2,7 @@
# https://stackoverflow.com/questions/13777387/check-for-ip-validity # https://stackoverflow.com/questions/13777387/check-for-ip-validity
function validip () { function validip () {
ip=$1 local ip=$1
if [ -z $ip ] || ! [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then if [ -z $ip ] || ! [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
return 1 return 1
else else