Skip to content

Bash

shebang #!

#! /bin/bash

make script executable

chmod +x hack.sh

execute file

./hack.sh

variables

TARGET=10.10.99.99
echo $TARGET

Arguments

#!/bin/bash
echo "Arg-1 $1 arg-2 $2"
Key Value
$0 The name of the Bash script
$1 - $9 The first 9 arguments to the Bash script
$# Number of arguments passed to the Bash script
$@ All arguments passed to the Bash script
$? The exit status of the most recently run process
$$ The process ID of the current script
$USER The username of the user running the script
$HOSTNAME The hostname of the machine
$RANDOM A random number
$LINENO The current line number in the script

Assign arguments

#!/bin/bash
echo "What is your anme ?"; 
read name;
echo "Your name is $name";

Assign output

#!/bin/bash
user=$(whoami)
echo $user kali

If / Else / Elif

If (string)

#!/bin/bash
echo "Select scan XSS/SQLi";

read -p "Attack name:" action

if [ $action == 'XSS' ]; then
 echo "XSS attack..."; 
fi

If (number)

#!/bin/bash
echo "Number of request";

read -p "Nr:" action

if [ $action -lt 10  ]; then
 echo "< 10";
fi

if [ $action -eq 10 ]; then
 echo "== 10"; 
fi

if [ $action -gt 10  ]; then
 echo "> 10";
fi

If-Else

#!/bin/bash
echo "Number of request";
read -p "Nr:" action
if [ $action -eq 10  ]; then
 echo "OK";
else
 echo "No OK"
fi

If-ElseIf-Else

#!/bin/bash
echo "Number of request";
read -p "Nr:" action

if [ $action -eq 10  ]; then
 echo "OK.1";
elif [ $action -eq 20 ]; then
 echo "OK.2";
else    
 echo "No OK"
fi

Logical Operations

grep 'kali' /etc/passwd && echo "User exist"
grep 'john' /etc/passwd || echo "User NOT exist"
[ $(whoami) == 'kali' ] && echo "Not root user"
[ $(whoami) == 'root' ] && echo "Root user"
[ $(whoami) != 'root' ] || echo "Ah NOT ROOT"

For

#!/bin/bash
for ip in $(seq 1 10); do 
  echo 10.11.1.$ip;
done
#!/bin/bash
for ip in {1..10}; do 
        echo 10.11.1.$ip; 
done

While

#!/bin/bash
index=1
while [ $index -le 5 ]
do
  echo "10.10.10$index"
  index=$(( $index + 1 ))
done

Function

#!/bin/bash
banner(){
  echo "Super HaCkEr Engine";
}

banner;
banner;