Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.43 KB | None | 0 0
  1. # Tips
  2. tips () {
  3. red='\033[01;31m'
  4. green='\033[01;32m'
  5. NC='\033[0m' # No Color
  6. #List all tip
  7. if [[ $# -eq 0 ]]; then
  8. clear
  9. echo -e "${red}Usage: tips TIP-NAME${NC}"
  10. echo -e
  11. echo -e "${green}random${NC}: How to generate a random number of a certain length
  12. ${green}para${NC}: Parameter expansion
  13. ${green}hist${NC}: History
  14. ${green}var${NC}: Bash Internal Variables
  15. ${green}test${NC}: Shell tests
  16. ${green}mov${NC} : Efficient Cursor Movements
  17. ${green}array${NC} : How to work with arrays
  18. ${green}short${NC} : Shortcut"| sort
  19. fi
  20. #List specific tip
  21. case $1 in
  22. random)
  23. clear
  24. echo -e "${red}To get random number from range:${NC}"
  25. echo -e "${green}shuf -i LOW-HIGH -n 1${NC}"
  26. echo -e
  27. ;;
  28.  
  29. mov)
  30. clear
  31. echo -e "${red}Moving to the beginning and end of line${NC}"
  32. echo -e "${green}^a${NC}: To beginning of the line (bol)."
  33. echo -e "${green}^e${NC}: To end of line (eol)."
  34. echo -e
  35. echo -e "${red}Moving a character or word at a time${NC}"
  36. echo -e "${green}^f${NC}: Forward one character."
  37. echo -e "${green}^b${NC}: Backward one character."
  38. echo -e "${green}alt+f${NC}: Move cursor forward one word."
  39. echo -e "${green}alt+b${NC}: Move cursor backward one word."
  40. echo -e
  41. echo -e "${red}Transposing letters or words${NC}"
  42. echo -e "${green}^t${NC}: Switches the character with the one next to it."
  43. echo -e "${green}alt+t${NC}: transpose the word at the cursor location with the one preceding it."
  44. echo -e
  45. echo -e "${red}Changing casing${NC}"
  46. echo -e "${green}alt+l${NC}: Convert characters from cursor to end of the word to lowercase."
  47. echo -e "${green}alt+u${NC}: Convert characters from cursor to end of the word to uppercase."
  48. echo -e
  49. echo -e "${red}Deleting characters or words${NC}"
  50. echo -e "${green}^d${NC}: Delete the character at the cursor location."
  51. echo -e "${green}^k${NC}: Kill the text from the cursor location to eol."""
  52. echo -e "${green}^u${NC}: Kill text from the cursor location to bol."
  53. echo -e "${green}alt+d${NC}: Kill text from the cursor location to the end of the current word."
  54. echo -e
  55. echo -e "${red}Clearing the terminal window${NC}"
  56. echo -e "${green}^l${NC}: Alternative to the command ${green}clear${NC}."
  57. echo -e
  58. ;;
  59.  
  60. para)
  61. clear
  62. x=abcdefghifirstjklmnopwordqrstuvwxuz0first1
  63.  
  64. echo -e ${green}'$x'${NC}: X: $x
  65. echo -e
  66. echo -e "${red}Case 1${NC}:"
  67. echo -e ${green}'${x^}'${NC}: Uppercase first letter ${x^}
  68. echo -e ${green}'${x^^}'${NC}: Uppercase all letters ${x^^}
  69. echo -e
  70. echo -e "${red}Substrings…${NC}:"
  71. echo -e ${green}'${x:12}'${NC}: First 12: ${x:12}
  72. echo -e ${green}'${x:15:10}'${NC}: 15 for 10: ${x:15:10}
  73. echo -e
  74. echo -e "${red}Middle Substrings…${NC}:"
  75. echo -e ${green}'mid=${x#*o} mid=${mid%o*}'${NC}: Shows what"'"s between the two "'o'": $x "->" ${x#*o} "->" ${mid%o*}
  76. echo -e
  77. echo -e "${red}Removals…${NC}"
  78. echo -e ${green}'${x#*o}'${NC}: everything to o ${x#*o}
  79. echo -e ${green}'${x##*o}'${NC}: everything to last o ${x##*o}
  80. echo -e ${green}'${x%o*}'${NC}: everything from back to o ${x%o*}
  81. echo -e ${green}'${x%%o*}'${NC}: everything from back to last o ${x%%o*}
  82. echo -e
  83. echo -e "${red}Replacements…${NC}"
  84. echo -e ${green}'${x/first/this}'${NC}: replace first occurrence of word ${x/first/this}
  85. echo -e ${green}'${x//first/this}'${NC}: replace all occurrence of word ${x//first/this}
  86. echo -e ${green}'${x/first}'${NC}: if no replacement given replace once ${x/first}
  87. echo -e ${green}'${x//first}'${NC}: if no replacement given replace all ${x//first}
  88.  
  89. x=ABCDEFGHIFIRSTJKLMNOPWORDQRSTUVWXUZ0FIRST1
  90.  
  91. echo -e
  92. echo -e "${red}Case 2${NC}"
  93. echo -e ${green}'$x'${NC}: X: $x
  94. echo -e
  95. echo -e ${green}'${x,}'${NC}: Lowercase first letter ${x,}
  96. echo -e ${green}'${x,,}'${NC}: Lowercase all letters ${x,,}
  97. echo -e ${green}'${x~}'${NC}: Lowercase first letter ${x~}
  98. echo -e ${green}'${x~~}'${NC}: Lowercase all letters ${x~~}
  99.  
  100. y=abababcacaca
  101. var="${y//[^a]}"
  102.  
  103. echo -e
  104. echo -e "${red}Count how many time a string appear${NC}"
  105. echo -e
  106. echo -e ${green}'$y'${NC}: X: $y
  107. echo -e
  108. echo -e ${green}'${y//[^a]}'${NC}: Extract the string 'a' from the whole 'abababcacaca', in this case: ${y//[^a]}
  109. echo -e ${green}'var="${y//[^a]}" && echo "${#var}"'${NC}: The proper count of how many 'a' there is: ${#var}
  110. echo -e
  111.  
  112. ;;
  113.  
  114. hist)
  115. clear
  116. echo -e "${red}History${NC}"
  117. echo -e "${green}!51${NC}: Recall and execute the command associated with the history number 51."
  118. echo -e "${green}!-2${NC}: Recall and execute a command that we typed before our most recent one."
  119. echo -e "${green}CTRL-p${NC}: Recall and execute the last command."
  120. echo -e "${green}!!${NC}: Recall and execute the last command '(shortcut to ${green}!-1${NC}.)'."
  121. echo -e "${green}sudo !!${NC}: Sudo the last command."
  122. echo -e "${green}^foo^bar^${NC}: Replace ${green}'foo'${NC} by ${green}'bar'${NC} in the last command."
  123. echo -e "${green}!!:1${NC}: Refers to the first argument of the last command."
  124. echo -e "${green}!!:2-4${NC}: Refers to the 2nd to 4th argument of the last command."
  125. echo -e "${green}!!:^${NC}: Refers to the first argument of the last command."
  126. echo -e "${green}!!:\$${NC}: Refers to the last argument of the last command."
  127. echo -e "${green}CTRL-r${NC}: Search the history forward."
  128. echo -e "${green}CTRL-s${NC}: Search history backward."
  129. echo -e "${green}ALT+.${NC}: Place the argument of the most recent command on the shell" # http://www.commandlinefu.com/commands/view/1551/place-the-argument-of-the-most-recent-command-on-the-shell
  130. echo -e
  131. echo -e "${red}Modifiers${NC}"
  132. echo -e "${green}!!:$:r${NC}: Removes extension of the last argument of the last command."
  133. echo -e "${green}!!:$:e${NC}: Removes all but the extension of the last argument of the last command."
  134. echo -e "${green}!!:$:h${NC}: Removes the trailing pathname component, leaving the head '"/usr/local/apache" to "/usr/local"'."
  135. echo -e
  136. ;;
  137.  
  138. test)
  139. clear
  140. echo -e "${red}Test if a variable is divisible by a number${NC}"
  141. echo -e 'if ! ((n % 4)); then'
  142. echo -e ' echo "$n divisible by 4."'
  143. echo -e 'fi'
  144. echo -e
  145. echo -e "${red}Test the number of arguments${NC}"
  146. echo -e 'if [ "$#" -eq 2 ]; then'
  147. echo -e ' echo "The arguments are 2 as they should be"'
  148. echo -e 'fi'
  149. echo -e
  150. echo -e "${red}Test the exit status${NC}"
  151. echo -e 'if [[ "$?" != 0 ]]; then'
  152. echo -e ' echo "Exit status is dofferent from 0"'
  153. echo -e 'fi'
  154. echo -e
  155. ;;
  156.  
  157. var)
  158. clear
  159. echo -e "${red}Bash Internal Variables${NC}"
  160. echo -e
  161. echo -e ${green}'$*'${NC}": Expands all the positional parameters (Bad for parameters with space)"
  162. echo -e '\t'\$ 'func() { for i in $*; do echo $i; done };'
  163. echo -e '\t'\$ 'func hi there "hi there"'
  164. echo -e '\thi'
  165. echo -e '\tthere'
  166. echo -e '\thi'
  167. echo -e '\tthere'
  168. echo -e
  169. echo -e ${green}'"$*"'${NC}": Expands all the positional parameters as ONE single string"
  170. echo -e '\t'\$ 'func() { for i in "$*"; do echo $i; done };'
  171. echo -e '\t'\$ 'func hi there "hi there"'
  172. echo -e '\thi there hi there'
  173. echo -e
  174. echo -e ${green}'$@'${NC}": Expands all the positional parameters (Bad for parameters with space)"
  175. echo -e '\t'\$ 'func() { for i in $@; do echo $i; done };'
  176. echo -e '\t'$ 'func hi there "hi there"'
  177. echo -e '\thi'
  178. echo -e '\tthere'
  179. echo -e '\thi'
  180. echo -e '\tthere'
  181. echo -e
  182. echo -e ${green}'"$@"'${NC}": Expands all the positional parameters and respects space (Good for parameters with space)"
  183. echo -e '\t'\$ 'func() { for i in "$@"; do echo $i; done };'
  184. echo -e '\t'\$ 'func hi there "hi there"'
  185. echo -e '\thi'
  186. echo -e '\tthere'
  187. echo -e '\thi there'
  188. echo -e
  189. echo -e ${green}'$#'${NC}": The number os positional parameters"
  190. echo -e
  191. echo -e ${green}'$?'${NC}": Exit code of the latest command in the foreground"
  192. echo -e
  193. echo -e ${green}'$!'${NC}": PID of the latest command in the background"
  194. echo -e
  195. echo -e ${green}'$$'${NC}": PID of the current shell (different for scripts but same for functions)"
  196. echo -e
  197. echo -e ${green}'$_'${NC}": Last argument of the last command"
  198. echo -e
  199. echo -e ${green}'$-'${NC}": Bash options (see xv())"
  200. echo -e
  201. ;;
  202.  
  203. short)
  204. clear
  205. echo -e "${red}Shortcuts${NC}"
  206. echo -e
  207. echo -e "${green}CTRL-u...CTRL-y${NC}: Delete a whole command line, check another command if needed then yank it back."
  208. echo -e
  209. echo -e "${green}(cd /tmp && ls)${NC}: Jump to a directory, execute a command and jump back to current dir"
  210. echo -e
  211. ;;
  212.  
  213. array)
  214. clear
  215. echo -e "${red}Set an array${NC}"
  216. echo -e '\t' array=\(one two three \"four five\"\)
  217. echo -e
  218. echo -e "${red}Show the array content${NC}"
  219. echo -e '\t' \$ echo \${array[@]}
  220. echo -e '\t' one two three four five
  221. echo -e "${red}Show the array indexes${NC}"
  222. echo -e '\t' \$ echo \${\!array[@]}
  223. echo -e '\t' 0 1 2 3
  224. echo -e "${red}Show the array length${NC}"
  225. echo -e '\t' \$ echo \${\#array[@]}
  226. echo -e '\t' 4
  227. echo -e "${red}Iterate depending on an array indexes${NC}"
  228. echo -e '\t' \$ for index in \"$\{\!array\[\@\]\}\"\; do echo \"\$\{array\[index\]\}\"\; done
  229. echo -e '\t' one
  230. echo -e '\t' two
  231. echo -e '\t' three
  232. echo -e '\t' four five
  233. echo -e
  234. echo -e ${green}check${NC} http://www.thegeekstuff.com/2010/06/bash-array-tutorial
  235. echo -e
  236. ;;
  237. esac
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement