some small fixes to block module

added confirm module
master
David Kebler 2020-11-07 22:21:27 -08:00
parent 9fb8af9a09
commit 378672e974
6 changed files with 94 additions and 164 deletions

View File

@ -1,5 +0,0 @@
# GoLang
export GOROOT=/opt/go/bin
export PATH=$GOROOT/bin:$PATH
export GOPATH=/opt/go/workspace
export PATH=$GOPATH/bin:$PATH

View File

@ -8,6 +8,7 @@ BLOCK_DESCRIPTION=""
function __update_block () {
if [ ! -z "$BLOCK_FILE" ]; then
mkdir -p "$(dirname $BLOCK_FILE)"
touch $BLOCK_FILE
tail -c1 $BLOCK_FILE | read -r _ || echo >> $BLOCK_FILE
fi
@ -30,7 +31,7 @@ function set_block () {
declare OPTIND
while getopts ':f:c:d:n:' OPTION; do
echo $OPTION $OPTARG
# echo $OPTION $OPTARG
case "$OPTION" in
f)
BLOCK_FILE=$OPTARG
@ -54,14 +55,14 @@ done
shift $(( OPTIND - 1 ))
__update_block
echo -e "new block template follows use: block-add >\n" $BLOCK
echo ----
# echo -e "new block template follows use: block-add >\n" $BLOCK
# echo ----
echo $([ -e "$BLOCK_FILE" ] || echo "Warning: No output file set yet, use block-set -f <path>" && echo "block will be placed in $BLOCK_FILE")
echo -------
# echo -------
}
function add_block () {
block_remove_content
# only add if it doesn't exist, existance is only based on block begin
local ADD
ADD=$(echo "${BLOCK}" | sed 's/\//\\\//g')
@ -71,6 +72,7 @@ sed -n '/^[ \t]*'"${BLOCK_BEGIN}"'/{q50}' "${BLOCK_FILE}"
WHERE=${1-${BLOCK_BEGIN}} # default is end
# echo Where $WHERE
sed -i '/'"${WHERE}"'/{s/.*/&\n'"${ADD}"'/;h};${x;/^$/{s//'"$ADD"'/;H};x}' "${BLOCK_FILE}"
[[ $1 ]] && block_add_line $1
else
echo "no file set in which to add block, use: ${RED_COLOR}set-block -f <file path>"
fi
@ -112,7 +114,6 @@ remove_block () {
}' "${BLOCK_FILE}"
}
block_remove_content () {
sed -i '/^'"${BLOCK_BEGIN}"'/,/^'"${BLOCK_END}"$'/ {
/'"${BLOCK_BEGIN}"'/b
@ -121,7 +122,6 @@ block_remove_content () {
}' "${BLOCK_FILE}"
}
block_add_line () {
# echo argument count $#, $1, $2, $3
local ADD

View File

@ -1,141 +0,0 @@
#!/usr/bin/env bash
# shebang for syntax detection, not a command
# do *not* set executable!
declare -Ag _modules_
shopt -s expand_aliases
alias _args_='(( $# ))'
alias _to_args_='set --'
module.already_loaded () {
declare -Ag _modules_
[[ -v _modules_[${BASH_SOURCE[1]}] ]]
}
_blank_ () {
[[ -z ${1:-} ]]
}
_defined_ () {
[[ -v $1 ]]
}
_eq_ () {
[[ $1 == "${2:-}" ]]
}
_functions_ () {
env -i bash <<END
shopt -s expand_aliases
alias source=:
\\source $1 &>/dev/null
compgen -A function;:
END
}
_in_ () {
[[ $IFS$1$IFS == *"$IFS$2$IFS"* ]]
}
_pop_ () {
local -n ref1=$1
local ref2=${2:-}
_present_ $ref2 && printf -v $ref2 %s "${ref1[-1]}"
unset -v $1[-1]
}
_present_ () {
[[ -n ${1:-} ]]
}
_push_ () {
local -n ref=$1
ref+=( "$2" )
}
_parent_=${BASH_SOURCE[1]:-}
[[ ${1:-} != module ]] && return
set -- ${*:2}
_numargs_+=( $# )
{ ! _args_ && _present_ ${_file_:-} && _eq_ $_parent_ $_file_ ;} && return
{ ! _args_ && _present_ $_parent_ && ! _eq_ ${_parent_##*/} module ;} && {
_in_ "${_ancestors_[*]:-}" $_parent_ && return
_push_ _ancestors_ $_parent_
}
! _args_ && _to_args_ $_parent_
for _file_; do
_module_=${_file_%%=*}
_file_=${_file_#*=}
_defined_ _modules_[$_file_] && continue
_eq_ $_module_ $_file_ && {
_module_=${_module_##*/}
_module_=${_module_%.*}
}
_prefixes_+=( $_module_ )
_push_ _aliases_ "$(alias)"
unalias -a
_functions_=$(_functions_ $_file_)
for _function_ in $_functions_; do
printf -v _prefix_ %s. ${_prefixes_[*]}
alias $_function_=$_prefix_$_function_
done
_push_ _files_ $_file_
_push_ _funcs_ "$_functions_"
set --
source $_file_
unset -v _prefixes_[-1]
_pop_ _files_ _file_
_pop_ _funcs_ _functions_
_modules_[$_file_]=''
for _function_ in $_functions_; do
unalias $_function_
done
eval "${_aliases_[-1]}"
_pop_ _aliases_
done
! (( ${#_files_[*]} )) && {
unalias _args_
unalias _to_args_
unset -f _blank_
unset -f _defined_
unset -f _eq_
unset -f _in_
unset -f _pop_
unset -f _present_
unset -f _push_
unset -v _file_
unset -v _funcs_
unset -v _function_
unset -v _functions_
unset -v _module_
unset -v _name_
unset -v _parent_
unset -v _prefix_
unset -v _prefixes_
}
{ ! (( _numargs_[-1] )) && [[ -n ${BASH_SOURCE[1]} && ${BASH_SOURCE[1]##*/} != module ]] ;} && unset -v _ancestors_[-1]
unset -v _numargs_[-1]

View File

@ -1,11 +0,0 @@
#!/bin/bash
# must have fuser and bindfs installed
module-load () {
BASH_MODULES="${BASH_MODULES:-/opt/bash/modules}"
MODULES=$1
echo $MODULES
for i in ${MODULES[@]}; do
echo $BASH_MODULES ${i}
. "$BASH_MODULES"/"$i".sh
done
}

View File

@ -0,0 +1,47 @@
# ======================================================================
#
# @link http://wuhrr.wordpress.com/2010/01/13/adding-confirmation-to-bash/#comment-3540
#
# Function: confirm
# Asks the user to confirm an action, If the user does not answer yes,
# then the script will immediately exit.
#
# Parameters:
# $@ - The confirmation message
#
# Examples:
# > # Example 1
# > # The preferred way to use confirm
# > confirm Delete file1? && echo rm file1
# >
# > # Example 2
# > # Use the $? variable to examine confirm's return value
# > confirm Delete file2?
# > if [ $? -eq 0 ]
# > then
# > echo Another file deleted
# > fi
# >
# > # Example 3
# > # Tell bash to exit right away if any command returns a non-zero code
# > set -o errexit
# > confirm Do you want to run the rest of the script?
# > echo Here is the rest of the script
#
# ======================================================================
function confirm()
{
echo -n "$@ "
read -e answer
for response in y Y yes YES Yes Sure sure SURE OK ok Ok
do
if [ "_$answer" == "_$response" ]
then
return 0
fi
done
# Any answer other than the list above is considered a "no" answer
return 1
}

View File

@ -0,0 +1,40 @@
#!/bin/bash
function get_platform () {
local OS
local ARCH
local PLATFORM
OS="$(uname -s)"
ARCH="$(uname -m)"
case $OS in
"Linux")
case $ARCH in
"x86_64")
ARCH=amd64
;;
"aarch64")
ARCH=arm64
;;
"armv6")
ARCH=armv6l
;;
"armv8")
ARCH=arm64
;;
.*386.*)
ARCH=386
;;
esac
PLATFORM="linux-$ARCH"
;;
"Darwin")
PLATFORM="darwin-amd64"
;;
esac
echo $PLATFORM
}