Bash Scripting Flashcards
What is GNU Bourne-Again Shell (Bash)?
It’s a powerful work environment and scripting engine. A competent security professional skillfully leverages Bash scripting to streamline and automate many Linux tasks and procedures.
How to create a simple ‘Hello World’ Bash script?
#!/bin/bash # Hello World Bash Script echo "Hello World!"
How to declare a variable “first_name” as “Kacper”.
kali@kali:~$ first_name=Kacper
How to display “first_name” variable using Bash?
kali@kali:~$ echo $first_name
How to use of command substitution (whoami) and variables?
user=$(whoami)
user=whoami
How to instruct bash to print additional debug output, so we could more easily see the commands that were executed and their results.
!/bin/bash -x
Describe special Bash variable ‘$0’
The name of the Bash script
Describe special Bash variable ‘$1 - $9’
The first 9 arguments to the Bash script
Describe special Bash variable ‘$#’
Number of arguments passed to the Bash script
Describe special Bash variable ‘$@’
All arguments passed to the Bash script
Describe special Bash variable ‘$?’
The exit status of the most recently run process
Describe special Bash variable ‘$$’
The exit status of the most recently run process
Describe special Bash variable ‘$USER’
The username of the user running the script
Describe special Bash variable ‘$HOSTNAME’
The hostname of the machine.
Describe special Bash variable ‘$RANDOM’
A random number.
Describe special Bash variable ‘$LINENO’
The current line number in the script
How to collect user input using read “answer”?
!/bin/bash
echo “Hello there, would you like to learn how to hack: Y/N?”
read answer
echo “Your answer was $answer”
What ‘-p’ option do in read command?
Allows to specify a prompt.
What ‘-s’ option do in read command?
Makes the user input silent. The latter is ideal for capturing user credentials.
How to prompt user for input and silently read it using read?
#!/bin/bash # Prompt the user for credentials read -p 'Username: ' username read -sp 'Password: ' password echo "Thanks, your creds are as follows: " $username " and " $password
What is ‘#!’ in bash?
Shebang
What is ‘/bin/bash’ in Bash scripting file?
Absolute path.
What char is used to create comments in bash scripting?
#
How to print string “Hello World!” to terminal?
echo “Hello World!”
How to make script executable?
chmod +x
How to run script “hello-world.sh”?
./hello-world.sh
General syntax for the if statement in bash scripting?
if [ some test ]
then
perform an action
fi
How to use the if statement in Bash to check age, and decline if age is less than 16?
#!/bin/bash # if statement example read -p "What is your age: " age if [ $age -lt 16 ] then echo "DECLINE" fi
What is option ‘-lt’ in bash scripting?
Less than.
Describe operator !EXPRESSION
The EXPRESSION is false
Describe operator -n STRING
STRING length is greater than zero
Describe operator -z STRING
The length of STRING is zero (empty)
Describe operator STRING1 != STRING2
STRING1 is not equal to STRING2
Describe operator STRING1 = STRING2
STRING1 is equal to STRING2
Describe operator INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
Describe operator INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
Describe operator INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
Describe operator INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
Describe operator INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
Describe operator INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
Describe operator -d FILE
FILE exists and is a directory
Describe operator -e FILE
FILE exists
Describe operator -r FILE
FILE exists and has read permission
Describe operator -s FILE
FILE exists and it is not empty
Describe operator -w FILE
FILE exists and has write permission
Describe operator -x FILE
FILE exists and has execute permission
Describe operator -x FILE
FILE exists and has execute permission
Using the test command in an if statement, check age and decline if it’s less than 16.
#!/bin/bash # if statement example 2 read -p "What is your age: " age if test $age -lt 16 then echo "You might need parental permission to take this course!" fi
What is general syntax for the else statement?
if [ ]
then
else
fi
Using the else statement in Bash check if age is less than 16, if it is, then DECLINE, if it isn’t then APPROVE.
#!/bin/bash # else statement example read -p "What is your age: " age if [ $age -lt 16 ] then echo "DECLINE" else echo "APPROVE" fi
What is the elif syntax in bash?
if [ ]
then
elif [ ]
then
else
fi
How to use elif statement in bash to read from user his age, if it’s less than 16 - DECLINE, if it’s above 16 - APPROVE, else if it’s greater than 60 - RESPECT.
#!/bin/bash read -p "What is your age: " age if [ $age -lt 16 ] then echo "DECLINE" elif [ $age -gt 60 ] then echo "RESPECT" else echo "APPROVE" fi
Boolean logical operator AND in Bash?
&&
Boolean logical operator OR in Bash?
||
How to use the AND (&&) boolean operator, to check if user2 variable which you have declared as “kacper” is present in /etc/passwd file.
kacper@hopper:~$ user2=kacper
kacper@hopper:~$ user3=bob
kacper@hopper:~$ grep $user2 /etc/passwd && echo “$user2 found!”
kacper:x:1000:1000:,,,:/home/kacper:/bin/bash
kacper found!
kacper@hopper:~$ grep $user3 /etc/passwd && echo “$user3 found!” || echo “$user3 not found!”
bob not found!
What 2 types of loops do we have in bash?
for and while
What loops are designed for?
Loops help us with repetitive tasks that we need to run until a certain criteria is met.
General syntax of the for loop?
for var-name in
do
done
How to print 10.11.1.1 - 10.11.1.10 ip addresses using for loops in bash?
kali@kali:~$ for ip in $(seq 1 10); do echo 10.11.1.$ip; done
or
kali@kali:~$ for i in {1..10}; do echo 10.11.1.$i;done
How to use brace expansion using ranges in Bash to print IP addresses in 10.11.1.1-10 range?
kali@kali:~$ for i in {1..10}; do echo 10.11.1.$i;done
General syntax of the while loop
while [ ]
do
done
How to use while loop bash to print IP addresses in range 10.11.1.1-10?
#!/bin/bash # while loop example counter=1 while [ $counter -le 10 ] do echo "10.11.1.$counter" ((counter++)) done
How to write function in bash scripting?
function function_name { commands... }
OR
function_name () {
commands…
}
How to use bash function to print a message to the screen?
#!/bin/bash print_me () { echo "You have been printed!" } print_me
How to pass an argument to function in Bash?
#!/bin/bash # passing arguments to functions pass_arg() { echo "Today's random number is: $1" } pass_arg $RANDOM
How to return a value from Bash Function in this case?
return_me() {
echo “Oh hello there, I’m returning a random value!”
return $RANDOM
}
#!/bin/bash # function return value example return_me() { echo "Oh hello there, I'm returning a random value!" return $RANDOM } return_me echo "The previous function returned a value of $?"
How to declare a local variable “name” with value “kacper” using Bash Scripting?
local name=”kacper”
What is the output?
#!/bin/bash # var scope example name1="John" name2="Jason" name_change() { local name1="Edward" echo "Inside of this function, name1 is $name1 and name2 is $name2" name2="Lucas" } echo "Before the function call, name1 is $name1 and name2 is $name2" name_change echo "After the function call, name1 is $name1 and name2 is $name2"
kali@kali:~$ ./varscope.sh
Before the function call, name1 is John and name2 is Jason
Inside of this function, name1 is Edward and name2 is Jason
After the function call, name1 is John and name2 is Lucas
How to download the index.html page from megacorpone.com?
kali@kali:~$ wget www.megacorpone.com
How to indetify hyperlinks in the index.html file?
kali@kali:~$ grep “href=” index.html
How to use grep to grab lines that contains “.megacorpone”, indicating the existence of a subdomain from index.html?
grep “href=” index.html | grep “.megacorpone”
How to strip away lines that contains “www.megacorpone.com”?
grep -v “www.megacorpone.com”
How to tell awk, we want second field?
‘{print $2}’
What is ‘-F’ option in awk?
Multi-character delimiter
How to use awk with a unique delimiter search? We want to get from file “index.html” all hyperlinks, that contains megacorpone domain, but isn’t the boring www.megacorpone.com address and also doesn’t have HTTP://
kali@kali:~$ grep “href=” index.html | grep “.megacorpone” | grep -v “www.megacorpone.com” | awk -F “http://” ‘{print $2}’
How to use cut command to set the delimiter to “/”?
cut -d “/” -f 1
How to cut domain name with cut?
cut -d “/” -f 1
What is the most elegant solution with a regular expression to carve only .megacorpone.com from index.html file?
grep -o ‘[^/]*.megacorpone.com’ index.html | sort -u > list.txt
How to check ip address of domain “facebook.com”?
host facebook.com