Bash Scripting Flashcards

1
Q

What is GNU Bourne-Again Shell (Bash)?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to create a simple ‘Hello World’ Bash script?

A
#!/bin/bash
# Hello World Bash Script
echo "Hello World!"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to declare a variable “first_name” as “Kacper”.

A

kali@kali:~$ first_name=Kacper

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to display “first_name” variable using Bash?

A

kali@kali:~$ echo $first_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to use of command substitution (whoami) and variables?

A

user=$(whoami)

user=whoami

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to instruct bash to print additional debug output, so we could more easily see the commands that were executed and their results.

A

!/bin/bash -x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe special Bash variable ‘$0’

A

The name of the Bash script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Describe special Bash variable ‘$1 - $9’

A

The first 9 arguments to the Bash script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe special Bash variable ‘$#’

A

Number of arguments passed to the Bash script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe special Bash variable ‘$@’

A

All arguments passed to the Bash script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe special Bash variable ‘$?’

A

The exit status of the most recently run process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe special Bash variable ‘$$’

A

The exit status of the most recently run process

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe special Bash variable ‘$USER’

A

The username of the user running the script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe special Bash variable ‘$HOSTNAME’

A

The hostname of the machine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe special Bash variable ‘$RANDOM’

A

A random number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe special Bash variable ‘$LINENO’

A

The current line number in the script

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How to collect user input using read “answer”?

A

!/bin/bash

echo “Hello there, would you like to learn how to hack: Y/N?”
read answer
echo “Your answer was $answer”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What ‘-p’ option do in read command?

A

Allows to specify a prompt.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What ‘-s’ option do in read command?

A

Makes the user input silent. The latter is ideal for capturing user credentials.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to prompt user for input and silently read it using read?

A
#!/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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is ‘#!’ in bash?

A

Shebang

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is ‘/bin/bash’ in Bash scripting file?

A

Absolute path.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What char is used to create comments in bash scripting?

A

#

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How to print string “Hello World!” to terminal?

A

echo “Hello World!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How to make script executable?

A

chmod +x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How to run script “hello-world.sh”?

A

./hello-world.sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

General syntax for the if statement in bash scripting?

A

if [ some test ]
then
perform an action
fi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How to use the if statement in Bash to check age, and decline if age is less than 16?

A
#!/bin/bash
# if statement example
read -p "What is your age: " age
if [ $age -lt 16 ]
then
echo "DECLINE"
fi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is option ‘-lt’ in bash scripting?

A

Less than.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Describe operator !EXPRESSION

A

The EXPRESSION is false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Describe operator -n STRING

A

STRING length is greater than zero

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Describe operator -z STRING

A

The length of STRING is zero (empty)

33
Q

Describe operator STRING1 != STRING2

A

STRING1 is not equal to STRING2

34
Q

Describe operator STRING1 = STRING2

A

STRING1 is equal to STRING2

35
Q

Describe operator INTEGER1 -eq INTEGER2

A

INTEGER1 is equal to INTEGER2

36
Q

Describe operator INTEGER1 -ne INTEGER2

A

INTEGER1 is not equal to INTEGER2

37
Q

Describe operator INTEGER1 -gt INTEGER2

A

INTEGER1 is greater than INTEGER2

38
Q

Describe operator INTEGER1 -lt INTEGER2

A

INTEGER1 is less than INTEGER2

39
Q

Describe operator INTEGER1 -ge INTEGER2

A

INTEGER1 is greater than or equal to INTEGER2

40
Q

Describe operator INTEGER1 -le INTEGER2

A

INTEGER1 is less than or equal to INTEGER2

41
Q

Describe operator -d FILE

A

FILE exists and is a directory

42
Q

Describe operator -e FILE

A

FILE exists

43
Q

Describe operator -r FILE

A

FILE exists and has read permission

44
Q

Describe operator -s FILE

A

FILE exists and it is not empty

45
Q

Describe operator -w FILE

A

FILE exists and has write permission

46
Q

Describe operator -x FILE

A

FILE exists and has execute permission

47
Q

Describe operator -x FILE

A

FILE exists and has execute permission

48
Q

Using the test command in an if statement, check age and decline if it’s less than 16.

A
#!/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
49
Q

What is general syntax for the else statement?

A

if [ ]
then

else

fi

50
Q

Using the else statement in Bash check if age is less than 16, if it is, then DECLINE, if it isn’t then APPROVE.

A
#!/bin/bash
# else statement example
read -p "What is your age: " age
if [ $age -lt 16 ]
then
echo "DECLINE"
else
echo "APPROVE"
fi
51
Q

What is the elif syntax in bash?

A

if [ ]
then

elif [ ]
then

else

fi

52
Q

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.

A
#!/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
53
Q

Boolean logical operator AND in Bash?

A

&&

54
Q

Boolean logical operator OR in Bash?

A

||

55
Q

How to use the AND (&&) boolean operator, to check if user2 variable which you have declared as “kacper” is present in /etc/passwd file.

A

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!

56
Q

What 2 types of loops do we have in bash?

A

for and while

57
Q

What loops are designed for?

A

Loops help us with repetitive tasks that we need to run until a certain criteria is met.

58
Q

General syntax of the for loop?

A

for var-name in
do

done

59
Q

How to print 10.11.1.1 - 10.11.1.10 ip addresses using for loops in bash?

A

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

60
Q

How to use brace expansion using ranges in Bash to print IP addresses in 10.11.1.1-10 range?

A

kali@kali:~$ for i in {1..10}; do echo 10.11.1.$i;done

61
Q

General syntax of the while loop

A

while [ ]
do

done

62
Q

How to use while loop bash to print IP addresses in range 10.11.1.1-10?

A
#!/bin/bash
# while loop example
counter=1
while [ $counter -le 10 ]
do
echo "10.11.1.$counter"
((counter++))
done
63
Q

How to write function in bash scripting?

A
function function_name {
commands...
}

OR

function_name () {
commands…
}

64
Q

How to use bash function to print a message to the screen?

A
#!/bin/bash
print_me () {
echo "You have been printed!"
}
print_me
65
Q

How to pass an argument to function in Bash?

A
#!/bin/bash
# passing arguments to functions
pass_arg() {
echo "Today's random number is: $1"
}
pass_arg $RANDOM
66
Q

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
}

A
#!/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 $?"
67
Q

How to declare a local variable “name” with value “kacper” using Bash Scripting?

A

local name=”kacper”

68
Q

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"
A

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

69
Q

How to download the index.html page from megacorpone.com?

A

kali@kali:~$ wget www.megacorpone.com

70
Q

How to indetify hyperlinks in the index.html file?

A

kali@kali:~$ grep “href=” index.html

71
Q

How to use grep to grab lines that contains “.megacorpone”, indicating the existence of a subdomain from index.html?

A

grep “href=” index.html | grep “.megacorpone”

72
Q

How to strip away lines that contains “www.megacorpone.com”?

A

grep -v “www.megacorpone.com”

73
Q

How to tell awk, we want second field?

A

‘{print $2}’

74
Q

What is ‘-F’ option in awk?

A

Multi-character delimiter

75
Q

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://

A

kali@kali:~$ grep “href=” index.html | grep “.megacorpone” | grep -v “www.megacorpone.com” | awk -F “http://” ‘{print $2}’

76
Q

How to use cut command to set the delimiter to “/”?

A

cut -d “/” -f 1

77
Q

How to cut domain name with cut?

A

cut -d “/” -f 1

78
Q

What is the most elegant solution with a regular expression to carve only .megacorpone.com from index.html file?

A

grep -o ‘[^/]*.megacorpone.com’ index.html | sort -u > list.txt

79
Q

How to check ip address of domain “facebook.com”?

A

host facebook.com