Guest User

Untitled

a guest
Feb 24th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.20 KB | None | 0 0
  1. #!/bin/bash
  2. ##############################################################################
  3. # SHORTCUTS
  4. ##############################################################################
  5.  
  6.  
  7. CTRL+A # move to beginning of line
  8. CTRL+B # moves backward one character
  9. CTRL+C # halts the current command
  10. CTRL+D # deletes one character backward or logs out of current session, similar to exit
  11. CTRL+E # moves to end of line
  12. CTRL+F # moves forward one character
  13. CTRL+G # aborts the current editing command and ring the terminal bell
  14. CTRL+J # same as RETURN
  15. CTRL+K # deletes (kill) forward to end of line
  16. CTRL+L # clears screen and redisplay the line
  17. CTRL+M # same as RETURN
  18. CTRL+N # next line in command history
  19. CTRL+O # same as RETURN, then displays next line in history file
  20. CTRL+P # previous line in command history
  21. CTRL+R # searches backward
  22. CTRL+S # searches forward
  23. CTRL+T # transposes two characters
  24. CTRL+U # kills backward from point to the beginning of line
  25. CTRL+V # makes the next character typed verbatim
  26. CTRL+W # kills the word behind the cursor
  27. CTRL+X # lists the possible filename completions of the current word
  28. CTRL+Y # retrieves (yank) last item killed
  29. CTRL+Z # stops the current command, resume with fg in the foreground or bg in the background
  30.  
  31. DELETE # deletes one character backward
  32. !! # repeats the last command
  33. exit # logs out of current session
  34.  
  35.  
  36. ##############################################################################
  37. # BASH BASICS
  38. ##############################################################################
  39.  
  40.  
  41. export # displays all environment variables
  42.  
  43. echo $SHELL # displays the shell you're using
  44. echo $BASH_VERSION # displays bash version
  45.  
  46. bash # if you want to use bash (type exit to go back to your normal shell)
  47. whereis bash # finds out where bash is on your system
  48.  
  49. clear # clears content on window (hide displayed lines)
  50.  
  51.  
  52. ##############################################################################
  53. # FILE COMMANDS
  54. ##############################################################################
  55.  
  56.  
  57. ls # lists your files
  58. ls -l # lists your files in 'long format', which contains the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified
  59. ls -a # lists all files, including hidden files
  60. ln -s <filename> <link> # creates symbolic link to file
  61. touch <filename> # creates or updates (edit) your file
  62. cat > <filename> # places standard input into file (can also be used to dump the content of a file on stdout)
  63. more <filename> # shows the first part of a file (move with space and type q to quit)
  64. head <filename> # outputs the first 10 lines of file
  65. tail <filename> # outputs the last 10 lines of file (useful with -f option)
  66. vim <filename> # lets you create and edit a file
  67. mv <filename1> <filename2> # moves a file
  68. cp <filename1> <filename2> # copies a file
  69. rm <filename> # removes a file
  70. diff <filename1> <filename2> # compares files, and shows where they differ
  71. wc <filename> # tells you how many lines, words and characters there are in a file
  72. chmod -options <filename> # lets you change the read, write, and execute permissions on your files
  73. gzip <filename> # compresses files
  74. gunzip <filename> # uncompresses files compressed by gzip
  75. gzcat <filename> # lets you look at gzipped file without actually having to gunzip it
  76. lpr <filename> # print the file
  77. lpq # check out the printer queue
  78. lprm <jobnumber> # remove something from the printer queue
  79. genscript # converts plain text files into postscript for printing and gives you some options for formatting
  80. dvips <filename> # print .dvi files (i.e. files produced by LaTeX)
  81. grep <pattern> <filenames> # looks for the string in the files
  82. grep -r <pattern> <dir> # search recursively for pattern in directory
  83.  
  84.  
  85. ##############################################################################
  86. # DIRECTORY COMMANDS
  87. ##############################################################################
  88.  
  89.  
  90. mkdir <dirname> # makes a new directory
  91. cd # changes to home
  92. cd <dirname> # changes directory
  93. pwd # tells you where you currently are
  94.  
  95.  
  96. ##############################################################################
  97. # SSH, SYSTEM INFO & NETWORK COMMANDS
  98. ##############################################################################
  99.  
  100.  
  101. ssh user@host # connects to host as user
  102. ssh -p <port> user@host # connects to host on specified port as user
  103. ssh-copy-id user@host # adds your ssh key to host for user to enable a keyed or passwordless login
  104.  
  105. whoami # returns your username
  106. passwd # lets you change your password
  107. quota -v # shows what your disk quota is
  108. date # shows the current date and time
  109. cal # shows the month's calendar
  110. uptime # shows current uptime
  111. w # displays whois online
  112. finger <user> # displays information about user
  113. uname -a # shows kernel information
  114. man <command> # shows the manual for specified command
  115. df # shows disk usage
  116. du <filename> # shows the disk usage of the files and directories in filename (du -s give only a total)
  117. last <yourUsername> # lists your last logins
  118. ps -u yourusername # lists your processes
  119. kill <PID> # kills the processes with the ID you gave
  120. killall <processname> # kill all processes with the name
  121. top # displays your currently active processes
  122. bg # lists stopped or background jobs ; resume a stopped job in the background
  123. fg # brings the most recent job in the foreground
  124. fg <job> # brings job to the foreground
  125.  
  126. ping <host> # pings host and outputs results
  127. whois <domain> # gets whois information for domain
  128. dig <domain> # gets DNS information for domain
  129. dig -x <host> # reverses lookup host
  130. wget <file> # downloads file
  131.  
  132.  
  133. ##############################################################################
  134. # VARIABLES
  135. ##############################################################################
  136.  
  137.  
  138. varname=value # defines a variable
  139. varname=value command # defines a variable to be in the environment of a particular subprocess
  140. echo $varname # checks a variable's value
  141. echo $$ # prints process ID of the current shell
  142. echo $! # prints process ID of the most recently invoked background job
  143. echo $? # displays the exit status of the last command
  144. export VARNAME=value # defines an environment variable (will be available in subprocesses)
  145.  
  146. array[0]=val # several ways to define an array
  147. array[1]=val
  148. array[2]=val
  149. array=([2]=val [0]=val [1]=val)
  150. array(val val val)
  151.  
  152. ${array[i]} # displays array's value for this index. If no index is supplied, array element 0 is assumed
  153. ${#array[i]} # to find out the length of any element in the array
  154. ${#array[@]} # to find out how many values there are in the array
  155.  
  156. declare -a # the variables are treaded as arrays
  157. declare -f # uses function names only
  158. declare -F # displays function names without definitions
  159. declare -i # the variables are treaded as integers
  160. declare -r # makes the variables read-only
  161. declare -x # marks the variables for export via the environment
  162.  
  163. ${varname:-word} # if varname exists and isn't null, return its value; otherwise return word
  164. ${varname:=word} # if varname exists and isn't null, return its value; otherwise set it word and then return its value
  165. ${varname:?message} # if varname exists and isn't null, return its value; otherwise print varname, followed by message and abort the current command or script
  166. ${varname:+word} # if varname exists and isn't null, return word; otherwise return null
  167. ${varname:offset:length} # performs substring expansion. It returns the substring of $varname starting at offset and up to length characters
  168.  
  169. ${variable#pattern} # if the pattern matches the beginning of the variable's value, delete the shortest part that matches and return the rest
  170. ${variable##pattern} # if the pattern matches the beginning of the variable's value, delete the longest part that matches and return the rest
  171. ${variable%pattern} # if the pattern matches the end of the variable's value, delete the shortest part that matches and return the rest
  172. ${variable%%pattern} # if the pattern matches the end of the variable's value, delete the longest part that matches and return the rest
  173. ${variable/pattern/string} # the longest match to pattern in variable is replaced by string. Only the first match is replaced
  174. ${variable//pattern/string} # the longest match to pattern in variable is replaced by string. All matches are replaced
  175.  
  176. ${#varname} # returns the length of the value of the variable as a character string
  177.  
  178. *(patternlist) # matches zero or more occurrences of the given patterns
  179. +(patternlist) # matches one or more occurrences of the given patterns
  180. ?(patternlist) # matches zero or one occurrence of the given patterns
  181. @(patternlist) # matches exactly one of the given patterns
  182. !(patternlist) # matches anything except one of the given patterns
  183.  
  184. $(UNIX command) # command substitution: runs the command and returns standard output
  185.  
  186.  
  187. ##############################################################################
  188. # FUNCTIONS
  189. ##############################################################################
  190.  
  191.  
  192. # The function refers to passed arguments by position (as if they were positional parameters), that is, $1, $2, and so forth.
  193. # $@ is equal to "$1" "$2"... "$N", where N is the number of positional parameters. $# holds the number of positional parameters.
  194.  
  195.  
  196. functname() {
  197. shell commands
  198. }
  199.  
  200. unset -f functname # deletes a function definition
  201. declare -f # displays all defined functions in your login session
  202.  
  203.  
  204. ##############################################################################
  205. # FLOW CONTROLS
  206. ##############################################################################
  207.  
  208.  
  209. statement1 && statement2 # and operator
  210. statement1 || statement2 # or operator
  211.  
  212. -a # and operator inside a test conditional expression
  213. -o # or operator inside a test conditional expression
  214.  
  215. str1 = str2 # str1 matches str2
  216. str1 != str2 # str1 does not match str2
  217. str1 < str2 # str1 is less than str2
  218. str1 > str2 # str1 is greater than str2
  219. -n str1 # str1 is not null (has length greater than 0)
  220. -z str1 # str1 is null (has length 0)
  221.  
  222. -a file # file exists
  223. -d file # file exists and is a directory
  224. -e file # file exists; same -a
  225. -f file # file exists and is a regular file (i.e., not a directory or other special type of file)
  226. -r file # you have read permission
  227. -r file # file exists and is not empty
  228. -w file # your have write permission
  229. -x file # you have execute permission on file, or directory search permission if it is a directory
  230. -N file # file was modified since it was last read
  231. -O file # you own file
  232. -G file # file's group ID matches yours (or one of yours, if you are in multiple groups)
  233. file1 -nt file2 # file1 is newer than file2
  234. file1 -ot file2 # file1 is older than file2
  235.  
  236. -lt # less than
  237. -le # less than or equal
  238. -eq # equal
  239. -ge # greater than or equal
  240. -gt # greater than
  241. -ne # not equal
  242.  
  243. if condition
  244. then
  245. statements
  246. [elif condition
  247. then statements...]
  248. [else
  249. statements]
  250. fi
  251.  
  252. for x in {1..10}
  253. do
  254. statements
  255. done
  256.  
  257. for name [in list]
  258. do
  259. statements that can use $name
  260. done
  261.  
  262. for (( initialisation ; ending condition ; update ))
  263. do
  264. statements...
  265. done
  266.  
  267. case expression in
  268. pattern1 )
  269. statements ;;
  270. pattern2 )
  271. statements ;;
  272. esac
  273.  
  274. select name [in list]
  275. do
  276. statements that can use $name
  277. done
  278.  
  279. while condition; do
  280. statements
  281. done
  282.  
  283. until condition; do
  284. statements
  285. done
  286.  
  287. ##############################################################################
  288. # COMMAND-LINE PROCESSING CYCLE
  289. ##############################################################################
  290.  
  291.  
  292. # The default order for command lookup is functions, followed by built-ins, with scripts and executables last.
  293. # There are three built-ins that you can use to override this order: `command`, `builtin` and `enable`.
  294.  
  295. command # removes alias and function lookup. Only built-ins and commands found in the search path are executed
  296. builtin # looks up only built-in commands, ignoring functions and commands found in PATH
  297. enable # enables and disables shell built-ins
  298.  
  299. eval # takes arguments and run them through the command-line processing steps all over again
  300.  
  301.  
  302. ##############################################################################
  303. # INPUT/OUTPUT REDIRECTORS
  304. ##############################################################################
  305.  
  306.  
  307. cmd1|cmd2 # pipe; takes standard output of cmd1 as standard input to cmd2
  308. > file # directs standard output to file
  309. < file # takes standard input from file
  310. >> file # directs standard output to file; append to file if it already exists
  311. >|file # forces standard output to file even if noclobber is set
  312. n>|file # forces output to file from file descriptor n even if noclobber is set
  313. <> file # uses file as both standard input and standard output
  314. n<>file # uses file as both input and output for file descriptor n
  315. n>file # directs file descriptor n to file
  316. n<file # takes file descriptor n from file
  317. n>>file # directs file description n to file; append to file if it already exists
  318. n>& # duplicates standard output to file descriptor n
  319. n<& # duplicates standard input from file descriptor n
  320. n>&m # file descriptor n is made to be a copy of the output file descriptor
  321. n<&m # file descriptor n is made to be a copy of the input file descriptor
  322. &>file # directs standard output and standard error to file
  323. <&- # closes the standard input
  324. >&- # closes the standard output
  325. n>&- # closes the ouput from file descriptor n
  326. n<&- # closes the input from file descripor n
  327.  
  328.  
  329. ##############################################################################
  330. # PROCESS HANDLING
  331. ##############################################################################
  332.  
  333.  
  334. # To suspend a job, type CTRL+Z while it is running. You can also suspend a job with CTRL+Y.
  335. # This is slightly different from CTRL+Z in that the process is only stopped when it attempts to read input from terminal.
  336. # Of course, to interrupt a job, type CTRL+C.
  337.  
  338. myCommand & # runs job in the background and prompts back the shell
  339.  
  340. jobs # lists all jobs (use with -l to see associated PID)
  341.  
  342. fg # brings a background job into the foreground
  343. fg %+ # brings most recently invoked background job
  344. fg %- # brings second most recently invoked background job
  345. fg %N # brings job number N
  346. fg %string # brings job whose command begins with string
  347. fg %?string # brings job whose command contains string
  348.  
  349. kill -l # returns a list of all signals on the system, by name and number
  350. kill PID # terminates process with specified PID
  351.  
  352. ps # prints a line of information about the current running login shell and any processes running under it
  353. ps -a # selects all processes with a tty except session leaders
  354.  
  355. trap cmd sig1 sig2 # executes a command when a signal is received by the script
  356. trap "" sig1 sig2 # ignores that signals
  357. trap - sig1 sig2 # resets the action taken when the signal is received to the default
  358.  
  359. disown <PID|JID> # removes the process from the list of jobs
  360.  
  361. wait # waits until all background jobs have finished
  362.  
  363.  
  364. ##############################################################################
  365. # TIPS & TRICKS
  366. ##############################################################################
  367.  
  368.  
  369. # set an alias
  370. cd; nano .bash_profile
  371. > alias gentlenode='ssh admin@gentlenode.com -p 3404' # add your alias in .bash_profile
  372.  
  373. # to quickly go to a specific directory
  374. cd; nano .bashrc
  375. > shopt -s cdable_vars
  376. > export websites="/Users/mac/Documents/websites"
  377.  
  378. source .bashrc
  379. cd $websites
  380.  
  381.  
  382. ##############################################################################
  383. # DEBUGGING SHELL PROGRAMS
  384. ##############################################################################
  385.  
  386.  
  387. bash -n scriptname # don't run commands; check for syntax errors only
  388. set -o noexec # alternative (set option in script)
  389.  
  390. bash -v scriptname # echo commands before running them
  391. set -o verbose # alternative (set option in script)
  392.  
  393. bash -x scriptname # echo commands after command-line processing
  394. set -o xtrace # alternative (set option in script)
  395.  
  396. trap 'echo $varname' EXIT # useful when you want to print out the values of variables at the point that your script exits
  397.  
  398. function errtrap {
  399. es=$?
  400. echo "ERROR line $1: Command exited with status $es."
  401. }
  402.  
  403. trap 'errtrap $LINENO' ERR # is run whenever a command in the surrounding script or function exists with non-zero status
  404.  
  405. function dbgtrap {
  406. echo "badvar is $badvar"
  407. }
  408.  
  409. trap dbgtrap DEBUG # causes the trap code to be executed before every statement in a function or script
  410. # ...section of code in which the problem occurs...
  411. trap - DEBUG # turn off the DEBUG trap
  412.  
  413. function returntrap {
  414. echo "A return occurred"
  415. }
  416.  
  417. trap returntrap RETURN # is executed each time a shell function or a script executed with the . or source commands finishes executing
Add Comment
Please, Sign In to add comment