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
How to make script executable?
chmod +x
26
How to run script "hello-world.sh"?
./hello-world.sh
27
General syntax for the if statement in bash scripting?
if [ some test ] then perform an action fi
28
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 ```
29
What is option '-lt' in bash scripting?
Less than.
30
Describe operator !EXPRESSION
The EXPRESSION is false
31
Describe operator -n STRING
STRING length is greater than zero
32
Describe operator -z STRING
The length of STRING is zero (empty)
33
Describe operator STRING1 != STRING2
STRING1 is not equal to STRING2
34
Describe operator STRING1 = STRING2
STRING1 is equal to STRING2
35
Describe operator INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
36
Describe operator INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
37
Describe operator INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
38
Describe operator INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
39
Describe operator INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
40
Describe operator INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
41
Describe operator -d FILE
FILE exists and is a directory
42
Describe operator -e FILE
FILE exists
43
Describe operator -r FILE
FILE exists and has read permission
44
Describe operator -s FILE
FILE exists and it is not empty
45
Describe operator -w FILE
FILE exists and has write permission
46
Describe operator -x FILE
FILE exists and has execute permission
47
Describe operator -x FILE
FILE exists and has execute permission
48
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 ```
49
What is general syntax for the else statement?
if [ ] then else fi
50
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 ```
51
What is the elif syntax in bash?
if [ ] then elif [ ] then else fi
52
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 ```
53
Boolean logical operator AND in Bash?
&&
54
Boolean logical operator OR in Bash?
||
55
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!
56
What 2 types of loops do we have in bash?
for and while
57
What loops are designed for?
Loops help us with repetitive tasks that we need to run until a certain criteria is met.
58
General syntax of the for loop?
for var-name in do done
59
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
60
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
61
General syntax of the while loop
while [ ] do done
62
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 ```
63
How to write function in bash scripting?
``` function function_name { commands... } ``` OR function_name () { commands... }
64
How to use bash function to print a message to the screen?
``` #!/bin/bash print_me () { echo "You have been printed!" } print_me ```
65
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 ```
66
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 $?" ```
67
How to declare a local variable "name" with value "kacper" using Bash Scripting?
local name="kacper"
68
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
69
How to download the index.html page from megacorpone.com?
kali@kali:~$ wget www.megacorpone.com
70
How to indetify hyperlinks in the index.html file?
kali@kali:~$ grep "href=" index.html
71
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"
72
How to strip away lines that contains "www.megacorpone.com"?
grep -v "www\.megacorpone\.com"
73
How to tell awk, we want second field?
‘{print $2}’
74
What is '-F' option in awk?
Multi-character delimiter
75
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}'
76
How to use cut command to set the delimiter to "/"?
cut -d "/" -f 1
77
How to cut domain name with cut?
cut -d "/" -f 1
78
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
79
How to check ip address of domain "facebook.com"?
host facebook.com