improved readabality

This commit is contained in:
pvincent
2024-03-05 23:29:55 +04:00
parent c9fd0aca3e
commit a06a05045e
7 changed files with 33 additions and 44 deletions
View File
+12 -20
View File
@@ -3,30 +3,22 @@
## ##
# return 0 (true) if array (passed by name) contains element # return 0 (true) if array (passed by name) contains element
# usage: # usage:
# ARRAY = ( a b c ) # ARRAY=( a b c )
# containsElement ARRAY 'a' => 0 # containsElement ARRAY 'a' => 0
containsElement() { function containsElement {
local -a 'arraykeys=("${!'"$1"'[@]}")' isArray "$1" || (echo >&2 "ERROR: <$1> not an array!" && return 2)
if $(isArray $1); then array_inter="$1[@]"
for index in ${arraykeys[*]}; do array=("${!array_inter}")
current=$1"[$index]" for i in "${array[@]}"; do [[ "$i" == "$2" ]] && return 0; done
[[ "${!current}" == "$2" ]] && return 0 # found return 1
done
return 1 # not found
else
echo >&2 "ERROR: $1 not an array!"
return 2 # not an array
fi
} }
isArray() { ## return 0 (true) if arg1 (passed by name) is an array
[[ "$(declare -p $1 2>/dev/null)" =~ "declare -a" ]] && return 0 # is an array function isArray {
return 1 # not an array [[ "$(declare -p "$1" 2>/dev/null)" =~ "declare -a" ]] || return 1
} }
## function askConfirmation {
#
askConfirmation() {
case "$1" in case "$1" in
y | Y | yes | YES) y | Y | yes | YES)
QUESTION="(Y/n)?" QUESTION="(Y/n)?"
@@ -37,7 +29,7 @@ askConfirmation() {
DEFAULT=1 DEFAULT=1
;; ;;
esac esac
read -p "$QUESTION : " choice read -rp "$QUESTION : " choice
case "$choice" in case "$choice" in
y | Y | yes | YES) return 0 ;; #true y | Y | yes | YES) return 0 ;; #true
n | no | N | NO) return 1 ;; #false n | no | N | NO) return 1 ;; #false
+7 -10
View File
@@ -1,22 +1,19 @@
#!/bin/bash #!/bin/bash
usage() { function usage {
echo "Usage: $(basename "$0") <REGEX> <STRING> <FILE>" builtin echo "Usage: $(basename "$0") <REGEX> <STRING> <FILE>"
} }
if [[ $# -ne 3 ]]; then [[ $# -ne 3 ]] && usage && exit 2
usage
exit 2
fi
REGEX=$1 REGEX=$1
STRING=$2 STRING=$2
FILE=$3 FILE=$3
if ! grep -Eq "$REGEX" "$FILE"; then if ! grep -Eq "$REGEX" "$FILE"; then
printf "$STRING\n" >>$FILE printf "%s\n" "$STRING" >>"$FILE"
echo appended echo 'appended'
else else
sed -Ei "s|$REGEX|$STRING|g" $FILE sed -Ei "s|$REGEX|$STRING|g" "$FILE"
echo replaced echo 'replaced'
fi fi
+4 -4
View File
@@ -3,7 +3,7 @@
# Clears the entire current line regardless of terminal size. # Clears the entire current line regardless of terminal size.
# See the magic by running: # See the magic by running:
# { sleep 1; clear_this_line ; }& # { sleep 1; clear_this_line ; }&
clear_this_line(){ clear_this_line() {
printf '\r' printf '\r'
cols="$(tput cols)" cols="$(tput cols)"
for i in $(seq "$cols"); do for i in $(seq "$cols"); do
@@ -16,7 +16,7 @@ clear_this_line(){
# Usage: erase_lines [AMOUNT] # Usage: erase_lines [AMOUNT]
# See the magic by running: # See the magic by running:
# { sleep 1; erase_lines 2; }& # { sleep 1; erase_lines 2; }&
erase_lines(){ erase_lines() {
# Default line count to 1. # Default line count to 1.
test -z "$1" && lines="1" || lines="$1" test -z "$1" && lines="1" || lines="$1"
@@ -30,10 +30,10 @@ erase_lines(){
if [ "$lines" = 1 ]; then if [ "$lines" = 1 ]; then
clear_this_line clear_this_line
else else
lines=$((lines-1)) lines=$((lines - 1))
clear_this_line clear_this_line
for i in $(seq "$lines"); do for i in $(seq "$lines"); do
printf "$UP" buildtin echo "$UP"
clear_this_line clear_this_line
done done
fi fi
+6 -4
View File
@@ -2,11 +2,13 @@
SIZE=${1:-12} SIZE=${1:-12}
re='^[0-9]+$' re='^[0-9]+$'
if ! [[ $SIZE =~ $re ]] ;then if ! [[ $SIZE =~ $re ]]; then
echo "error: SIZE=$SIZE Not a number" >&2; exit 1 echo "error: SIZE=$SIZE Not a number" >&2
exit 1
fi fi
if [[ $SIZE -lt 4 || $SIZE -gt 20 ]]; then if [[ $SIZE -lt 4 || $SIZE -gt 20 ]]; then
echo "expected SIZE=$SIZE not in range [4..20]" >&2; exit 1 echo "expected SIZE=$SIZE not in range [4..20]" >&2
exit 1
fi fi
tr -cd '[:alnum:]' < /dev/urandom | fold -w $SIZE | head -n1 tr -cd '[:alnum:]' </dev/urandom | fold -w "$SIZE" | head -n1
+1 -1
View File
@@ -94,7 +94,7 @@ fi
COMMAND="${1:-ask}" COMMAND="${1:-ask}"
if [[ $COMMAND == 'ask' ]]; then if [[ $COMMAND == 'ask' ]]; then
echo -n "Press 'M' for Major, 'm' for minor, 'p' for patch ? " echo -n "Press 'M' for Major, 'm' for minor, 'p' for patch ? "
read -n1 input read -rn1 input
echo echo
COMMAND="-$input" COMMAND="-$input"
fi fi
+3 -5
View File
@@ -1,15 +1,14 @@
#!/bin/bash #!/bin/bash
[ `id -u` -ne 0 ] && echo 'root privilege required' && exit 1 [ "$(id -u)" -ne 0 ] && echo 'root privilege required' && exit 1
BASEDIR=$PWD BASEDIR=$PWD
[ ! $BASEDIR == '/opt/miaou-bash' ] && echo 'should be installed in /opt/miaou-bash directory' && exit 1 [ ! "$BASEDIR" == '/opt/miaou-bash' ] && echo 'should be installed in /opt/miaou-bash directory' && exit 1
ORIGINAL="ORIGINAL" ORIGINAL="ORIGINAL"
declare -a arr=(/etc/bash.bashrc /etc/inputrc /etc/vim/vimrc) declare -a arr=(/etc/bash.bashrc /etc/inputrc /etc/vim/vimrc)
for i in "${arr[@]}" for i in "${arr[@]}"; do
do
if [ -f "$i.$ORIGINAL" ]; then if [ -f "$i.$ORIGINAL" ]; then
echo "uninstalling $i" echo "uninstalling $i"
rm "$i" rm "$i"
@@ -18,4 +17,3 @@ do
echo "$i" not overriden echo "$i" not overriden
fi fi
done done