You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

43 regels
949 B

  1. # common/.profile.d/20-functions-path.sh
  2. # cat out a : separated env variable
  3. # variable is the parameter
  4. cat_env()
  5. {
  6. set | grep '^'"$1"'=' > /dev/null 2>&1 && eval "echo \$$1" | tr ':' '
  7. ' | awk '!/^\s+$/' | awk '!/^$/'
  8. }
  9. # Convert a line delimited input to : delimited
  10. to_env()
  11. {
  12. awk '!a[$0]++' < /dev/stdin | tr -s '
  13. ' ':' | sed -e 's/\:$//' | awk 'NF > 0'
  14. }
  15. # Unshift a new value onto the env var
  16. # first arg is ENV second is value to unshift
  17. # basically prepend value to the ENV variable
  18. unshift_env()
  19. {
  20. new=$(eval "echo $2; echo \$$1" | to_env)
  21. eval "$1=${new}; export $1"
  22. }
  23. # Opposite of above, but echos what was shifted off
  24. shift_env()
  25. {
  26. first=$(cat_env "$1" | head -n 1)
  27. rest=$(cat_env "$1" | awk '{if (NR!=1) {print}}' | to_env)
  28. eval "$1=$rest; export $1"
  29. echo "${first}"
  30. }
  31. # push $2 to $1 on the variable
  32. push_env()
  33. {
  34. have=$(cat_env "$1")
  35. new=$(printf "%s
  36. %s" "$have" "$2" | to_env)
  37. eval "$1=$new; export $1"
  38. }