Giter Site home page Giter Site logo

bash-scripting-cheatsheet's Introduction

NOTES

To save and get out of anything press Ctrl+D


You get can all the referencial scripts in here.

run these commands in root terminal else it might give you some weird error

apt-get update && apt-get upgrade -y
apt-get -y install open-vm-tools-desktop && reboot
 apt-get -y install fuse  

Adding a shared drive in vmware

First of all create a directory in windows that you want to share
Then Right-Click on the folder -> Properties -> Sharing -> Advanced sharing -> Check the 'Share this folder' Option -> Permissions -> Check 'Full -control' for 'Everyone'
Enable folder sharing in the VMWARE as 'Always Enabled'
after that run the following command no matter how long it takes run this command(make sure you are in sudo su) in kali:

Post that run the following command

cd /mnt

Use the above command to move to the mnt folder then use the command

sudo mkdir hgfs

After that use the command

  sudo mount -t fuse.vmhgfs-fuse .host:/ /mnt/hgfs -o allow_other

BASH SCRIPTING CHEATSHEET

TYPES OF SHELLS AVAILABLE

cat /etc/shells

This command gives you the list of all the available shells Checkout Command 2

'which < shell-script >' command to get the location of a particular script

which bash

This command gives you the location of bash which you have to include in the bash script
Checkout Command 3 and its output


Writing my First Bash Script

First Script! Script Traversing and storing it in the Desktop and gaving it the execution command and executing it Commands


REDIRECTION OF FILES

Writing the output of a file into another file

To write the output of a file into another file. Here is how you do it! Example

Taking the input from terminal and writing the output into another file

To write into another file with scripting, here is how you do it! Example
Output for the same Output

NOTE: Press Ctrl-D after finishing the cat on terminal to save it

To append the output of the script in the file use '>>' . Example
Check out the output of the script here


CONDITIONAL STATEMENTS

Reference Script

if-else-elseif

Checkout the script for the if-else and its respective output as well.

if-else with && operator

Checkout the script for the if-else-and-operator and its respective output as well.

if-else with || operator

Checkout the script for the if-else-or-operator and its respective output as well.

switch-Case

Checkout the SWITCH CASE and its respective output as well.
Script 1
Script 2


LOOPING STATEMENTS

While Loops

While loops run until the condition is false.
To use while loops Example

Until Loops

Until Loops run until the condition is true.
To use until loops Example

For Loops

There are multiple ways of runnning a FOR Loop:
Normal loop. Example 1
Normal loop with a step function. Example 2
Traditional For LoopExample 3

Break Statement in Loops

Break Statement stops the entire execution of the loop and exits the loop.
Example

Continue Statement in Loops

Where the Break Statement stops the entire execution of the loops and exits the loop, the continue statement only skips that one specific iteration of the loops and continues it execution further.
Example


SCRIPT I/O STATEMENTS

Taking inputs from User

Bounded number of inputs can taken from the user through terminal.
Exameple

Taking unbounded inputs from User

Bounded number of inputs can taken from the user through terminal.
Example

echo $# to print out the length of the array

To find out the length of the array we use the command 'echo #$'.
Example

reading a file content from a file using /dev/stdin

To read a file content as an argument from the terminal '${1:-/dev/stdin}'.
Example 2 here file.txt is given as an argument
NOTE: If any file name is not passed as an argument then the script acts as a cat for the terminal
Example 1 where there is no filename passed as an argument

reading a file content from a file without stdin

To read a file content without the use of '${1:-/dev/stdin}', just replace '${1:-/dev/stdin}' with the filename.
Example where file.txt has been replaced at the location of the file

"stdop" and "stderr" writing the output or an error of a script into a file

ls -al 1>fileOUTPUT.txt 2>fileErr.txt

For this refer this VIDEO from time 57:04-1:02:00


PIPES

Pipes are also known as - "Send inputs of one script to another script"

First PIPES script

Sending a message from one scripts output as an input to another script.
Example


STRING PROCESSING OR MANUPILATION

First String processing script

Checking if the Strings are equal or not.
Example

Check to see if the strings are of the same sizes or not

'/>' and '</' are the two operators required for checking if the string is of the smaller or greater size.
Example

Concetinating two strings

Example

Changing cases of some two strings - Sentence Case, Upper Case

Example


NUMBERS AND ARTHIMETIC

First Program
Second Program


DECLARE COMMAND - declare variables

declare <Variable-name> = <value>

declare command is used to declare a variable. Example

Displaying all the declared variables in the past

declare -p 

This command gives you the output and shows you all the previously declared variables. Example

Declaring the variable and making it read only so that it cannot be modified

declare -r psswdLocation = /etc/passwd 

declaring the variable with -r converts a traditional variable to a read-only variable Example


ARRAYS

Syntax for declaring an Array

variableName = ('Value1','Value2','Value3')

Example
At line number 6 we use the '!' operator to print the index of the elements
Line 7 shows how to find out the size of the array
We use unset method to discard the value at the index 1


FUNCTIONS

First function example shows how to define and call a function
Second function example shows how to define and call an argumentative function


FILES AND DIRECTORIES

mkdir -p <directory-name> 

Using mkdir command in the script to create a new directory in the current directory
Example

To check if the directory exists or not

Script

To create file with a parameter name

Script

To create a file if does not exist and then appending the data in the file

Script

To read from a file if it exist

Script


CURL

url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
CURL ${url}

the above command is used to download different stuff from the URL

url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
curl ${url} -O

The above command with flag -O is used to download the file with the orginal filename as stored in the remote location

url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
curl ${url} -o Filename

The above command with flag -o: where o represents options, with -o we append the filename which would be given to the file being downloaded.
An Alternative to the above command is:

url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
curl ${url} > Filename

The below command is used to download the header(if you want to check if a 10GB of file is the exact one that you are looking for or not then you dont necessarily have to download the entire file to check, you can just download the header of the file) of the file.

url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
curl ${url} -I Filename

PROFESSIONAL MENUS

First we look at the select loop

#! /usr/bin/bash
select car in TOYOTA TESLA MARUTI TATAMOTORS
do 
        echo "You have selected $car as your favorite option!!"
done

Now instead of echoing out the output we can write switch statements

#! /usr/bin/bash
select car in TOYOTA TESLA MARUTI TATAMOTORS
do 
        case $car in 
        TOYOTA) 
                    echo "You selected $car as your option!!";;
        TESLA) 
                    echo "You selected $car as your option!!";;
        MARUTI) 
                    echo "You selected $car as your option!!";;
        TATAMOTORS) 
                    echo "You selected $car as your option!!";;
        *)
                    echo "Please enter a valid choice!!";;
        esac
        
done

In the below code after the function body we see that there is a while loop

#! /usr/bin/bash
function FuncCar(){
    select car in TOYOTA TESLA MARUTI TATAMOTORS
    do 
            case $car in 
            TOYOTA) 
                        echo "You selected $car as your option!!";;
            TESLA) 
                        echo "You selected $car as your option!!";;
            MARUTI) 
                        echo "You selected $car as your option!!";;
            TATAMOTORS) 
                        echo "You selected $car as your option!!";;
            *)
                        echo "Please enter a valid choice!!";;
            esac
            
    done
}

echo "press any ket to continue"
while [ true ]
do 
    read -t 3 -n 1
    if [ $? = 0 ]
    then 
        echo "you have terminated the script"
        FuncCar
        exit;
    else
        echo "Waiting for you to press a key!!!"
    fi
done

After the while loop we in the while body we have read command with a flag -t which stands for the time
That is "-t 3 -n" together means after 3 seconds interval go to a new line and execute the code in the loop.


I NODE NOTIFY - iNOTIFY

It is basically a notification facility that alerts you on changes in files and directories
In other words it is used to monitor files and directories.
First of all we have to install iNotify with:

sudo apt install inotify-tools

NOTE: We can either monitor the directory or file that has been created or for learning purpose include a file or a directory that has already been created

After the installation we create a script that will be used to monitor a file or a directory:

inotify -m directory/file-name

GREP COMMAND

grep stringToBeSearched fileInWhichStringIsToBeSearched

The above command is used to search for a string
We can also accomplish other tasks for example counting the occurences of the string in the file or to cat everything except that string that is to be searched
All this can be accomplished with the help of different flags
Let's take an example, first we need a file filled with raw data that is to be examined, then we run grep in the file by giving the appropriate locations in the script

grep stringToBeSearched fileInWhichStringIsToBeSearched

With no flags the grep just lists out all the occurences of a particular string

grep -i stringToBeSearched fileInWhichStringIsToBeSearched

Grep with the flag -i works just as same as the above command

grep -i -n stringToBeSearched fileInWhichStringIsToBeSearched

Grep with the -n command tells you the numbers at which lines the string resides

grep -i -n -c stringToBeSearched fileInWhichStringIsToBeSearched

-c flag is used to count the number of occurences of the string in the file

grep -i -v stringToBeSearched fileInWhichStringIsToBeSearched

-v flag is used to give the output of everything except the searched string


AWK

AWK is a scripting language that is used to manupilate data and generating reports
It makes it easier for string manupilation and arthimetic operations
Just like bash which has an extension of .sh awk has na extension of .awk
Utility that is tiny but powerful
Writing our First Program to print everything in the raw file
Second program is to print the lines in which Linux exists!
Third Program is if we want to display 2 word of a line or 3 word of a line then we use this script.


SCREEN EDITOR

Also used for string and data manupilation
In first program we substitute where we replace 'i' with 'I'. 's/i/I/g' where s is for substitute 'i' is to replace with the 'I' and 'g' stands for global
Second program is just another way to achieve the same thing
If noticed carefully it does not really make any changes in the raw.txt file so instead of making changes in the existing file it is recommended that we save the output in a different file
Following code of the third program will help you for that.


DEBUGGING THE BASH SCRIPT

bash +x ./scriptName.sh

use the above command to debugg your script

#! /usr/bin/bash -x

Appending the '-x' flag to your bash location at the start of the script will make you debugg your script as well

set-x
---------------
---------------
---------------
---------------
set +x

The above method is used to set a start point and an end point which basically means that the program between the set -x and set +x will be debugged


WORKING WITH SPLUNK

Splunk is a SIEM TOOL that helps in making 'data in to moving' which means, that it follows a client-server model and collects logs from different client machines and forwards it to the reciever.
We have a centralized reciever that recieves the data from the variety of clients.
Note: Make sure to perform below commands using the 'sudo su' which is using the root previlege:

wget -O splunk-8.2.4-87e2dda940d1-Linux-x86_64.tgz 'https://download.splunk.com/products/splunk/releases/8.2.4/linux/splunk-8.2.4-87e2dda940d1-Linux-x86_64.tgz'

After the dowload use the below command to install
Make sure that you have the root previleges and that you are in the '/' folder while using this command

 tar -zxvf yourTGZfile.gz

After installation check if you have the splunk installed and have all its respective directories like 'bin', 'etc'

cd /splunk
ls
cd bin
ls

If everything is all good till here the use the following command to start the splunk
Make sure that you are in /splunk/bin directory while using this command

./splunk start --accept-license

After that enter the admin creation id and passward: for example for me it is user:admin and password:admin@143
After installing the splunk in your kali, you can configure it to recieve the data from the monitoring system.
We download a splunk universal forwarder which gathers all the data needed and forwards it to the splunk reciever.
For reference walkthrough use the following tutorial.

bash-scripting-cheatsheet's People

Contributors

sagar98cyber avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.