Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. assign-path()
  2. {
  3. str=$1
  4. # if the $PATH is empty, assign it directly.
  5. if [ -z $PATH ]; then
  6. PATH=$str;
  7. # if the $PATH does not contain the substring, append it with ':'.
  8. elif [[ $PATH != *$str* ]]; then
  9. PATH=$PATH:$str;
  10. fi
  11. }
  12.  
  13. assign( bigstr, substr )
  14. {
  15. if [ -z bigstr ]; then
  16. bigstr=substr;
  17. elif [[ bigstr != *str* ]]; then
  18. bigstr=bigstr:substr;
  19. fi
  20. }
  21.  
  22. $ var=hello
  23. $ foo () { echo "${!1}"; }
  24. $ foo var
  25. hello
  26.  
  27. $ var=target
  28. $ declare "$var=hello"
  29. $ echo "$target"
  30. hello
  31.  
  32. shopt -s extglob
  33.  
  34. assign()
  35. {
  36. target=$1
  37. bigstr=${!1}
  38. substr=$2
  39.  
  40. if [ -z "$bigstr" ]; then
  41. declare -g -- "$target=$substr"
  42. elif [[ $bigstr != @(|*:)$substr@(|:*) ]]; then
  43. declare -g -- "$target=$bigstr:$substr"
  44. fi
  45. }
  46.  
  47. assign PATH /path/to/binaries
  48.  
  49. [[ $bigstr != $substr && $bigstr != *:$substr &&
  50. $bigstr != $substr:* && $bigstr != *:$substr:* ]]
  51.  
  52. eval
  53.  
  54. eval
  55.  
  56. eval
  57.  
  58. augment
  59.  
  60. augment
  61.  
  62. v='echo "OHNO!" ; var' ; l=val ; eval $v='$l'
  63.  
  64. assign ()
  65. {
  66. if [ -z ${!1} ]; then
  67. eval $1=$2
  68. else
  69. if [[ ${!1} != *$2* ]]; then
  70. eval $1=${!1}:$2
  71. fi
  72. fi
  73. }
  74.  
  75. $ echo =$x=
  76. ==
  77. $ assign x y
  78. $ echo =$x=
  79. =y=
  80. $ assign x y
  81. $ echo =$x=
  82. =y=
  83. $ assign x z
  84. $ echo =$x=
  85. =y:z=
  86.  
  87. : ${PATH:=this is only assigned to $PATH if $PATH is null or unset}
  88.  
  89. _fn() { echo "$1 ${2:-"$1"} $str" ; }
  90.  
  91. % str= ; _fn "${str:=hello}"
  92. > hello hello hello
  93.  
  94. assign() { oFS=$IFS ; IFS=: ; add=$*
  95. set -- $PATH ; for p in $add ; do {
  96. for d ; do [ -z "${d%"$p"}" ] && break
  97. done ; } || set -- $* $p ; done
  98. PATH= ; echo "${PATH:="$*"}" ; IFS=$oFS
  99. }
  100.  
  101. % PATH=/usr/bin:/usr/yes/bin
  102. % assign
  103. /usr/bin
  104. /usr/yes/bin
  105. /usr/nope/bin
  106. /usr/bin
  107. /nope/usr/bin
  108. /usr/nope/bin
  109.  
  110. > /usr/bin:/usr/yes/bin:/usr/nope/bin:/nope/usr/bin
  111.  
  112. % echo "$PATH"
  113. > /usr/bin:/usr/yes/bin:/usr/nope/bin:/nope/usr/bin
  114.  
  115. % dir="/some crazy/dir"
  116. % p=`assign /usr/bin /usr/bin/new "$dir"`
  117. % echo "$p" ; echo "$PATH"
  118. > /usr/bin:/usr/yes/bin:/usr/nope/bin:/nope/usr/bin:/some crazy/dir:/usr/bin/new
  119. > /usr/bin:/usr/yes/bin:/usr/nope/bin:/nope/usr/bin:/some crazy/dir:/usr/bin/new
  120.  
  121. testPassingParams() {
  122.  
  123. @var hello
  124. l=4 @array anArrayWithFourElements
  125. l=2 @array anotherArrayWithTwo
  126. @var anotherSingle
  127. @reference table # references only work in bash >=4.3
  128. @params anArrayOfVariedSize
  129.  
  130. test "$hello" = "$1" && echo correct
  131. #
  132. test "${anArrayWithFourElements[0]}" = "$2" && echo correct
  133. test "${anArrayWithFourElements[1]}" = "$3" && echo correct
  134. test "${anArrayWithFourElements[2]}" = "$4" && echo correct
  135. # etc...
  136. #
  137. test "${anotherArrayWithTwo[0]}" = "$6" && echo correct
  138. test "${anotherArrayWithTwo[1]}" = "$7" && echo correct
  139. #
  140. test "$anotherSingle" = "$8" && echo correct
  141. #
  142. test "${table[test]}" = "works"
  143. table[inside]="adding a new value"
  144. #
  145. # I'm using * just in this example:
  146. test "${anArrayOfVariedSize[*]}" = "${*:10}" && echo correct
  147. }
  148.  
  149. fourElements=( a1 a2 "a3 with spaces" a4 )
  150. twoElements=( b1 b2 )
  151. declare -A assocArray
  152. assocArray[test]="works"
  153.  
  154. testPassingParams "first" "${fourElements[@]}" "${twoElements[@]}" "single with spaces" assocArray "and more... " "even more..."
  155.  
  156. test "${assocArray[inside]}" = "adding a new value"
  157.  
  158. Function.AssignParamLocally() {
  159. local commandWithArgs=( $1 )
  160. local command="${commandWithArgs[0]}"
  161.  
  162. shift
  163.  
  164. if [[ "$command" == "trap" || "$command" == "l="* || "$command" == "_type="* ]]
  165. then
  166. paramNo+=-1
  167. return 0
  168. fi
  169.  
  170. if [[ "$command" != "local" ]]
  171. then
  172. assignNormalCodeStarted=true
  173. fi
  174.  
  175. local varDeclaration="${commandWithArgs[1]}"
  176. if [[ $varDeclaration == '-n' ]]
  177. then
  178. varDeclaration="${commandWithArgs[2]}"
  179. fi
  180. local varName="${varDeclaration%%=*}"
  181.  
  182. # var value is only important if making an object later on from it
  183. local varValue="${varDeclaration#*=}"
  184.  
  185. if [[ ! -z $assignVarType ]]
  186. then
  187. local previousParamNo=$(expr $paramNo - 1)
  188.  
  189. if [[ "$assignVarType" == "array" ]]
  190. then
  191. # passing array:
  192. execute="$assignVarName=( "${@:$previousParamNo:$assignArrLength}" )"
  193. eval "$execute"
  194. paramNo+=$(expr $assignArrLength - 1)
  195.  
  196. unset assignArrLength
  197. elif [[ "$assignVarType" == "params" ]]
  198. then
  199. execute="$assignVarName=( "${@:$previousParamNo}" )"
  200. eval "$execute"
  201. elif [[ "$assignVarType" == "reference" ]]
  202. then
  203. execute="$assignVarName="$$previousParamNo""
  204. eval "$execute"
  205. elif [[ ! -z "${!previousParamNo}" ]]
  206. then
  207. execute="$assignVarName="$$previousParamNo""
  208. eval "$execute"
  209. fi
  210. fi
  211.  
  212. assignVarType="$__capture_type"
  213. assignVarName="$varName"
  214. assignArrLength="$__capture_arrLength"
  215. }
  216.  
  217. Function.CaptureParams() {
  218. __capture_type="$_type"
  219. __capture_arrLength="$l"
  220. }
  221.  
  222. 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; '
  223. alias @param='@trapAssign local'
  224. alias @reference='_type=reference @trapAssign local -n'
  225. alias @var='_type=var @param'
  226. alias @params='_type=params @param'
  227. alias @array='_type=array @param'
  228.  
  229. function foo(){
  230. # $1 is first param
  231. # $2 is second param ...
  232. }
  233.  
  234. result=`foo "param1" "param2" ...`
  235. # they will go the right way.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement