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