Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- assign-path()
- {
- str=$1
- # if the $PATH is empty, assign it directly.
- if [ -z $PATH ]; then
- PATH=$str;
- # if the $PATH does not contain the substring, append it with ':'.
- elif [[ $PATH != *$str* ]]; then
- PATH=$PATH:$str;
- fi
- }
- assign( bigstr, substr )
- {
- if [ -z bigstr ]; then
- bigstr=substr;
- elif [[ bigstr != *str* ]]; then
- bigstr=bigstr:substr;
- fi
- }
- $ var=hello
- $ foo () { echo "${!1}"; }
- $ foo var
- hello
- $ var=target
- $ declare "$var=hello"
- $ echo "$target"
- hello
- shopt -s extglob
- assign()
- {
- target=$1
- bigstr=${!1}
- substr=$2
- if [ -z "$bigstr" ]; then
- declare -g -- "$target=$substr"
- elif [[ $bigstr != @(|*:)$substr@(|:*) ]]; then
- declare -g -- "$target=$bigstr:$substr"
- fi
- }
- assign PATH /path/to/binaries
- [[ $bigstr != $substr && $bigstr != *:$substr &&
- $bigstr != $substr:* && $bigstr != *:$substr:* ]]
- eval
- eval
- eval
- augment
- augment
- v='echo "OHNO!" ; var' ; l=val ; eval $v='$l'
- assign ()
- {
- if [ -z ${!1} ]; then
- eval $1=$2
- else
- if [[ ${!1} != *$2* ]]; then
- eval $1=${!1}:$2
- fi
- fi
- }
- $ echo =$x=
- ==
- $ assign x y
- $ echo =$x=
- =y=
- $ assign x y
- $ echo =$x=
- =y=
- $ assign x z
- $ echo =$x=
- =y:z=
- : ${PATH:=this is only assigned to $PATH if $PATH is null or unset}
- _fn() { echo "$1 ${2:-"$1"} $str" ; }
- % str= ; _fn "${str:=hello}"
- > hello hello hello
- assign() { oFS=$IFS ; IFS=: ; add=$*
- set -- $PATH ; for p in $add ; do {
- for d ; do [ -z "${d%"$p"}" ] && break
- done ; } || set -- $* $p ; done
- PATH= ; echo "${PATH:="$*"}" ; IFS=$oFS
- }
- % PATH=/usr/bin:/usr/yes/bin
- % assign
- /usr/bin
- /usr/yes/bin
- /usr/nope/bin
- /usr/bin
- /nope/usr/bin
- /usr/nope/bin
- > /usr/bin:/usr/yes/bin:/usr/nope/bin:/nope/usr/bin
- % echo "$PATH"
- > /usr/bin:/usr/yes/bin:/usr/nope/bin:/nope/usr/bin
- % dir="/some crazy/dir"
- % p=`assign /usr/bin /usr/bin/new "$dir"`
- % echo "$p" ; echo "$PATH"
- > /usr/bin:/usr/yes/bin:/usr/nope/bin:/nope/usr/bin:/some crazy/dir:/usr/bin/new
- > /usr/bin:/usr/yes/bin:/usr/nope/bin:/nope/usr/bin:/some crazy/dir:/usr/bin/new
- testPassingParams() {
- @var hello
- l=4 @array anArrayWithFourElements
- l=2 @array anotherArrayWithTwo
- @var anotherSingle
- @reference table # references only work in bash >=4.3
- @params anArrayOfVariedSize
- test "$hello" = "$1" && echo correct
- #
- test "${anArrayWithFourElements[0]}" = "$2" && echo correct
- test "${anArrayWithFourElements[1]}" = "$3" && echo correct
- test "${anArrayWithFourElements[2]}" = "$4" && echo correct
- # etc...
- #
- test "${anotherArrayWithTwo[0]}" = "$6" && echo correct
- test "${anotherArrayWithTwo[1]}" = "$7" && echo correct
- #
- test "$anotherSingle" = "$8" && echo correct
- #
- test "${table[test]}" = "works"
- table[inside]="adding a new value"
- #
- # I'm using * just in this example:
- test "${anArrayOfVariedSize[*]}" = "${*:10}" && echo correct
- }
- fourElements=( a1 a2 "a3 with spaces" a4 )
- twoElements=( b1 b2 )
- declare -A assocArray
- assocArray[test]="works"
- testPassingParams "first" "${fourElements[@]}" "${twoElements[@]}" "single with spaces" assocArray "and more... " "even more..."
- test "${assocArray[inside]}" = "adding a new value"
- Function.AssignParamLocally() {
- local commandWithArgs=( $1 )
- local command="${commandWithArgs[0]}"
- shift
- if [[ "$command" == "trap" || "$command" == "l="* || "$command" == "_type="* ]]
- then
- paramNo+=-1
- return 0
- fi
- if [[ "$command" != "local" ]]
- then
- assignNormalCodeStarted=true
- fi
- local varDeclaration="${commandWithArgs[1]}"
- if [[ $varDeclaration == '-n' ]]
- then
- varDeclaration="${commandWithArgs[2]}"
- fi
- local varName="${varDeclaration%%=*}"
- # var value is only important if making an object later on from it
- local varValue="${varDeclaration#*=}"
- if [[ ! -z $assignVarType ]]
- then
- local previousParamNo=$(expr $paramNo - 1)
- if [[ "$assignVarType" == "array" ]]
- then
- # passing array:
- execute="$assignVarName=( "${@:$previousParamNo:$assignArrLength}" )"
- eval "$execute"
- paramNo+=$(expr $assignArrLength - 1)
- unset assignArrLength
- elif [[ "$assignVarType" == "params" ]]
- then
- execute="$assignVarName=( "${@:$previousParamNo}" )"
- eval "$execute"
- elif [[ "$assignVarType" == "reference" ]]
- then
- execute="$assignVarName="$$previousParamNo""
- eval "$execute"
- elif [[ ! -z "${!previousParamNo}" ]]
- then
- execute="$assignVarName="$$previousParamNo""
- eval "$execute"
- fi
- fi
- assignVarType="$__capture_type"
- assignVarName="$varName"
- assignArrLength="$__capture_arrLength"
- }
- Function.CaptureParams() {
- __capture_type="$_type"
- __capture_arrLength="$l"
- }
- alias @trapAssign='Function.CaptureParams; trap "declare -i "paramNo+=1"; Function.AssignParamLocally "$BASH_COMMAND" "$@"; [[ $assignNormalCodeStarted = true ]] && trap - DEBUG && unset assignVarType && unset assignVarName && unset assignNormalCodeStarted && unset paramNo" DEBUG; '
- alias @param='@trapAssign local'
- alias @reference='_type=reference @trapAssign local -n'
- alias @var='_type=var @param'
- alias @params='_type=params @param'
- alias @array='_type=array @param'
- function foo(){
- # $1 is first param
- # $2 is second param ...
- }
- result=`foo "param1" "param2" ...`
- # they will go the right way.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement