1/46
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Bash
A command-line scripting language used for the command shell inside Unix-like systems (Linux and MacOS)
Bash is NOT object oriented
All bash scripts should start with #!/bin/bash
Bash Variables
Define a variable with varName = <value>
To use the variable, use $. $varName for example
Bash Variable Declaring
declare option VarName=<value>
Ex: declare -i PhoneNumber=1111111 (treat as integer)
Ex: declare -r Pi=3.14 (read only)
Bash Arrays
tempArray=[value1, value2, value3]
Index with $tempArray[index] (remember that in bash you reference variables with $)
Bash Named and Associative Arrays
declare -A PhoneBook (declare it first)
PhoneBook[name]=”Jason”
PhoneBook[number]=”111-1111”
${PhoneBook[name]} to get the name (named arrays use curly brackets
${PhoneBook[number]} to get the number
Bash Arithmetic Comparison
is a equal to b → if [ “$a” -eq “$b” ]
is a not equal to b → if [ “$a” -ne “$b” ]
greater than → -gt
greater than or equal to → -ge
less than → -lt
less than or equal to → le
You can use < and > but you have to use double parenthesis around the operation
((“$a” < “$b”)) for example
Bash String Comparison
if [ “$a” = “$b” ] or if [ “$a” == “$b” ] for is equal to
if [ “$a” != “$b” ] for not equal to
You can compare words with < and >. This will order based on ASCII ordering
if [ “$a” \< “$b” ] for is less than (in alphabetical order)
if [[ “$a” < “$b” ]] for is less than (remember double wrapped if using raw < and >
Bash Logical Comparison
if [condition]
then
# do some command
elif [condition]
then
# do something
else
# do something
fiBash For Loop Done
for var in <list>
do
# commands
doneEx:
for value in {1..5}
do
echo $value
done
echo "All done"Bash While Do Done
while [some test]
do
# commands
doneEx:
counter=1
while [$counter -lt 10]
do
echo $counter
((counter++))
done
echo "All done"Bash Until Do Done
Perform a set of commands until a test is true
until [some test]
do
# commands
doneEx:
counter = 1
until [$counter -gt 5]
do
echo $counter
((counter++))
done
echo "All done"Bash String Operations
The commands used to manipulate data in string format
Bash Substrings
testString = "Test String"
echo ${testString:2:4}
> st SFirst number is start index, second number is how many characters from that position (including the position itself)
Bash Inputting and Outputting Data
Output with echo
Input with read
Read input into a variable: read UserName
Bash Getting Input From File
TempFile = $(<test.txt)
echo "$TempFile"TempFile contains the contents of test.txt
Bash Outputting to a File
> overwrites what is in the file
» appends to a file
echo "hi" > hi.txt
echo "amongus" >> hi.txtBash Command Line Args
$1 is the first argument passed into the script
$0 is the name of the script itself
PowerShell Comments
Uses #
<# a bunch of stuff #> also works to make large multi-line comments
PowerShell Variables
All variable creation begins with $
$varName = <value>
$CustomerName = Jason
Access variables by also using the $ notation
Also allows typing
[int]$AnswerNumber = 42
[string]$AnswerString = “The life, the universe, and everything”
Constants (set the variable to read only)
Set-Variable <VarName> -Option ReadOnly -Value <value>
PowerShell Arrays
$tempArray = @() # Blank array
$TempArray = @('Jason', 'Sahra', 'Eduardo', 'Linda')Arrays are denoted with @
Access with $tempArray[index]
PowerShell Named and Associative Arrays
$PhoneBook = @{}
$PhoneBook.name = 'Jason'
$PhoneBook.number = '321-1234'
#Reference
$PhoneBook.name
# All in one
$PhoneBook = @{'name' = 'Jason', 'number' = '321-1234'}PowerShell Comparisons
Same as Bash, also uses -eq, -le, -ge, -lt, -gt, etc
PowerShell Conditional Statements
if (condition) {
# Then do some command
}if (condition) {
# Code here
}
elseif (condition) {
# Code here
}
else {
# Code here
}PowerShell For Loop
for (<Init> ; <Condition> ; <Repeat>) {
<Statement list>
}for ($i = 1; $1 -lt 5; $i++) {
Write-Host $i
}
Write-Host "All done"PowerShell Do While
Do {
# Commands
}
While ($this -eq $that)$i = 1
Do {
Write-Host $i
$i++
}
While ($i -lt 10)
Write-Host "All Done"PowerShell Until Do
Do {
# commands
}
Until ($this -eq $that)$i = 1
Do {
Write-Host $i
$i++
}
Until ($i -gt 5)
Write-Host "All Done"PowerShell String Operations
Print with Write-Host
Concatenation: Write-Host $testString + “2” → ”Test String2”
PowerShell Substrings
$testString = "Test String"
$testString.substring(2, 4)
> st SFirst number is starting index, second number is amount of characters to include (including the starting index itself
PowerShell Get Input
Read-Host $VarName
PowerShell Read File
$TempFile = Get-Content - Path C:\Test.txt
Write-Host $TempFilePowerShell Write to File
> overwrites
» Appends
Write-Host "Hello" > hello.txt
Write-Host "Hi" >> hello.txtPowerShell Dead Giveaway
The cmdlets are dead giveaways that a script is PowerShell
Python Comments
Uses #
Python Variables
varName = <value>
Python Type Casting / Setting
Price = int(42) # Variable will always be an int
Price = float(42.00) # Variable will always be a float
Price = str("The life, the universe, and everything")No need to do anything special to reference variables, just use the name
Python Constants
Use all caps to denote
PI = 3.14159Python Arrays
tempArray = []
tempArray = [value1, value2, value3]
nameArray = ["Jason", "Mary", "Joe"]
nameArray[0] => JasonPython Dictionary
PhoneBook = {}
PhoneBook = {'name' = 'Jason', 'number' = "321-1234"}
PhoneBook["name"] => JasonPython Comparisons
equal to → ==
not equal to → != or <>
greater than, less than, gte, lte → >, <, >=, <=
Python Conditional Statements
if condition:
# codeif a == b:
print(a)if (condition):
# code
elif (condition):
# more stuff
else:
# stuffPython For Loop
for x in list:
# StuffPython While Loop
i = 1
while i < 6:
print(i)
i++
print("All done")Python Until Loop
Don’t really exist in Python. You do it by reversing the conditional check of a while loop
Python String Operations
String Concatenation → print(testString, “ in Python” + “ today”)
Python Substrings
MyName = "Jason Dion"
MyName[0] -> 'J'
MyName[0:4] -> 'Jaso'
MyName[6:8] -> 'Di'
MyName[-2] -> 'o'
MyName[6:-2] -> 'Di'
MyName[-4:-2] -> 'Di'Python substring, first number is the start index, second number is end index non-inclusive
Allows negative indexing. -1 is the very last character
Python Input and Output
Input with var = input(“Please enter your name:”)
Output with print
Python Files
Write to file
tempFile = open('test.txt', 'w') # write will overwrite
tempFile = open('test.txt', 'a') # append will append, no overwriteRead from file
tempFile = open('test.txt', 'r')
print(tempFile.read())
print(tempFile.read(50)) # prints first 50 characters
print(tempFile.readline(-5)) # Prints last 5 lines of file