h4rithdpentest

$Target variables

Set it once. Every <IP>, <USER> or <DOMAIN> placeholder in the notes is rewritten live, and the copy buttons copy your values.

Stored in this browser only
h4rithd.com ↗

13Languages

Bash

Read
3 min
Blocks
16
Updated
Target

/bin/bash

bash9 lines
## ------------------| Environment Variables
env # List all envs
echo $PATH # List path env
echo $USER # Current user
echo $HOME # Current user's home
echo $PWD # Current workind dir
echo $HISTSIZE # Number of commnds (in memory/current session)
echo $HISTFILESIZE # How many commands in ~./history
export ip="0.0.0.0" # Make ip as global variable

01 Introduction

Shebang

bash1 line
#!/bin/bash

Arrays

bash2 lines
array=(hello this is an array 123)
echo ${array[2]}

Arguments

text9 lines
## ------------------| Maximam 9 ($0-$9)
./script.sh ARG1 ARG2 ARG3 ... ARG9
$0 $1 $2 $3 ... $9
## ------------------| Special Variables / Internal Field Separator (IFS)
$# Number of arguments passed to the script.
$@ Retrieve the list of command-line arguments.
$$ The process ID of the currently executing process.
$? The exit status of the script. ( 0 <= SUCC , 1 <= FAIL).

Comparison Operators

  • String Operators
text6 lines
== is equal to [EX: "$1" == "HackTheBox"]
!= is not equal to
< is less than in ASCII alphabetical order & works only within the double square brackets [[ <condition> ]]
> is greater than in ASCII alphabetical order & works only within the double square brackets [[ <condition> ]]
-z if the string is empty (null) (EX: [[ -z $1 ]])
-n if the string is not null
  • Integer Operators
text6 lines
-eq is equal to
-ne is not equal to
-lt is less than
-le is less than or equal to
-gt is greater than
-ge is greater than or equal to
  • File Operators
bash11 lines
-e file exist ?
-f a file ?
-d a directory ?
-L a symbolic link ?
-O current user owns the file ?
-r the file has read permission ?
-w the file has write permission ?
-x the file has execute permission ?
-s the file has a size greater than 0 ?
-G the file’s group id matches the current user’s ?
-N checks if the file was modified after it was last read
  • Logical Operators
bash3 lines
&& logical AND (Ex: [[ -e "$1" && -r "$1" ]])
! logical negotation NOT
|| logical OR

Arithmetic Operators

bash8 lines
+ Addition
- Substraction
* Multiplication
/ Division
% Modulus
variable++ Increase the value of the variable by 1
variable-- Decrease the value of the variable by 1
${#variable} Length of the variable

Input / Output

  • Inputs
bash4 lines
read -p "Select your option: " option
echo $option
-p Input remains on the same line
  • Output
bash3 lines
..... | tee -a filename.txt
-a / --append

02 Flow Control

Loops

  • For Loops
bash14 lines
## ------------------| With Numbers
for $i in 1 2 3 4
do
echo $i
done
## ------------------| With Files
for $words in file1 file2 file3
do
echo $words
done
## ------------------| One line
for i in {1..5};do echo $i;done
  • While Loops
bash5 lines
## A statement is executed as long as a condition is fulfilled (true).
while [ <CONDITION> ]
do
<TASK>
done
  • Until Loops
bash8 lines
## The code inside a until loop is executed as long as the particular condition is false.
counter=0
until [ $counter -eq 10 ]
do
# Increase $counter by 1
((counter++))
echo "Counter: $counter"
done

If Else

bash9 lines
if [ <CONDITION> ]
then
<TASK-I>
elif [ CONDITION ]
then
<TASK-II>
else
<TASK-FINAL>
fi

Case Statements

bash5 lines
case <expression> in
pattern_1 ) statements ;;
pattern_2 ) statements ;;
"*") exit 0 ;;
esac

Functions

bash9 lines
## ------------------| Method 1
function name {
<commands>
}
## ------------------| Method 2
name() {
<commands>
}
  • Parameter Passing
bash7 lines
function print {
echo $1 $2 $3
}
two="Second"
print_pars "First" "$two" "3"
  • Return Values (Return Code)
bash8 lines
1 General errors
2 Misuse of shell builtins
126 Command invoked cannot execute
127 Command not found
128 Invalid argument to exit
128+n Fatal error signal "n"
130 Script terminated by Control-C
255\* Exit status out of range