Section 21+22: Scripts Flashcards
Bash:
Comment
Variables
Arrays
o Comment
▪ # This is the first line of my script
▪ # This script is used to do backups of my systems
o Variables
▪ variable = value
CustomerName = Jason
$CustomerName
▪ declare option VariableName = value
declare -i PhoneNumber = 1111111
declare -r Pi = 3.14
o Arrays
▪ tempArray = (value1, value2, value3)
tempArray[position]
$tempArray[1] => value2
Bash:
Comparisons
Arithmetic
● is equal to
o if [ “$a” -eq “$b” ]
● is not equal to
o if [ “$a” -ne “$b” ]
● is greater than
o if [ “$a” -gt “$b” ]
● is greater than or equal to
o if [ “$a” -ge “$b” ]
● is less than
o if [ “$a” -lt “$b” ]
String Comparison
● is equal to
o if [ “$a” = “$b” ]
if [ “$a” == “$b” ]
● is not equal to
o if [ “$a” != “$b” ]
● is less than (in ASCII alphabetical order)
o if [ “$a” < “$b” ]
if [[ “$a” < “$b” ]]
● is greater than (in ASCII alphabetical order)
o if [ “$a” > “$b” ]
if [[ “$a” > “$b” ]]
Bash:
Inputting and Outputting Data
echo “Please enter your name:”
read UserName
echo “Hello $UserName!”
Powershell:
Comments
Variables
Arrays
Comment
▪ # This is the first line of my script
▪ <#
This is a comment block. You can use this to comment out large sections
of text or code in your scripts.
#>
o Variables
▪ $variable = value
$CustomerName = Jason
▪ [int]$AnswerNumber = 42
▪ [string]$AnswerString = “The life, the universe, and everything.”
▪ To declare a constant, simply make the variable read only
● Set-Variable Pi -Option ReadOnly -Value 3.14159
o Arrays
▪ Allows for the storage of multiple values and reference them from a
single name
▪ $tempArray = @()
$tempArray = @(‘Jason’, ‘Sahra’, ‘Eduardo’, ‘Linda’)
$tempArray[position]
$tempArray[1] => Sahra
Powershell:
Comparisons
Comparisons
▪ is equal to
● $a -eq $b
▪ is not equal to
● $a -ne $b
▪ is greater than
● $a -gt $b
▪ is greater than or equal to
● $a -ge $b
▪ is less than
● $a -lt $b
▪ is less than or equal to
● $a -le $b
Powershell:
Inputting/Outputting Data
Inputting and Outputting Data
▪ Write-Host “Please enter your name:”
Read-Host $UserName
Write-Host “Hello ” + $UserName + “!”
Powershell:
Reading and Writing Data into Files
Reading and Writing Data into Files
▪ $TempFile = Get-Content - Path C:\test.txt
Write-Host $TempFile
▪ Write-Host “This is the beginning of a new script log file” > script.log
▪ < means input, > means output
▪ > overwrites,»_space; appends
● .\enumerate.ps1»_space; script.log
Bash:
Reading and Writing Data into Files
Reading and Writing Data into Files
▪ TempFile=$(<filename)
▪ TempFile=$(<test.txt)
echo “$TempFile”
▪ < means input, > means output
▪ > overwrites,»_space; appends
● echo “This is now going to be added to the end of the file as the
next line”»_space; test.txt
Python:
Inputting and Outputting Data
Reading and Writing Data into Files
Inputting and Outputting Data
▪ userName = input(“Please enter your name:”)
print(“Hello ” + userName + “!”)
o Reading and Writing Data into Files
▪ tempFile = open(‘test.txt’, ‘w’) # write will overwrite any existing content
▪ tempFile = open(‘test.txt’, ‘a’) # append to the end without overwriting
▪ tempFile = open(‘test.txt’, ‘r’) # reads a file
▪ print(tempFile.read())
▪ print(tempFile.read(50)). # this would read the first 50 characters in the
file
▪ print(tempFile.readline(-5)). # this would read the last 5 lines of the file
Python:
Comment
Variables
Arrays
Comment
▪ # This is the first line of my script
▪ # This script is used to do backups of my systems
o Variables
▪ variable = value
Price = 10
▪ Integer variable doesn’t use quotes around the value
▪ String variables use quotes around letters and numbers
▪ Vendor = “CompTIA”
Vendor = ‘CompTIA’
Vendor = “123”
▪ Price = int(42)
Price = float(42.00)
Price = str(“The life, the universe, and everything.”)
▪ Type the variable name to display or interact with that variable
▪ In Python, constants aren’t really used like in other languages
▪ By convention, uppercase words are treated as constants and lowercase
or title case words as variables
o Arrays
▪ tempArray = []
tempArray = [value1, value2, value3]
nameArray = [“Jason”, “Mary”, “Joe”, “Susan”]
tempArray[position]
nameArray[0] => Jason
Python: Comparisons
Comparisons
▪ is equal to
● a == b
▪ is not equal to
● a != b OR a <> b
▪ is greater than
● a > b
▪ is greater than or equal to
● a >= b
▪ is less than
● a < b
▪ is less than or equal to
● a <= b
Perl:
Comment
variables
Arrays
Comment
▪ # This is the first line of my script
▪ # This script is used to do backups of my systems
o Variables
▪ $variable = value;
$CustomerName = Jason;
$CustomerName
▪ There is no need to declare variable types in Perl
▪ use constant NAME => <value>;</value>
o Arrays
▪ @tempArray = (value1, value2, value3);
@ages = (18, 21, 25, 30);
@names = (“Jason”, “Susan”, “David”, “Tamera”);
$tempArray[position]
$names[3] => Tamera
Perl:
Comparisons
Comparisons
▪ Numeric
● is equal to
o if ($a == $b)
● is not equal to
o if ($a != $b)
● is greater than
o if ($a > $b)
● is greater than or equal to
o if ($a >= $b)
● is less than
o if ($a < $b)
● is less than or equal to
o if ($a <= $b)
▪ String Comparison
● is equal to
o if ($a -eq $b)
● is not equal to
o if ($a -ne $b)
● is greater than
o if ($a -gt $b)
● is greater than or equal to
o if ($a -ge $b)
● is less than
o if ($a -lt $b)
● is less than or equal to
o if ($a -le $b)
Perl:
Inputting/Outputting Data
Reading/Writing Data to Files
Inputting and Outputting Data
▪ printf(“Please enter your name:”);
$string = <STDIN>;
printf(“You entered: $string”);</STDIN>
o Reading and Writing Data into Files
▪ open(DATA1, “<read.log”);
open(DATA2, “>write.log”);
▪ > overwrites,»_space; appends
▪ while (<DATA1>) {
p</DATA1>
JavaScript:
Comment
Variables
Arrays
Comment
▪ // This is the first line of my script
▪ /*
This is a multi-line
comment block
*/
o Variables
▪ let variable = value;
let CustomerName = ‘Jason’;
CustomerName = ‘Dion’;
▪ const PI = 3.14159
o Arrays
▪ let tempArray = [value1, value2, value3);
let listOfNames = [‘Jason’, ‘Mary’, ‘Christle’, ‘Tim’];
listOfNames[position]
listOfNames[1] => Mary
▪ var myPhoneBook = {};
var myPhoneBook = {Jason: 111-1234,
Mary: 222-5678};
▪ myPhoneBook.Jason
myPhoneBook.Jason = 333-1234;
myPhoneBook[“Jason”] = 333-1234;