\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{danilobanjac} \pdfinfo{ /Title (bash-scripting-language-cheat-sheet.pdf) /Creator (Cheatography) /Author (danilobanjac) /Subject (Bash Scripting Language Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{1574A3} \definecolor{LightBackground}{HTML}{F0F6F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Bash Scripting Language Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{danilobanjac} via \textcolor{DarkBackground}{\uline{cheatography.com/50162/cs/13858/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}danilobanjac \\ \uline{cheatography.com/danilobanjac} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 17th December, 2017.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Commands Line Commands}} \tn % Row 0 \SetRowColor{LightBackground} clear & Clears the screeen. \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} vim filename.sh & Creates a .sh file. \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} sh filename.sh & Execute Bash Script. \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} ./filename.sh & Other way of executing bash script. \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} ls -l & See all file permissions inside a folder. \tn % Row Count 8 (+ 3) % Row 5 \SetRowColor{white} chmod +x filename.sh & This will add execute permission to the file. \tn % Row Count 11 (+ 3) % Row 6 \SetRowColor{LightBackground} ls -{}-help & This will open all available commands. \tn % Row Count 13 (+ 2) % Row 7 \SetRowColor{white} ls -{}-help | grep "\textbackslash{}-U" & This will grab more information about "-U" command. \tn % Row Count 16 (+ 3) % Row 8 \SetRowColor{LightBackground} touch wood.txt & This will create a file. \tn % Row Count 18 (+ 2) % Row 9 \SetRowColor{white} echo "here is something use it" \textgreater{} wood.txt & This will write to the file that we first created. \tn % Row Count 21 (+ 3) % Row 10 \SetRowColor{LightBackground} cat wood.txt & This will get us the output of the file. \tn % Row Count 23 (+ 2) % Row 11 \SetRowColor{white} cat \{testfile01,testfile02\} \textgreater{} test00 & This will take as many files as we want and store the content of those file in one file in this case "test00". \tn % Row Count 29 (+ 6) % Row 12 \SetRowColor{LightBackground} echo "here is something new" \textgreater{} wood.txt & This will replace the whole content of the wood.txt file with this new content, if we want to add to file we need to use the command from below. \tn % Row Count 37 (+ 8) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Commands Line Commands (cont)}} \tn % Row 13 \SetRowColor{LightBackground} echo "here is comething new" \textgreater{}\textgreater{} wood.txt & Double \textgreater{}\textgreater{} will add to file. \tn % Row Count 2 (+ 2) % Row 14 \SetRowColor{white} : \textgreater{} wood.txt & This will empty the whole file, remeber ":" that says do nothing. \tn % Row Count 6 (+ 4) % Row 15 \SetRowColor{LightBackground} rm wood.txt & Will remove the file completely. \tn % Row Count 8 (+ 2) % Row 16 \SetRowColor{white} touch test1 test2 test3 test4 & for example if we create a hundreds of file with similar name how we could delete them all or select them all? (Solution below). \tn % Row Count 15 (+ 7) % Row 17 \SetRowColor{LightBackground} rm test* & ("*" astrix sign will mark everything that begins with test and delete it. \tn % Row Count 19 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.84 cm} x{6.16 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Vim Commands}} \tn % Row 0 \SetRowColor{LightBackground} Press "a" & In order to start editing the file. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Press "esc" & In order to quit editing the file. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Write ":" & To allow you to save or quit or write to file. \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} Write ":wg" & To write those changes and quit with saving. \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} Write ":wg!" & To force this action. \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} Write ":q!" & To quit without saving. \tn % Row Count 12 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Part One (Variables and Parameters)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{ECHOING VARIABLES: \newline - var=10 \newline echo var (This will echo "var") \newline echo \$var (This will echo the actual value, this is why we are using dollar sign "\$" before variable. So that program knows that we want a value from the variable) \newline UNSET: \newline - var=10 \newline unset var \newline echo \$var ("unset" will actually reassign the value of "var" to null) \newline ASIGN VALUE TO THE VARIABLE THROUGH USER: \newline - echo "type in some value" \newline read var2 (This will ask user for input same like "prompt" in javascript and store this value to variable) \newline echo \$var2 \newline \newline PROPERTIES OF VARIABLES: \newline - var="T r a l a l a lalalal l" \newline echo \$var (This will output the string but we will miss some spaces, this means that ot all of our spaces will be printed that is why we are using "") \newline var="T r a l a l a lalalal l" \newline echo "\$var" (When we use "" to wrap our variable with as well the "\$" dollar sign then all of our spaces will be outputed. This is recommended way of doing "echo" in bash) \newline DEFINE NULL VARIABLE: \newline - var= (This will define variable with a value of "null") \newline \newline DECLARING VARIABEL ON SAME LINE: \newline - var1=11 var2=22 var3=33 \newline echo "\$var1 \$var2 \$var3" (Same like in javascript) \newline \newline ASSIGNING, REASSIGNING AND UNSETTING THE VALUE: \newline - var= \newline echo "\$var" \newline var=9 \newline echo "\$var" \newline var=10 \newline echo "\$var" \newline unset var \newline echo "\$var" \newline \newline ASSIGN VALUE FROM BASH COMMAND TO VARIABLE: \newline - hi=\$(ls -la) (This is the way of doing it) \newline echo "\$hi" (Will print value of "hi") \newline \newline \newline ADDING TO A VARIABLE OR DOING ARITHMETIC OPERATIONS ON VARIABLES: \newline - var= \newline let "var ++" \newline echo "\$var" (This will output 1) \newline let "var += 10" \newline echo "\$var" (This will output 10) \newline \newline REPLACING VALUES IN NUMBER: \newline - num=1100 \newline var=\$\{num/10/B\} \newline echo "\$var" (This will echo "1B0") \newline \newline ENVIRONMENTAL VARIABLES: \newline - \#!/bin/bash \newline \newline MIN=10 (Minimum passed arguments in order to execute the script) \newline \newline if {[} -n "\$1" {]}; then echo "1st one is \$1"; fi ("\$1" first argument that is being passed to script and "-n" check if argument exists. Returning either "true" or "false") \newline if {[} -n "\$2" {]}; then echo "2st one is \$2"; fi \newline if {[} -n "\$3" {]}; then echo "3rd one is \$3"; fi \newline if {[} -n "\$4" {]}; then echo "4th one is \$4"; fi \newline if {[} -n "\$5" {]}; then echo "5th one is \$5"; fi \newline if {[} -n "\$6" {]}; then echo "6th one is \$6"; fi \newline if {[} -n "\$7" {]}; then echo "7th one is \$7"; fi \newline if {[} -n "\$8" {]}; then echo "8th one is \$8"; fi \newline if {[} -n "\$9" {]}; then echo "9th one is \$9"; fi \newline if {[} -n "\$\{10\}" {]}; then echo "10th one is \$\{10\}"; fi \newline \newline echo "List of arguments: "\${\emph{"" ("\$}}" this will take all the arguments that are being passed to script) \newline echo "Name of Script: \textbackslash{}""\$0"\textbackslash{}"" ("\$0" is used to grab the name of the file) \newline \newline if {[} \$\# -lt "\$MIN" {]}; then echo "Not enought arguments, need "\$MIN" to run!"; fi (This check "\$\#" number of all arguments being passed to script and compare it to our defined variable.) \newline - sh filename.sh 1 2 3 4 5 6 7 8 9 10 (Passing arguments to our script and printing them)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Part Two (Return Values)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{RETURN VALUES: \newline - \#!/bin/bash \newline \newline NO\_OF\_ARGS=2 \newline E\_BADARGS=85 \newline E\_UNREADABLE=86 \newline \newline if {[} \$\# -ne "\$NO\_OF\_ARGS" {]}; then echo "Usage: "\$0" fileOne fileTwo"; exit \$E\_BADARGS; fi \newline if {[} ! -r "\$1" {]} || {[} ! -r "\$2" {]}; then echo "One or both files does not exist: "\$1" or "\$2""; exit "\$E\_UNREADABLE"; fi \newline ( \newline cmp \$1 \$2 \newline ) \textgreater{} /dev/null 2\textgreater{}\&1 \newline if {[} \$? -eq 0 {]}; then echo "Files are the same!"; else echo "Files are not the same!"; fi \newline exit 0 \newline \newline - Explanation: This script will accept two files and than compare if they are identical. We are defining our "EXIT CODES" and returning them depending on situation. (exit code 0 == good (true); exit code 1 == bad (false))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Bash Script Programming Language Basics}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{ECHO: \newline - echo "Some text" (This is same like "print" in python. It will print to command line) \newline - echo "Some text" \#This is comment ("This is comment" will not be executed) \newline DEFINING VARIABLES: \newline - name=10 (This desfines a variable with integer) \newline - name=tea (This defines a variable) \newline USING HASH "\#": \newline - echo "The word \$name contains \$\{\#name\} chars" (This is how to do a string formatting in bash) \newline - \$(\#name) (This will return a lenght of the string in bash) \newline - \$name will replace this part of the string with the current variable. \newline USING SEMICOLON: \newline - echo "hi there"; echo "you there?" (; semicolon sign will tell bash to run this as a next line of the code) \newline IF/THEN/ELSE STATEMENT: \newline - var=10 \newline - if {[} "\$var" -gt 5 {]}; then echo "YES"; else echo "NO" \newline fi (Please note the spaces inside the brackets they need to be there for statement to run, and every semicolon will be seen as a next line and at the end we have "fi" this closes the if statement) \newline FOR LOOP: \newline - colors="red black white" (This defines a variable as a string) \newline - for col in \$colors \newline do (This will run the desired action) \newline echo "\$col" (This will echo "col") \newline done (This will finish the for loop) \newline - if we run this script now the result below will be printed to console: \newline red \newline black \newline white \newline - as you can see for loop threated variable colors as a list. Because "for col in \$colors" dollar sign and colors will make a list from the string. If we put \$colors inside double quotes like this "\$colors" this will be then threated as a string and the ouput to the console will be: \newline red black white \newline - if we put single quotes '\$colors' this would take the actual word \$colors and print it. \newline USING LET: \newline - let "y=((x=20, 10/2))" (let in bash will let us to perform arithemic operations on variables) \newline - echo \$y (This will return 5 because we separated the operation with comma) \newline CHANGING THE STRING TO UPPER OR LOWER: \newline - var = DSLConnection \newline - echo \$\{var,\} (This will change the first character of the string to lower) \newline - echo \$\{var,,\} (This will change the whole string to lower) \newline USING "\textbackslash{}" ESCAPE CHARACTER: \newline - echo ""Linux is awesome"" \newline will output this Linux is awesome to the console. \newline - echo "\textbackslash{}"Linux is awesome"\textbackslash{}" (This will take quotes literaly) \newline will output this "Linux is awesome" to the console \newline REASIGN THE VALUE: \newline - let val=500/2 \newline val2=`echo \$val` (This will allow us to reasign the value from the first variabel to the second variabel) \newline echo "\$val2" \newline will give same output "250" \newline IMPORTANT \newline In order to get the value from any oder script example Python. Bash script will only recognise the value if value is printed, that means that function can return number but at the end when we call the function in python script it should look like this: \newline print return\_value() \newline USING ":" SIGN: \newline - var=20 \newline if {[} "\$var" -gt 15 {]}; then :;else echo "\$var";fi (This ":" sign after then will actually tell our code to do nothing this will come very useful, same like "pass" in python) \newline IF STATEMENT USING "?" MARK: \newline - var=10 \newline echo \$(( var2=var1\textless{}20?1:0 )) (This will return the first value "1" if statement is true and second value if statement is false. In this case we don't need the "\$" sign before "var1" to tell the program to use value, this is special case) \newline CREATE ARRAY: \newline - Colors=(red blue green white) (This will create an array in bash) \newline WRAP STRINGS INTO SOME CHARACTERS: \newline - echo \textbackslash{}+\{test1,test2,test3\}\textbackslash{}+ (You can replace the "+" sign with any other sing for example "\$") \newline will output +test1+ +test2+ +test3+ to console \newline CREATE RANGE: \newline - echo \{0..9\} (This will print all numbers between 0 and 9 (including 9) same like range() in python) \newline SEPARATE THE BLOCK OF CODE: \newline - var1=1 \newline var2=2 \newline \{ \newline var1=10 \newline var2=12 \newline \} \newline echo "\$var1 \$var2" (The output to the console will be 10 and 12 because "\{\}" will separate this part of code) \newline SAVE EXIT CODE FROM THE LAST COMMAND: \newline - python myPythonScript.py \newline ret=\$? \newline if {[} \$ret -ne 0 {]}; then \newline \#Handle failure \newline \#exit if required \newline fi \newline USE EXIT CODE TO MANIPULATE SCRIPT: \newline - \#!/bin/bash \newline \newline touch /root/test 2\textgreater{} /dev/null \newline \newline if {[} \$? -eq 0 {]} \newline then \newline echo "Successfully created file" \newline exit 0 \newline else \newline echo "Could not create file" \textgreater{}\&2 \newline exit 1 \newline fi \newline HIDE WHOLE OUTPUT FROM THE SCRIPT: \newline - ( \newline ./manage.py create\_test\_database \newline ) \textgreater{} /dev/null 2\textgreater{}\&1 \newline \newline CONNECTING IF STATEMENTS: \newline - var=1 \newline if {[} "\$var" -gt 0 {]} \&\& {[} "\$var" -eq 10 {]}; then echo "THEN PART"; else echo "HELLOOO"; fi (Example with logical "and" statment) \newline var=1 \newline if {[} "\$var" -gt 0 {]} || {[} "\$var" -eq 10 {]}; then echo "THEN PART"; else echo "HELLOOO"; fi (Example with logical "or" statment) \newline MODULO: \newline - let var=5\%4 \newline echo "\$var" (Result will be one) \newline STRING UPPER CASE: \newline - some\_word=tEsT \newline echo "\$\{some\_word\textasciicircum{}\}" (This will grab first letter and make it upper case) \newline echo "\$\{some\_word\textasciicircum{}\textasciicircum{}\}" (This will grab whole word and make it upper case)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}