Bash Scripting Flashcards

1
Q

what is relative path?

A

location from the current directory

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

what’s absolute path?

A

something like /bin/ping

-used in case the program is not specified in PATH

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

~

A

current user’s home directory

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

*

A

wildcard used for finding certain types of files

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

output

A

results of command in a bash shell

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

command > file.txt

A

create or overwrite

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

command&raquo_space; file.txt

A

create or append

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

what are 2 types of output redirection

A
  1. to a file

2. to another command

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

what bash character sends output to another command?

A

(pipe)

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

what is a “oneliner” script?

A

chaining multiple commands to automate a task

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

what does this oneliner do?

file ‘ls /etc/*conf’ | sort > test.txt && cat test.txt | wc -l

A
  1. anything between ‘’ operator gets evaluated first
  2. for each file listed (ls), it is sorted and put into a text file called test.txt
  3. && means execute what’s after it if the previous commands were successful
  4. read the test.txt file and print out the number of lines (wc = word count -l = lines)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what extension does a bash script file have?

A

.sh

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

what is the first line of any bash script file?

A

!/bin/bash

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

after saving a bash file, what should you make sure of?

A

make sure the file is executable, change the permissions

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

how do you make a file executable?

A

chmod+x

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

how do you run a script in linux terminal?

A

./

17
Q

what is they syntax for a bash conditional statement?

A
if ; then
commands
elif ; then
commands
else
commands
fi
18
Q

What do these operators mean?

  • eq
  • ne
A

equal

not equal

19
Q

What do these operators mean?

  • lt
  • le
A

less than

less than or equal to

20
Q

What do these operators mean?

  • gt
  • ge
A

greater than

greater than or equal to

21
Q
what does this for loop do?
#!/bin/bash

for i in $( ls ); do
echo item: $i
done

A

print out every item in a current directory

22
Q
what does this for loop do?
#!/bin/bash

for i in ‘seq 1 10’;
do
echo $i
done

A

uses the ‘seq’ command to iterate over numbers

23
Q

what is the ‘while’ loops useful for

A

iterating over items in a file

24
Q

what does the script below do?

while[condition]; do ; ; done

A

basic syntax to iterate over times

25
Q

what does the script below do?

while read line; do echo $line; done < file.txt

A

read and print items from a file

26
Q

what does grep do?

A

filter for the chosen keyword

27
Q

what does the following do?

cat *.nmap | grep “open”
cat *.nmap | grep “open” | grep -v “filtered”
cat *.nmap | grep “open” | grep -v “filtered” | cut -d “/” -f 1
cat *.nmap | grep “open” | grep -v “filtered” | cut -d “/” -f 1 | sort -u
cat *.nmap | grep “open” | grep -v “filtered” | cut -d “/” -f 1 | sort -u | xargs
cat *.nmap | grep “open” | grep -v “filtered” | cut -d “/” -f 1 | sort -u | xargs | tr ‘ ‘ ‘,’
cat *.nmap | grep “open” | grep -v “filtered” | cut -d “/” -f 1 | sort -u | xargs | tr ‘ ‘ ‘,’ > ports.txt

A
  1. output a list of open ports
  2. remove the item with the word “filtered”
  3. print out only port numbers
  4. sort/remove duplicates, keep only unique
  5. put side by side
  6. save to file ports.txt

-d is a marker
-f specifies which element should be printed after splitting
1 is the first element
-u is unique
tr translate or delete characters (tr ,text2replace, replacement)

28
Q

what command issues http requests to see their responses

A

curl

aka command-line browser

29
Q

where is the linux “black hole” where all sent items are deleted

A

/dev/null

30
Q

what is one way you can use curl if you have a list of domains/IP?

A

you can use it to test domains/IP to see if they are an http or and https server

31
Q

in an instance of curl, what flag allows you to format it’s output?

A

–write-out

32
Q

what does the following do?

curl –write-out “%{http_code}” http://google.com

curl –write-out “%{http_code}\n” –output /dev/null –silent http://google.com

A
  1. outputs http response from website and it’s response code
  2. removes unnecessary/unreadable output, \n adds new line, –silent removes errors

3.

33
Q

what does the flag -L do for curl?

A

follow redirections

34
Q

what does the flag –insecure do for curl?

A

bypass bad ssl error code

35
Q

what does the following do?

timeout 5 curl -L –write-out “%{http_code}\n” –output /dev/null –silent –insecure http://google.com

A
  1. if response is taking too long it times out after 5 seconds
  2. follows redirect if any
  3. only returns response code and sends other output to black hole
  4. –silent doesn’t return any errors
  5. –insecure bypasses bad or no ssl