Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. # Variables
  2.  
  3. FOO=here
  4. FOO="here" # equivalent, always use quotes
  5.  
  6. BAR=$FOO
  7. BAR="$FOO"
  8. BAR="${FOO}"
  9. BAR='$FOO' # single quotes, does not evaluate the variable
  10.  
  11. BAZ="$FOOsy" # different variable name
  12. BAZ="${FOO}sy"
  13.  
  14. # Mnemonic: # (3 on keyboard) is in FRONT and % (5 on keboard) is in BACK
  15. echo "${BAZ%e*}" # delete shortest occurrence at end of string
  16. echo "${BAZ#*e}" # ... at beginning of string
  17. echo "${FOO##*e}" # longest occurrence at beginning
  18. echo "${FOO%%e*}" # longest occurrence at end
  19. echo "${FOO/e/a}" # substitute a for first occurence of e
  20. echo "${FOO//e/a}" # substitute a for all occurrences of e
  21. echo "${FOO/#he/ha}" # if beginning of string matches he, replace it with ha
  22. echo "${FOO/%re/ro}" # if end of string ...
  23. echo "${BAZ:0:2}" # characters 0 through 2
  24. echo "${#BAZ}" # number of characters in variable
  25.  
  26. # Arithmetic
  27. # Operators: Add(+) Subtract(-) Multiply(*) Divide(/) Modulo(%) Exponent(**)
  28. echo "$((1+2))"
  29. X=1
  30. echo "$((X+1))"
  31. echo "$((${X}+1))"
  32.  
  33. # Tests
  34. # [ vs [[ (latter includes regex (=~) support)
  35. # -d (is directory), -f (is file), -e (exists), -n (nonempty), -z (zero-length)
  36. # -eq, ==
  37. if [[ "${FOO}" =~ ^he ]]; do
  38. echo "Match"
  39. fi
  40.  
  41. # Control Structures
  42. for VARIABLE in list of values; do
  43. echo "${VARIABLE}"
  44. done
  45.  
  46. while true; do
  47. echo date
  48. sleep 1
  49. done
  50.  
  51. if [[ -n "$FOO" ]]; then
  52. echo "Yep"
  53. fi
  54.  
  55. while read WORD1 WORD2; do # reads a line from stdin, assigns first two words to variables
  56. echo "1: ${WORD1}, 2: ${WORD2}"
  57. done
  58.  
  59. while : ; do # infinite loop, ":" is a noop
  60. ...
  61. break
  62. done
  63.  
  64. for FILE in $(ls *.jpeg); do
  65. mv "${FILE}" "${FILE%.jpeg}.jpg"
  66. done
  67.  
  68. # Functions
  69. # local, shift
  70. my_func () {
  71. local VAR1="$1"; shift
  72. local VAR2="$1"
  73.  
  74. echo "${VAR1}"
  75. echo "${VAR2}"
  76. }
  77.  
  78. # Paths
  79. # $BASH_SOURCE, path to the current script
  80. # dirname $SOMEPATH, basename $SOMEPATH
  81. # globbing
  82. # ?[ab]*
  83. # {ab*,bc*}
  84.  
  85. # Command Substitution
  86. # Stop using backticks!
  87. FOO=`dirname $BASH_SOURCE`
  88. FOO="`dirname $BASH_SOURCE`"
  89.  
  90. # much better
  91. FOO="$(dirname $(realpath "$BASH_SOURCE"))"
  92.  
  93. # settings/shopt
  94. #!/bin/bash -x
  95. set -x # print every line before evaluating
  96. set -e # fail on errors
  97. set -o pipefail # fail on errors in pipelines, -o can set many options
  98. set -u # fail when undefined variables are encountered
  99.  
  100. # Pipelines and IO
  101. # > overwrite file
  102. # >> append to file
  103. # < read from file
  104. # << heredoc
  105. # &0, &1, &2 stdin, stdout, stderr
  106. # exec 6<&0; exec 0</foo/bar; ... ; exec 0<&6 6<&- # rearrange file descriptors
  107. # | pipe stdout, |& pipe stdout+stderr
  108. # <() subshell fifo
  109. # <<< string redirection
  110.  
  111. # Temporary files
  112. TEMPFILE="$(mktemp)"
  113. trap "rm -f $TEMPFILE" exit
  114.  
  115. # Winning Combos
  116. # curl -s | jq
  117. # ssh somehost bash < my_local_script.sh
  118. # find -exec
  119.  
  120. # Interactive
  121. # ctrl-r (history search)
  122. # !! - previous command line
  123. # !$ - last argument of previous command
  124. # !ssh - most recent command beginning with ssh
  125. # host somewebsite.com; ^some^my - run previous command with substitutions
  126. # echo hello my name is matthew; echo !!:1 !!:$ - revise and run last command
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement