Bash Scripting Flashcards
what is relative path?
location from the current directory
what’s absolute path?
something like /bin/ping
-used in case the program is not specified in PATH
~
current user’s home directory
*
wildcard used for finding certain types of files
output
results of command in a bash shell
command > file.txt
create or overwrite
command»_space; file.txt
create or append
what are 2 types of output redirection
- to a file
2. to another command
what bash character sends output to another command?
(pipe)
what is a “oneliner” script?
chaining multiple commands to automate a task
what does this oneliner do?
file ‘ls /etc/*conf’ | sort > test.txt && cat test.txt | wc -l
- anything between ‘’ operator gets evaluated first
- for each file listed (ls), it is sorted and put into a text file called test.txt
- && means execute what’s after it if the previous commands were successful
- read the test.txt file and print out the number of lines (wc = word count -l = lines)
what extension does a bash script file have?
.sh
what is the first line of any bash script file?
!/bin/bash
after saving a bash file, what should you make sure of?
make sure the file is executable, change the permissions
how do you make a file executable?
chmod+x
how do you run a script in linux terminal?
./
what is they syntax for a bash conditional statement?
if ; then commands elif ; then commands else commands fi
What do these operators mean?
- eq
- ne
equal
not equal
What do these operators mean?
- lt
- le
less than
less than or equal to
What do these operators mean?
- gt
- ge
greater than
greater than or equal to
what does this for loop do? #!/bin/bash
for i in $( ls ); do
echo item: $i
done
print out every item in a current directory
what does this for loop do? #!/bin/bash
for i in ‘seq 1 10’;
do
echo $i
done
uses the ‘seq’ command to iterate over numbers
what is the ‘while’ loops useful for
iterating over items in a file
what does the script below do?
while[condition]; do ; ; done
basic syntax to iterate over times
what does the script below do?
while read line; do echo $line; done < file.txt
read and print items from a file
what does grep do?
filter for the chosen keyword
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
- output a list of open ports
- remove the item with the word “filtered”
- print out only port numbers
- sort/remove duplicates, keep only unique
- put side by side
- 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)
what command issues http requests to see their responses
curl
aka command-line browser
where is the linux “black hole” where all sent items are deleted
/dev/null
what is one way you can use curl if you have a list of domains/IP?
you can use it to test domains/IP to see if they are an http or and https server
in an instance of curl, what flag allows you to format it’s output?
–write-out
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
- outputs http response from website and it’s response code
- removes unnecessary/unreadable output, \n adds new line, –silent removes errors
3.
what does the flag -L do for curl?
follow redirections
what does the flag –insecure do for curl?
bypass bad ssl error code
what does the following do?
timeout 5 curl -L –write-out “%{http_code}\n” –output /dev/null –silent –insecure http://google.com
- if response is taking too long it times out after 5 seconds
- follows redirect if any
- only returns response code and sends other output to black hole
- –silent doesn’t return any errors
- –insecure bypasses bad or no ssl