Bash
For-loops
Another way to write this is by using the program seq
. Seq is pretty much like range()
in python. So it can be used like this:
If/Else
Command line arguments
Command line arguments are represented like this
This is the first command line argument
Daemonize an execution
If you do a ping-sweep with host the command will take about a second to complete. And if you run that against 255 hosts I will take a long time to complete. To avoid this we can just deamonize every execution to make it faster. We use the &
to daemonize it.
Use the output of command
It has happened to me several times that I want to input the output of a command into a new command, for example:
I search for a file, find three, and take the last line, which is a path. Now I want to cat that path:
This can be done like this:
Iterate over a file
This script will iterate over a file and echo out every single line
Another way of writing is this
picoCTF flag save
#Create script with below
#File save.sh and chmod +x
#!/bin/bash
original_directory=$(pwd)
cd ..
mv $original_directory ${original_directory}_COMPLETED
#run as follows
source script.sh
#save flag script
#!/bin/bash
winning_command=$(history | tail -n 1 | cut -b 8-)
#Create a new script within this script to pass the value of winning_command
#EOF > End of file
cat <<EOF>./get_flag.sh
#!/bin/bash
${winning_command}
EOF
chmod +x get_flag.sh
get_flag.sh > flag.txt
Running the above must be done after obtaining the flag
run the command .\run | grep -oE "picoCTF{.*}" --color=none
Run only the save.sh
Now that would have created a get_flag.sh and a flag.txt
you can cat the txt file for the flag