Guest User

Untitled

a guest
Mar 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. if [ -z "$STATE" ]; then
  2. echo "Need to set STATE"
  3. exit 1
  4. fi
  5.  
  6. if [ -z "$DEST" ]; then
  7. echo "Need to set DEST"
  8. exit 1
  9. fi
  10.  
  11. : ${STATE?"Need to set STATE"}
  12. : ${DEST:?"Need to set DEST non-empty"}
  13.  
  14. : "${STATE?Need to set STATE}"
  15. : "${DEST:?Need to set DEST non-empty}"
  16.  
  17. ${parameter:?word}
  18.  
  19. In script.sh line 13:
  20. : ${FOO:?"The environment variable 'FOO' must be set and non-empty"}
  21. ^-- SC2086: Double quote to prevent globbing and word splitting.
  22.  
  23. : "${STATE?Need to set STATE}"
  24. : "${DEST:?Need to set DEST non-empty}"
  25.  
  26. $ mkdir junk
  27. $ cd junk
  28. $ > abc
  29. $ > def
  30. $ > ghi
  31. $
  32. $ x="*"
  33. $ printf "%sn" ${x:?You must set x} # Careless; not recommended
  34. abc
  35. def
  36. ghi
  37. $ unset x
  38. $ printf "%sn" ${x:?You must set x} # Careless; not recommended
  39. bash: x: You must set x
  40. $ printf "%sn" "${x:?You must set x}" # Careful: should be used
  41. bash: x: You must set x
  42. $ x="*"
  43. $ printf "%sn" "${x:?You must set x}" # Careful: should be used
  44. *
  45. $ printf "%sn" ${x:?"You must set x"} # Not quite careful enough
  46. abc
  47. def
  48. ghi
  49. $ x=
  50. $ printf "%sn" ${x:?"You must set x"} # Not quite careful enough
  51. bash: x: You must set x
  52. $ unset x
  53. $ printf "%sn" ${x:?"You must set x"} # Not quite careful enough
  54. bash: x: You must set x
  55. $
  56.  
  57. [ -z "$STATE" ] && echo "Need to set STATE" && exit 1;
  58.  
  59. ${MyVariable:=SomeDefault}
  60.  
  61. ${MyVariable:-SomeDefault}
  62.  
  63. ${MyVariable:=SomeDefault}
  64.  
  65. MyVariable=${MyVariable:=SomeDefault}
  66.  
  67. if [ "$MYVAR" = "" ]
  68. then
  69. echo "Does not exist"
  70. else
  71. echo "Exists"
  72. fi
  73.  
  74. if [ "x$STATE" == "x" ]; then echo "Need to set State"; exit 1; fi
  75.  
  76. $ unset a
  77. $ b=
  78. $ c=
  79. $ [[ -v a ]] && echo "a is set"
  80. $ [[ -v b ]] && echo "b is set"
  81. b is set
  82. $ [[ -v c ]] && echo "c is set"
  83. c is set
  84.  
  85. mapfile -t arr < variables.txt
  86.  
  87. EXITCODE=0
  88.  
  89. for i in "${arr[@]}"
  90. do
  91. ISSET=$(env | grep ^${i}= | wc -l)
  92. if [ "${ISSET}" = "0" ];
  93. then
  94. EXITCODE=-1
  95. echo "ENV variable $i is required."
  96. fi
  97. done
  98.  
  99. exit ${EXITCODE}
  100.  
  101. #
  102. # assert if variables are set (to a non-empty string)
  103. # if any variable is not set, exit 1 (when -f option is set) or return 1 otherwise
  104. #
  105. # Usage: assert_not_null [-f] variable ...
  106. #
  107. function assert_not_null() {
  108. local fatal var num_null=0
  109. [[ "$1" = "-f" ]] && { shift; fatal=1; }
  110. for var in "$@"; do
  111. [[ -z "${!var}" ]] &&
  112. printf '%sn' "Variable '$var' not set" >&2 &&
  113. ((num_null++))
  114. done
  115.  
  116. if ((num_null > 0)); then
  117. [[ "$fatal" ]] && exit 1
  118. return 1
  119. fi
  120. return 0
  121. }
  122.  
  123. one=1 two=2
  124. assert_not_null one two
  125. echo test 1: return_code=$?
  126. assert_not_null one two three
  127. echo test 2: return_code=$?
  128. assert_not_null -f one two three
  129. echo test 3: return_code=$? # this code shouldn't execute
  130.  
  131. test 1: return_code=0
  132. Variable 'three' not set
  133. test 2: return_code=1
  134. Variable 'three' not set
  135.  
  136. #!/bin/bash
  137. declare -a vars=(NAME GITLAB_URL GITLAB_TOKEN)
  138.  
  139. for var_name in "${vars[@]}"
  140. do
  141. if [ -z "$(eval "echo $$var_name")" ]; then
  142. echo "Missing environment variable $var_name"
  143. exit 1
  144. fi
  145. done
  146.  
  147. if (set -u; : $HOME) 2> /dev/null
  148. ...
  149. ...
  150.  
  151. is_this_an_env_variable ()
  152. local var="$1"
  153. if env |grep -q "^$var"; then
  154. return 0
  155. else
  156. return 1
  157. fi
  158. }
  159.  
  160. if [ $?BLAH == 1 ]; then
  161. echo "Exists";
  162. else
  163. echo "Does not exist";
  164. fi
Add Comment
Please, Sign In to add comment