[go: up one dir, main page]

0% found this document useful (0 votes)
7 views37 pages

104 - Basics of Bash Scripting

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views37 pages

104 - Basics of Bash Scripting

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

01

Shell

The power of Linux


AGENDA
1. Part 1: Shell in Linux

2. Part2: Shell commands

3. Part3: additional commands

4. Part4: Processes

5. Part5: bash code


Shell in Linux
● Simply the shell is a program that takes commands
from the user and gives them to the operating
system to perform.

● In other words we can say that, shell is the layer of


programming that understands and executes the
commands a user enters.

● In some systems, the shell is called a command interpreter.


Linux User Kernel
Commands
Linux User Kernel

SHELL
The Programming interface/Interpreter

Execute the CMD


Shell in Linux
● There are different flavors of a shell, just as there are different flavors of
operating systems.

● Each flavor of shell has its own set of recognized commands and
functions.

● On most Linux systems a program called bash (which stands for Bourne Again
SHell, an enhanced version of the original Unix shell) program
sh (written by Steve Bourne) acts as the shell program.

● Besides them, there are other shell programs available for Linux
systems. These include: ksh, tcsh and zsh.
User

Application Shell

Low level utilities and modules

Kernel
Terminal
● Terminal is a program that opens a window and lets you interact with
the shell.

● There are a bunch of different terminal emulators we can use.

● These might include:


○ gnome-terminal
○ Konsole
○ Xterm
○ Rxvt
○ Kvt
○ nxterm
○ eterm.
User

Terminal

Application Shell

Low level utilities and modules

Kernel
02

Shell Commands

Enjoy the power of shell !


Shell Commands

● Shell is powerful program that allow you to perform a lot of commands


to save your effort, time and many more

● Some of commands we are going to discuss are:


○ Echo
○ > and >>
○ grep
○ Piping “|”
○ Head
○ Tail
○ ; and &&
○ Locate
○ The &
Shell Commands : Echo

● Echo command allows you to print out the string or other on the
terminal

● $ echo “Hello .. This is Banhawi”


Shell Commands : > and >>

● These two symbols “>” and “>>” let you save the command output into
a file

● The difference between them is:


○ “>” will delete whatever pre-stored in the file and save the new
output into this file

○ “>>” will abend over whatever pre-stored in the file


Shell Commands : grep

● Grep command let you filter or search for specific word inside a file

● Suppose you listed /etc subfile and subdirectories and saves it into
Test.txt on the Desktop

● This file contains a huge files and to search for specific file you need to
use “grep” command
Shell Commands : piping “|”

● Piping “|” let you to do another command after the first command be
successfully performed.

● So if you need to list the subdirectories and store it after you can do it
using this command too.

● Thus we can say that piping command always related to the previous
command, it will perform an additional command on the output of the
first command
Shell Commands : head

● Head command let you view the top lines of any file.

● You can specify number of lines you need to view using the argument
-n
Shell Commands : tail

● Tail command let you view the bottom lines of any file.

● You can specify number of lines you need to view using the argument
-n
Shell Commands : “;” and &&

● The semicolon “;” and “&&” allow you to do two or more different
command in the same line one after one.
Shell Commands : locate

● To search for some file or directory, locate is one of the command you
can use to search for this file or directory.

● As we see, we tried to search for the file “rockyou.txt” in the system


and the location is: /usr/share/wordlist/
Shell Commands : &

● Now suppose you need to perform a command however you know that
this command will take a lot of time to be executed.

● The proper way to do this is to run this command in the background


and here come the power of “&”

● & let you perform your command in the background while you perform
other tasks.

● All you need to do is to type your command and at the very end just
type “&”
Shell Commands : &

● As you can see below after typing the command, a new process
initiated with PID: 2542 to perform this task in the background

● After the task has been finished the terminal notified you with that this
task has been done successfully

● If you need to see the live process


that take a place right now only you
need to type
$ top
03
Additional
bash
Commands
Shell Commands

● Now we are going to discuss more commands to use.

● Some of commands we are going to discuss are:


○ wget
○ Sort
○ Cut
Shell Commands : wget

● Wget let you download the index page of any website.

● For example, let’s wget the facebook page


Shell Commands : wget

● Now if you cat the index file that has just downloaded you will see a
huge file
Shell Commands : wget

● Let us grep it to get only facebook.com


● To do it in effective way just type:
$ cat index.html | grep -i -o “facebook.com”
○ -i >> to ignore the case sensitivity
○ -o >> to restrict the output on “facebook.com” only
Shell Commands : wget

● But if you are looking of the subdomains of facebook you need to


retype the command as following:
$ cat index.html | grep -i -o “[a-z0-9]*.facebook.com”
Shell Commands : sort

● As you can see in the previous slide, there are repeated outcomes we
need to eliminate them

● To do so and get only the unique values, use the sort function as
following:
$ cat index.html | grep -i -o “[a-z0-9]*.facebook.com” | sort -u
Shell Commands : cut

● Now suppose you only need the first part of the domain and ignore the
.facebook.com

● To do so, use cut function with the delimiter as following:


$ cat index.html | grep -i -o “[a-z0-9]*.facebook.com” | sort -u | cut -d “.” -f1
04
Processes
What is doing behind !
More Commands
● Processes in linux is something similar to the task manager in
Windows.

● It shows you the process running in the background with more extra
information

● A lot of commands used for this task:


○ Ps
○ Ps -aux
○ Top
○ htop
05

Bash scripting
Bash Scripting
● In bash scripting you can perform all of the previous tasks and save
them as sh or bash file to be executed automatically once needed.

● Let us try to make our examples ...

You might also like