Folder full of pertinent coursework
#!/bin/bash1# First line of the script is shebang which tells the system how to execute2# the script: http://en.wikipedia.org/wiki/Shebang_(Unix)3# As you already figured, comments start with #. Shebang is also a comment.45# Borrowed from: http://learnxinyminutes.com/docs/bash/6# Bash is a name of the unix shell, which is also distributed as the shell for the GNU operating system7# and as default shell on Linux and Mac OS X. Nearly all examples below can be a part of a shell script or executed directly in the shell.89# Simple hello world example:10echo Hello world!1112# Each command starts on a new line, or after semicolon:13echo 'This is the first line'; echo 'This is the second line'1415# Declaring a variable looks like this:16Variable="Some string"1718# But not like this:19Variable = "Some string"20# Bash will decide that Variable is a command it must execute and give an error21# because it can't be found.2223# Or like this:24Variable= 'Some string'25# Bash will decide that 'Some string' is a command it must execute and give an26# error because it can't be found. (In this case the 'Variable=' part is seen27# as a variable assignment valid only for the scope of the 'Some string'28# command.)2930# Using the variable:31echo $Variable32echo "$Variable"33echo '$Variable'34# When you use the variable itself — assign it, export it, or else — you write35# its name without $. If you want to use variable's value, you should use $.36# Note that ' (single quote) won't expand the variables!3738# String substitution in variables39echo ${Variable/Some/A}40# This will substitute the first occurrence of "Some" with "A"4142# Substring from a variable43Length=744echo ${Variable:0:Length}45# This will return only the first 7 characters of the value4647# Default value for variable48echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"}49# This works for null (Foo=) and empty string (Foo=""); zero (Foo=0) returns 0.50# Note that it only returns default value and doesn't change variable value.5152# Builtin variables:53# There are some useful builtin variables, like54echo "Last program return value: $?"55echo "Script's PID: $$"56echo "Number of arguments: $#"57echo "Scripts arguments: $@"58echo "Scripts arguments separated in different variables: $1 $2..."5960# Reading a value from input:61echo "What's your name?"62read Name # Note that we didn't need to declare a new variable63echo Hello, $Name!6465# We have the usual if structure:66# use 'man test' for more info about conditionals67if [ $Name -ne $USER ]68then69echo "Your name isn't your username"70else71echo "Your name is your username"72fi7374# There is also conditional execution75echo "Always executed" || echo "Only executed if first command fails"76echo "Always executed" && echo "Only executed if first command does NOT fail"7778# To use && and || with if statements, you need multiple pairs of square brackets:79if [ $Name == "Steve" ] && [ $Age -eq 15 ]80then81echo "This will run if $Name is Steve AND $Age is 15."82fi8384if [ $Name == "Daniya" ] || [ $Name == "Zach" ]85then86echo "This will run if $Name is Daniya OR Zach."87fi8889# Expressions are denoted with the following format:90echo $(( 10 + 5 ))9192# Unlike other programming languages, bash is a shell — so it works in a context93# of current directory. You can list files and directories in the current94# directory with the ls command:95ls9697# These commands have options that control their execution:98ls -l # Lists every file and directory on a separate line99100# Results of the previous command can be passed to the next command as input.101# grep command filters the input with provided patterns. That's how we can list102# .txt files in the current directory:103ls -l | grep "\.txt"104105# You can redirect command input and output (stdin, stdout, and stderr).106# Read from stdin until ^EOF$ and overwrite hello.py with the lines107# between "EOF":108cat > hello.py << EOF109#!/usr/bin/env python110from __future__ import print_function111import sys112print("#stdout", file=sys.stdout)113print("#stderr", file=sys.stderr)114for line in sys.stdin:115print(line, file=sys.stdout)116EOF117118# Run hello.py with various stdin, stdout, and stderr redirections:119python hello.py < "input.in"120python hello.py > "output.out"121python hello.py 2> "error.err"122python hello.py > "output-and-error.log" 2>&1123python hello.py > /dev/null 2>&1124# The output error will overwrite the file if it exists,125# if you want to append instead, use ">>":126python hello.py >> "output.out" 2>> "error.err"127128# Overwrite output.out, append to error.err, and count lines:129info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err130wc -l output.out error.err131132# Run a command and print its file descriptor (e.g. /dev/fd/123)133# see: man fd134echo <(echo "#helloworld")135136# Overwrite output.out with "#helloworld":137cat > output.out <(echo "#helloworld")138echo "#helloworld" > output.out139echo "#helloworld" | cat > output.out140echo "#helloworld" | tee output.out >/dev/null141142# Cleanup temporary files verbosely (add '-i' for interactive)143rm -v output.out error.err output-and-error.log144145# Commands can be substituted within other commands using $( ):146# The following command displays the number of files and directories in the147# current directory.148echo "There are $(ls | wc -l) items here."149150# The same can be done using backticks `` but they can't be nested - the preferred way151# is to use $( ).152echo "There are `ls | wc -l` items here."153154# Bash uses a case statement that works similarly to switch in Java and C++:155case "$Variable" in156#List patterns for the conditions you want to meet1570) echo "There is a zero.";;1581) echo "There is a one.";;159*) echo "It is not null.";;160esac161162# for loops iterate for as many arguments given:163# The contents of $Variable is printed three times.164for Variable in {1..3}165do166echo "$Variable"167done168169# Or write it the "traditional for loop" way:170for ((a=1; a <= 3; a++))171do172echo $a173done174175# They can also be used to act on files..176# This will run the command 'cat' on file1 and file2177for Variable in file1 file2178do179cat "$Variable"180done181182# ..or the output from a command183# This will cat the output from ls.184for Output in $(ls)185do186cat "$Output"187done188189# while loop:190while [ true ]191do192echo "loop body here..."193break194done195196# You can also define functions197# Definition:198function foo ()199{200echo "Arguments work just like script arguments: $@"201echo "And: $1 $2..."202echo "This is a function"203return 0204}205206# or simply207bar ()208{209echo "Another way to declare functions!"210return 0211}212213# Calling your function214foo "My name is" $Name215216# There are a lot of useful commands you should learn:217# prints last 10 lines of file.txt218tail -n 10 file.txt219# prints first 10 lines of file.txt220head -n 10 file.txt221# sort file.txt's lines222sort file.txt223# report or omit repeated lines, with -d it reports them224uniq -d file.txt225# prints only the first column before the ',' character226cut -d ',' -f 1 file.txt227# replaces every occurrence of 'okay' with 'great' in file.txt, (regex compatible)228sed -i 's/okay/great/g' file.txt229# print to stdout all lines of file.txt which match some regex230# The example prints lines which begin with "foo" and end in "bar"231grep "^foo.*bar$" file.txt232# pass the option "-c" to instead print the number of lines matching the regex233grep -c "^foo.*bar$" file.txt234# if you literally want to search for the string,235# and not the regex, use fgrep (or grep -F)236fgrep "^foo.*bar$" file.txt237238239# Read Bash shell builtins documentation with the bash 'help' builtin:240help241help help242help for243help return244help source245help .246247# Read Bash manpage documentation with man248apropos bash249man 1 bash250man bash251252# Read info documentation with info (? for help)253apropos info | grep '^info.*('254man info255info info256info 5 info257258# Read bash info documentation:259info bash260info bash 'Bash Features'261info bash 6262info --apropos bash263264265