Basics Of Shell Scripting

Basics Of Shell Scripting

Shell is a way to talk to your Operating System.

We can communicate with the O.S. using Shell Commands. These shell commands are common through different Unix distributions. A Shell provides you with an interface to the Unix system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output.

You can refer to this blog to create Ubuntu-related EC2 instances so that you have a Linux environment. Link

Get Started

We will first start with whether we are in a bash shell or not. We will be working with the bash shell. To install bash.....

sudo apt update
sudo apt install bash
echo $SHELL   //to know current shell

To change your shell to Bash shell refer this: https://www.cyberciti.biz/faq/how-to-change-shell-to-bash/

COMMANDS

To list the files and folders in a particular directory

ls

To know current working directory

pwd
/home/ubuntu

To change the directory

ubuntu@ip-172-31-28-28:~$ ls
myfolder
ubuntu@ip-172-31-28-28:~$ cd myfolder
ubuntu@ip-172-31-28-28:~/myfolder$
  1. Right now we are in myfolder to go back one directory we use cd ..

  2. If we want to go back two directories we use cd../..

ubuntu@ip-172-31-28-28:~/myfolder$ cd ..
ubuntu@ip-172-31-28-28:~$

If we want to list all folder with timestamp, owner of file or is file/directory. We list the files and folder with its properties

ubuntu@ip-172-31-28-28:~$ ls -ltr
total 4
drwxrwxr-x 2 ubuntu ubuntu 4096 May 16 16:14 myfolder

To create a file

ubuntu@ip-172-31-28-28:~$ touch vijay
ubuntu@ip-172-31-28-28:~$ ls
myfolder  vijay

To write inside the file we use vi command. We enter VIM editor . Here we can press escape button and then press i button to write inside the file . Again to save the file wth its contents press escape and type :wq .

ubuntu@ip-172-31-28-28:~$ vi vijay
ubuntu@ip-172-31-28-28:~$ cat vijay
Hello My name is Vijay

To make a directory we use mkdir command

ubuntu@ip-172-31-28-28:~$ mkdir devops
ubuntu@ip-172-31-28-28:~$ ls
devops  myfolder  vijay

To delete a directory we use : rm -r <Directory_Name>

To check our System/Server properties using Shell commands

ubuntu@ip-172-31-28-28:~$ free -g
               total        used        free      shared  buff/cache   available
Mem:               0           0           0           0           0           0
Swap:              0           0           0

If we want to check number of CPU's

ubuntu@ip-172-31-28-28:~$ nproc
1

Disk size used

ubuntu@ip-172-31-28-28:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       7.6G  1.7G  5.9G  23% /
tmpfs           484M     0  484M   0% /dev/shm
tmpfs           194M  848K  193M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
/dev/xvda15     105M  6.1M   99M   6% /boot/efi
tmpfs            97M  4.0K   97M   1% /run/user/1000

To see running processes

If we want to monitor CPU percentage, memory management we use top command.

ubuntu@ip-172-31-28-28:~$ top
top - 16:46:01 up 57 min,  1 user,  load average: 0.00, 0.00, 0.00
Tasks:  98 total,   1 running,  97 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.3 sy,  0.0 ni, 99.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    966.2 total,    162.0 free,    206.8 used,    597.4 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.    605.1 avail Mem