Saturday, July 9, 2022

Git - Distributed Version Control System

Git - Distributed Version Control System

Git is a version control system.

Git is developed by Linus Torvald.

He created Git while working on Linux Kernel Development.

It is designed to record changes on the filesystem.

Git gives us ability to revert the files or the set of files or the set of files you made changes.


Three Kind Of Version Control System:

1)Local Version Control System. Ex: We can take backup with date

2)Central Version Control System. Ex: SVN

 - All files are stored on single central server    

 


3)Distributed Version Control System. Ex: Git




Git Installation on windows and Linux Server:

Windows: How to Install Git on Windows {Step-by-Step Tutorial} - PhoenixNAP

Linux: How to Install and Configure Git on Linux (makeuseof.com)


How Git Works?





Git Commands:

Uploading: 220642 of 220642 bytes uploaded.


User@DESKTOP-75I86E3 MINGW64 ~

$ git config -l

diff.astextplain.textconv=astextplain

filter.lfs.clean=git-lfs clean -- %f

filter.lfs.smudge=git-lfs smudge -- %f

filter.lfs.process=git-lfs filter-process

filter.lfs.required=true

http.sslbackend=openssl

http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt

core.autocrlf=true

core.fscache=true

core.symlinks=false

pull.rebase=false

credential.helper=manager-core

credential.https://dev.azure.com.usehttppath=true

init.defaultbranch=master


User@DESKTOP-75I86E3 MINGW64 ~

$ git config --global user.name "kishoraswar"


User@DESKTOP-75I86E3 MINGW64 ~

$ git config --global user.email "kishoraswar22@gmail.com

> "


User@DESKTOP-75I86E3 MINGW64 ~

$ git config -l

diff.astextplain.textconv=astextplain

filter.lfs.clean=git-lfs clean -- %f

filter.lfs.smudge=git-lfs smudge -- %f

filter.lfs.process=git-lfs filter-process

filter.lfs.required=true

http.sslbackend=openssl

http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt

core.autocrlf=true

core.fscache=true

core.symlinks=false

pull.rebase=false

credential.helper=manager-core

credential.https://dev.azure.com.usehttppath=true

init.defaultbranch=master

user.name=kishoraswar

user.email=kishoraswar22@gmail.com


User@DESKTOP-75I86E3 MINGW64 ~/GitDemo

 $ git init

Initialized empty Git repository in C:/Users/User/GitDemo/.git/

 $ git status

On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

$ touch helloworld.java

$ git add helloworld.java

$ git commit -m "First Commit"

[master (root-commit) 367a888] First Commit

 1 file changed, 0 insertions(+), 0 deletions(-)

 create mode 100644 helloworld.java


$ git status

On branch master

nothing to commit, working tree clean


User@DESKTOP-75I86E3 MINGW64 ~/GitDemo (master)

$ git remote add origin  https://github.com/kishoraswar/Remote_Repo.git


User@DESKTOP-75I86E3 MINGW64 ~/GitDemo (master)

$ git push -u origin master

Enumerating objects: 3, done.

Counting objects: 100% (3/3), done.

Writing objects: 100% (3/3), 217 bytes | 217.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

To https://github.com/kishoraswar/Remote_Repo.git

 * [new branch]      master -> master

branch 'master' set up to track 'origin/master'.



HEAD: the current commit your repo is on. Most of the time HEAD points to the latest commit in your current branch, but that doesn't have to be the case. HEAD really just means "what is my repo currently pointing at".


In the event that the commit HEAD refers to is not the tip of any branch, this is called a "detached head".


master: the name of the default branch that git creates for you when first creating a repo. In most cases, "master" means "the main branch". Most shops have everyone pushing to master, and master is considered the definitive view of the repo. But it's also common for release branches to be made off of master for releasing. Your local repo has its own master branch, that almost always follows the master of a remote repo.


origin: the default name that git gives to your main remote repo. Your box has its own repo, and you most likely push out to some remote repo that you and all your coworkers push to. That remote repo is almost always called origin, but it doesn't have to be.


Git Origin Master

The term "git origin master" is used in the context of a remote repository. It is used to deal with the remote repository. The term origin comes from where repository original situated and master stands for the main branch. Let's understand both of these terms in detail.


Git Origin

In Git, The term origin is referred to the remote repository where you want to publish your commits. The default remote repository is called origin, although you can work with several remotes having a different name at the same time. It is said as an alias of the system.

The origin is a short name for the remote repository that a project was initially being cloned. It is used in place of the original repository URL. Thus, it makes referencing much easier.


1. git config

Git config command is super helpful. Especially when you are using Git for the first time, or you have a new Git installation. This command will set up your identity - Name and Email address. And this information will be used with every commit.

Usage

$ git config --global user.name "Your name"  

$ git config --global user.email "Your email"


2. git version

As its name implies, it's just to check which version of Git you are using. At the moment, writing this guide, the latest version of Git for Windows is 2.31.1. It was released on 27th March 2021.

Usage  

$ git version


3. git init

This is probably the first command you use to start a new project in Git. This command will create a blank new repository, and then you can store your source code inside this repo.

Usage  

$ git init 

Or you can use the repository name with your git init command.

$ git init <your repository name>


4. git clone

The git clone command will use an existing repository to copy. There is one main difference between the git init and git clone. 

You will use the Git clone when you need to make a copy on an existing repository. The git clone command internally uses the git init command first and then checks out all its contents.

Usage 

git clone <your project URL>


5. git add

The Git add command will add all the new code files or modified files into your repository. This command offers different options to add files and folders. 

Here is the usage of the Git add command.

Usage

$ git add your_file_name (it will add a single file to your staging area)

$ git add * ( this option will add all the modified and new files to the staging area)


6. git commit

This Git command is essential. Your project quality may drop if you will not use this command appropriately. There is another article about how to use Git commands property, and you can read that here.

In simple words, the Git commit will add your changes to your local repository.

Usage 

$ git commit -m “your useful commit message”


7. git status 

This Git command is convenient to see how many files are there which need your attention. You can run this command at any time. 

You can use it in between Git add, and Git commits to see the status.

Usage 

$ git status 


8. git branch

Most of the time, you have multiple branches in your Git repository. In simple terms, the branch is an independent line of code development. 

With the Git branch command, you can manage your branches effectively. There are many different options and switches of the Git branch. 

To make it simple, here I will highlight how you can create and delete a Git branch (in case you need more depth, you can refer to - Git Branching and Merging section of this article).

Usage 

(i) To list all branches:

$ git branch 


(ii) To create a new branch:

$ git branch <branch_name>


(iii) To delete a branch:

$ git branch -d <branch_name>


9. git checkout

This Git command is used to switch between branches. This is one of the powerful git commands and can use used as a swiss knife, 

In simple words, here is the syntax to switch to another branch.

Usage

$ git checkout <branch_name>

Also, you can create and checkout to a branch in a single like, here is the usage for that 

$ git checkout -b <your_new_branch_name>


10. git remote

Git remote command acts like a border, and If you need to connect with the outside world, you have to use the Git remote command. This command will connect your local repository to the remote. 

Usage 

$ git remote add <shortname> <url>


Example

$ git remote add origin https://dev.azure.com/aCompiler/_git/DemoProject


11. git push

Once you are connected with the remote repository (with the help of the git remote command), it's time to push your changes to that repository.

Usage 

$ git push -u <short_name> <your_branch_name>

Example 

$ git push -u origin feature_branch


You should have origin and upstream set up before you use Git push. And here is the command to set up upstream.

Usage 

$ git push --cdm <short_name> <branch_name>

Example 

$ git push --set-upstream origin feature_branch


13. git fetch 

When you need to download other team members' changes, you have to use git fetch. 

This command will download all information for commits, refs, etc., so you can review it before applying those changes in your local repository.

Usage 

$ git fetch 


14. git pull

The Git pull command downloads the content (and not the metadata) and immediately updates your local repository with the latest content. 

Usage 

$ git pull <remote_url>


fetch v/s pull



15. git stash

This Git command temporarily stores your modified files. You can work in stashed with the following Git command. 

Usage 

$ git stash 

And you can view all of your stashes with the following command 

$ git stash list 

And if you need a apply a stash to a branch, simply use apply 

$ git stash apply 


  • git stash – Create a stash with local modifications and revert back to the head commit.
  • git stash list – Display a list of all stashes in your repository.
  • git stash show – View the content of your most recent stash. This will show your stashed changes as a diff between the stashed content and the commit from back when the stash was created.
  • git stash drop <stash> – Remove a stash from the list of stashes in your repository.
  • git stash pop <stash> – Apply a stash to the top of the current working tree and remove it from your list of stashes.
  • git stash apply <stash> – Apply a stash on top of the current working tree. The stash will not be removed from your list of stashes.
  • git stash clear – Remove all stashes from your repository.

16. git log

With the help of the Git log, you can see all the previous commits with the most recent commit appear first.

Usage 

$ git log 

By default, it will show you all the commits of the currently checked out branch, but you can force it to see all the commits of all the branches with all options. 

$ git log --all


17. git shortlog

The shortlog command shows you a summary from the Git log command. This command is helpful if you are just interested in the short summary. 

This command is helpful to see who worked on what as it group author with their commits.

Usage 

$ git shortlog  

Git Shortlog Results


18. git show

Compared to the Git log, this command git show will show you details about a specific commit.

Usage 

$ git show <your_commit_hash>


19. git rm

Sometimes you need to delete files from your codebase, and in that case, you can use the Git rm command.

It can delete tracked files from the index and the working directory.

Usage 

$ git rm <your_file_name>


20. git merge

Git merge helps you to integrate changes from two branches into a single branch. 

Usage 

$ git merge <branch_name>

This command will merge the <branch_name> into your current selected branch.


A list of my commonly used Git commands

--

Getting & Creating Projects

CommandDescription
git initInitialize a local Git repository
git clone ssh://git@github.com/[username]/[repository-name].gitCreate a local copy of a remote repository

Basic Snapshotting

CommandDescription
git statusCheck status
git add [file-name.txt]Add a file to the staging area
git add -AAdd all new and changed files to the staging area
git commit -m "[commit message]"Commit changes
git rm -r [file-name.txt]Remove a file (or folder)

Branching & Merging

CommandDescription
git branchList branches (the asterisk denotes the current branch)
git branch -aList all branches (local and remote)
git branch [branch name]Create a new branch
git branch -d [branch name]Delete a branch
git push origin --delete [branch name]Delete a remote branch
git checkout -b [branch name]Create a new branch and switch to it
git checkout -b [branch name] origin/[branch name]Clone a remote branch and switch to it
git branch -m [old branch name] [new branch name]Rename a local branch
git checkout [branch name]Switch to a branch
git checkout -Switch to the branch last checked out
git checkout -- [file-name.txt]Discard changes to a file
git merge [branch name]Merge a branch into the active branch
git merge [source branch] [target branch]Merge a branch into a target branch
git stashStash changes in a dirty working directory
git stash clearRemove all stashed entries

Sharing & Updating Projects

CommandDescription
git push origin [branch name]Push a branch to your remote repository
git push -u origin [branch name]Push changes to remote repository (and remember the branch)
git pushPush changes to remote repository (remembered branch)
git push origin --delete [branch name]Delete a remote branch
git pullUpdate local repository to the newest commit
git pull origin [branch name]Pull changes from remote repository
git remote add origin ssh://git@github.com/[username]/[repository-name].gitAdd a remote repository
git remote set-url origin ssh://git@github.com/[username]/[repository-name].gitSet a repository's origin branch to SSH

Inspection & Comparison

CommandDescription
git logView changes
git log --summaryView changes (detailed)
git log --onelineView changes (briefly)
git diff [source branch] [target branch]Preview changes before merging

Stash Commands

CommandDescription
git stashgit stash is used to save the changes in present working directory
git stash applythis command is used to apply the changes which you have saved before with the command git stash
git stash listthis command list all the stashed changes
git stash dropthis command delete the recent stashed chnages.
git remote -vList all currently configured remote repositories
git push --all originPush all branches to your remote repository
git push origin :<branchname>Delete a branch on your remote repository
git tag 1.0.0 <commitID>You can use tagging to mark a significant changeset, such as a release
git push --tags originPush all tags to remote repository
git fetch originfetch the remote changes to local
git reset --hard <sha-value>this command is used to switch from one commit -id to another [This command discards all history and goes back to the specified commit.]
git reset --mixedthis command is used to switch from one commit to another but ....local changes will remain.
git reset --softthis command is also used to switch but stage area changes will remain.
git reset <filename>this command is used to unstage the file
git reset <commit-id>This command undoes all the commits after the specified commit and preserves the changes locally [unstage all the chnages whatever files present in this commit-id]
git log –follow [filename]This command lists version history for a file, including the renaming of files also
git show [commit id]This command shows the metadata and content changes of the specified commit.
git tag [commitID]This command is used to give tags to the specified commit.
git revert <commitid>this command is used to remove/delete the commitid and it will create a new commited for history
git rebase -i <commitid>this command is used to delete the commit id but it will not create a history
git cherry-pick <commit id>




this command is used to copy a sha-value from one branch to other



Git Reset Modes

Git Reset has three modes in which it can work. Each mode will revert changes in one of the three stages of the workflow(Working Directory, Staging Area, and Repository) and will have the functionalities of the previous option. Let's take a look at these three options.

Git Reset --soft

The --soft option is used to reset changes only in the Repository. It works by changing the position of our HEAD to point to the previous commits. However, it keeps the staging area and the working directory as it is.

Git Reset --mixed

The --mixed option has the functionality of the --soft with the additional feature of resetting changes in the staging area. In this mode, Git will first revert changes in the repository by making the HEAD point to one of the previous commits and then also update the Staging Area to the state it was in when that previous commit was used. This is also the default mode that Git Reset uses.

Git Reset --hard

The --hard option is the most powerful one among the three in the sense that it has the functionalities of the above options along with an additional feature. The --hard option will also reset the changes in the working directory after resetting the Repository and the Staging Area. But one must be careful while using this option as all our unsaved changes in the working directory will be lost.






GIT Guide Tutorial - Studytonight


====================================================================

Recover Deleted Branch:


User@DESKTOP-75I86E3 MINGW64 ~/kishor/git_recover (feature1)

$ git log --oneline

948d4ab (HEAD -> feature1) added both file

73df6c3 (master) second commit

d206382 first commit


User@DESKTOP-75I86E3 MINGW64 ~/kishor/git_recover (feature1)

$ git checkout master

Switched to branch 'master'


User@DESKTOP-75I86E3 MINGW64 ~/kishor/git_recover (master)

$ git log --oneline

73df6c3 (HEAD -> master) second commit

d206382 first commit


User@DESKTOP-75I86E3 MINGW64 ~/kishor/git_recover (master)

$ git branch -D feature1

Deleted branch feature1 (was 948d4ab).


User@DESKTOP-75I86E3 MINGW64 ~/kishor/git_recover (master)

$ git checkout -b feature1 948d4ab

Switched to a new branch 'feature1'


User@DESKTOP-75I86E3 MINGW64 ~/kishor/git_recover (feature1)

$ git log --oneline

948d4ab (HEAD -> feature1) added both file

73df6c3 (master) second commit

d206382 first commit

========================================================================

User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (master)

$ git branch -d feature1

error: The branch 'feature1' is not fully merged.

If you are sure you want to delete it, run 'git branch -D feature1'.


User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (master)

$ git branch -D feature1

Deleted branch feature1 (was c83f3e2).


User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (master)

$ git checkout -b feature_new c83f3e2bd2bd8e3abf7bf7be6c30aa5588611082

Switched to a new branch 'feature_new'


User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (feature_new)

$ git log --oneline

c83f3e2 (HEAD -> feature_new) added c.java

b11c6ee (master) update both file

fd554e1 mycommit1



User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref/.git/logs (GIT_DIR!)
$ tail -f HEAD
0000000000000000000000000000000000000000 fd554e1c96fd2f3430d47d7906e52de4ecb4d203 kishoraswar <kishoraswar22@gmail.com> 1658077289 +0530       commit (initial): mycommit1
fd554e1c96fd2f3430d47d7906e52de4ecb4d203 fd554e1c96fd2f3430d47d7906e52de4ecb4d203 kishoraswar <kishoraswar22@gmail.com> 1658077306 +0530       checkout: moving from master to feature1
fd554e1c96fd2f3430d47d7906e52de4ecb4d203 2707b57ad419b0e04fc0ed50765d0ea840a5d356 kishoraswar <kishoraswar22@gmail.com> 1658077327 +0530       commit: second commit
2707b57ad419b0e04fc0ed50765d0ea840a5d356 fd554e1c96fd2f3430d47d7906e52de4ecb4d203 kishoraswar <kishoraswar22@gmail.com> 1658077348 +0530       checkout: moving from feature1 to master
fd554e1c96fd2f3430d47d7906e52de4ecb4d203 b11c6ee5593e4c9d7e9e393012e8fdd1a3a53b37 kishoraswar <kishoraswar22@gmail.com> 1658077795 +0530       commit: update both file
b11c6ee5593e4c9d7e9e393012e8fdd1a3a53b37 b11c6ee5593e4c9d7e9e393012e8fdd1a3a53b37 kishoraswar <kishoraswar22@gmail.com> 1658077825 +0530       checkout: moving from master to feature1
b11c6ee5593e4c9d7e9e393012e8fdd1a3a53b37 c83f3e2bd2bd8e3abf7bf7be6c30aa5588611082 kishoraswar <kishoraswar22@gmail.com> 1658077848 +0530       commit: added c.java
c83f3e2bd2bd8e3abf7bf7be6c30aa5588611082 b11c6ee5593e4c9d7e9e393012e8fdd1a3a53b37 kishoraswar <kishoraswar22@gmail.com> 1658077862 +0530       checkout: moving from feature1 to master
b11c6ee5593e4c9d7e9e393012e8fdd1a3a53b37 c83f3e2bd2bd8e3abf7bf7be6c30aa5588611082 kishoraswar <kishoraswar22@gmail.com> 1658077924 +0530       checkout: moving from master to feature_new

=========================

Git Cherry Pick:

User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (feature_new)

$ git log --oneline

c83f3e2 (HEAD -> feature_new) added c.java

b11c6ee (master) update both file

fd554e1 mycommit1


User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (feature_new)

$ git checkout master

Switched to branch 'master'


User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (master)

$ git cherry-pick c83f3e2

[master fb0d281] added c.java

 Date: Sun Jul 17 22:40:48 2022 +0530

 1 file changed, 0 insertions(+), 0 deletions(-)

 create mode 100644 c.java


User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (master)

$ git log --oneline

fb0d281 (HEAD -> master) added c.java

b11c6ee update both file

fd554e1 mycommit1

===========================

git Sqesh:
it squash is a technique that helps you to take a series of commits and condense it to a few commits. 

User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (master)
$ git log --oneline
290e83b (HEAD -> master) added d.java
fb0d281 added c.java
b11c6ee update both file
fd554e1 mycommit1

User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (master)
$ git rebase -i HEAD~3
[detached HEAD f67cf02] update both file
 Date: Sun Jul 17 22:39:55 2022 +0530
 4 files changed, 2 insertions(+)
 create mode 100644 c.java
 create mode 100644 d.java
Successfully rebased and updated refs/heads/master.

User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (master)
$ git log --oneline
f67cf02 (HEAD -> master) update both file
fd554e1 mycommit1

User@DESKTOP-75I86E3 MINGW64 ~/kishor/gitref (master)
$ ls -l
total 2
-rw-r--r-- 1 User 197121 0 Jul 17 22:54 c.java
-rw-r--r-- 1 User 197121 0 Jul 17 22:54 d.java
-rw-r--r-- 1 User 197121 6 Jul 17 22:39 f1.java
-rw-r--r-- 1 User 197121 6 Jul 17 22:39 f2.java




pick b11c6ee update both file
s fb0d281 added c.java
s 290e83b added d.java

# Rebase fd554e1..290e83b onto fd554e1 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified); use -c <commit> to reword the commit message
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
.git/rebase-merge/git-rebase-todo[+] [unix] (22:51 17/07/2022)    


=================
.gitignore

When sharing your code with others, there are often files or parts of your project, you do not want to share.

Examples

  • log files
  • temporary files
  • hidden files
  • personal files
  • etc.

Git can specify which files or parts of your project should be ignored by Git using a .gitignore file.

Git will not track files and folders specified in .gitignore. However, the .gitignore file itself IS tracked by Git.

To create a .gitignore file, go to the root of your local Git, and create it:

Now open the file using a text editor.

We are just going to add two simple rules:

  • Ignore any files with the .log extension
  • Ignore everything in any directory named temp

touch .gitignore

# ignore ALL .log files
*.log

# ignore ALL files in ANY directory named temp
temp/

ow all .log files and anything in temp folders will be ignored by Git.

Branching Strategy:


Git Interview Preparation:

Sunday, June 26, 2022

Shell Scripting Training

 What is shell Script?

A shell script is a text file that contains a sequence of commands for a UNIX-based operating system.

ex: server_details.sh

[root@ip-172-31-42-179 ShellScript]# cat server_details.sh

#!/bin/bash

hostname

uname -a

uptime

cat /etc/os-release

Output:

[root@ip-172-31-42-179 ShellScript]# ./server_details.sh

ip-172-31-42-179.us-east-2.compute.internal

Linux ip-172-31-42-179.us-east-2.compute.internal 5.10.118-111.515.amzn2.x86_64 #1 SMP Wed May 25 22:12:19 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

 15:56:41 up 12:57,  1 user,  load average: 0.00, 0.00, 0.00

NAME="Amazon Linux"

VERSION="2"

ID="amzn"

ID_LIKE="centos rhel fedora"

VERSION_ID="2"

PRETTY_NAME="Amazon Linux 2"

ANSI_COLOR="0;33"

CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"

HOME_URL="https://amazonlinux.com/"

-----------------------------------------------------------------------------------------------------------------------------

Write Your First Shell Script.

#touch HelloWorld.sh

#vi HelloWorld.sh

#!/bin/bash

echo "Hello World"

Execute Shell Script

#bash shellscript_name.sh

#./shellscript_name.sh

What is shebang?

The shebang is the combination of the # (pound key) and ! (exclamation mark).

This character combination has a special meaning when it is used in the very first line of the script. 

It is used to specify the interpreter with which the given script will be run by default.

So, if the first line of a script is:

#!/bin/bash

echo command: To print output

echo "Hello World"

comments in shell script

#single line comment
: '
this is multi
line comment'

what is comment?
Instruction which present in file but its not performing any task.. its gives us information about code.

Loops:

1)While Loop:
[root@ip-172-31-42-179 ShellScript]# cat while_example.sh
#!/bin/bash
i=1
while [ $i -lt 11 ]
do
        echo $i
        i=`expr $i + 1`
done

[root@ip-172-31-42-179 ShellScript]# ./while_example.sh
1
2
3
4
5
6
7
8
9
10

2) For Loop:

[root@ip-172-31-42-179 ShellScript]# cat for_loop.sh
#!/bin/bash
for ((  number=1; number<10; number++ ))
do
        echo $number
done

OUTPUT:

[root@ip-172-31-42-179 ShellScript]# ./for_loop.sh
1
2
3
4
5
6
7
8
9

For Vs While Loop?

For loop

  • The initialization, condition checking, and the iteration statements are written at the beginning of the loop.

  • It is used only when the number of iterations is known beforehand.

  • If the condition is not mentioned in the 'for' loop, then the loop iterates infinite number of times.

  • The initialization is done only once, and it is never repeated.

  • The iteration statement is written at the beginning.

  • Hence, it executes once all statements in loop have been executed

While condition

  • The initialization and the condition checking is done at the beginning of the loop.

  • It is used only when the number of iterations isn’t known.

  • If the condition is not mentioned in the 'while' loop, it results in a compilation error.

  • If the initialization is done when the condition is being checked, then initialization occurs every time the loop is iterated through.

  • The iteration statement can be written within any point inside the loop.


User Input:
In Shell script if want to take any input from user then we are using read command

[root@ip-172-31-42-179 ShellScript]# cat user_input.sh
#!/bin/bash
echo "Enter Username"
read user
echo "Welcome $user to Bash Shell Scripting Program"

[root@ip-172-31-42-179 ShellScript]# ./user_input.sh
Enter Username
kishor
Welcome kishor to Bash Shell Scripting Program
[root@ip-172-31-42-179 ShellScript]#


If statement:
You can use if condition with single or multiple conditions. Starting and ending block of this statement is define by ‘if’ and ‘fi’. 

[root@ip-172-31-42-179 ShellScript]# cat if_statement.sh
#!/bin/bash
i=30
if [ $i -gt 20 ];
then
        echo "$i is greator than 20"
fi

[root@ip-172-31-42-179 ShellScript]# ./if_statement.sh
30 is greator than 20

if else statement:
#!/bin/bash
n=2
if [ $n -eq 1 ]
then
        echo "This is true"
else
        echo "N is no longer 1"
fi


If statement with AND , OR logic

if with AND

#!/bin/bash

echo "Enter username"
read username
echo "Enter password"
read password

if [[ ( $username == "admin" && $password == "secret" ) ]]; then
echo "valid user"
else
echo "invalid user"
fi

if with OR

#!/bin/bash

echo "Enter any number"
read n

if [[ ( $n -eq 15 || $n  -eq 45 ) ]]
then
echo "You won the game"
else
echo "You lost the game"
fi


else if statement:
#!/bin/bash

echo "Enter your lucky number"
read n

if [ $n -eq 101 ];
then
echo "You got 1st prize"
elif [ $n -eq 510 ];
then
echo "You got 2nd prize"
elif [ $n -eq 999 ];
then
echo "You got 3rd prize"

else
echo "Sorry, try for the next time"
fi



Case Statement
Case statement is used as the alternative of if-elseif-else statement. The starting and ending block of this statement is defined by ‘case’ and ‘esac’.

syntax:
case value in
pattern1)
        statement1
        ...
        statementN
        ;;
pattern2)
        statement1
        ...
        statementN
        ;;
*)
        statement1
        ...
	statementN
	;;
esac
Ex:

#!/bin/bash

echo "Enter your lucky number"
read n
case $n in
101)
echo echo "You got 1st prize" ;;
510)
echo "You got 2nd prize" ;;
999)
echo "You got 3rd prize" ;;
*)
echo "Sorry, try for the next time" ;;
esac

Get Arguments from Command Line 
Bash script can read input from command line argument like other programming language. For example, $1 and $2 variable are used to read first and second command line arguments.

#!/bin/bash
echo "Total arguments : $#"
echo "1st Argument = $1"
echo "2nd argument = $2"

Combine two strings in a variable:
#!/bin/bash

string1="Linux"
string2="Hint"
echo "$string1$string2"
string3=$string1+$string2
string3+=" is a good tutorial blog site"
echo $string3

Get Substring of Strings

Like other programming language, bash has no built-in function to cut value from any string data. But you can do the task of substring in another way in bash that is shown in the following script.

#!/bin/bash
Str="Learn Linux from LinuxHint"
subStr=${Str:6:5}
echo $subStr


Add 2 numbers into a variable
#!/bin/bash
echo "Enter first number"
read x
echo "Enter second number"
read y
(( sum=x+y ))
echo "The result of addition=$sum"

Create a Function

#!/bin/bash
function myfunction()
{
echo 'I like bash programming'
}

myfunction


Use Function Parameters
#!/bin/bash

Rectangle_Area() {
area=$(($1 * $2))
echo "Area is : $area"
}

Rectangle_Area 10 20


Make directory
#!/bin/bash
echo "Enter directory name"
read newdir
`mkdir $newdir`


Make directory by checking existence
#!/bin/bash
echo "Enter directory name"
read ndir
if [ -d "$ndir" ]
then
echo "Directory exist"
else
`mkdir $ndir`
echo "Directory created"
fi

Pass Return Value from Script
#!/bin/bash
function return_val() {

str="Hello, $name"
echo $str

}

echo "Enter your name"
read name

val=$(return_val)
echo "Return value of the function is $val"

Read a file:
#!/bin/bash
file='book.txt'
while read line; do
echo $line
done < $file

#!/bin/bash
read -p "Enter file name : " filename
while read -n1 character
do 
echo $character
done < $filename


Delete a File:
#!/bin/bash
echo "Enter filename to remove"
read file_name
rm -i $file_name


Append to file:
#!/bin/bash

echo "Before appending the file"
cat file_name.txt

echo "Learning Laravel 5">> file_name.txt
echo "After appending the file"
cat file_name.txt


Test if File Exists

#!/bin/bash
filename=$1
if [ -f "$filename" ]; then
echo "File exists"
else
echo "File does not exist"
fi

Send Email Example
#!/bin/bash
Recipient=”kishoraswar@gmail.com”
Subject=”MailDemo”
Message=”Welcome to our site”
`mail -s $Subject $Recipient <<< $Message

Get Parse Current Date:
#!/bin/bash
Year=`date +%Y`
Month=`date +%m`
Day=`date +%d`
Hour=`date +%H`
Minute=`date +%M`
Second=`date +%S`
echo `date`
echo "Current Date is: $Day-$Month-$Year"
echo "Current Time is: $Hour:$Minute:$Second"


Wait Command:
wait  is a built-in command of Linux that waits for completing any running process. wait command is used with a particular process id or job id. If no process id or job id is given with wait command then it will wait for all current child processes to complete and returns exit status.

#!/bin/bash
echo "Wait command" &
process_id=$!
wait $process_id
echo "Exited with status $?"


Sleep Command
When you want to pause the execution of any command for specific period of time then you can use sleep command. You can set the delay amount by seconds (s), minutes (m), hours (h) and days (d). 


#!/bin/bash
echo “Wait for 5 seconds”
sleep 5
echo “Completed”


grep:

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out). 

Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
 with each such part on a separate output line.

-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.

 root@ip-172-31-42-179 ~]# grep -C2 "localhost" /var/log/messages
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: Running in initial RAM disk.
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: No hostname configured.
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: Set hostname to <localhost>.
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: Initializing machine ID from random generator.
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: Reached target Local File Systems.
[root@ip-172-31-42-179 ~]# grep -A2 "localhost" /var/log/messages
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: Set hostname to <localhost>.
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: Initializing machine ID from random generator.
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: Reached target Local File Systems.
[root@ip-172-31-42-179 ~]# grep -B2 "localhost" /var/log/messages
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: Running in initial RAM disk.
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: No hostname configured.
Jun 26 02:58:49 ip-172-31-42-179 systemd[1]: Set hostname to <localhost>.
[root@ip-172-31-42-179 ~]#



SED VS AWK
Use sed to automate tasks you would do otherwise in a text editor manually. That's why it is called stream editor. (You can use the same commands to edit text in vim). Use awk if you want to analyze text, meaning counting fields, calculate totals, extract and reorganize structures etc.




  • Sed - when you need to do simple text transforms on files.
  • Awk - when you only need simple formatting and summarisation or transformation of data

awk :Aho Weinberger Kernighan


AWK is an interpreted programming language. It is very powerful and specially designed for text processing. Its name is derived from the family names of its authors − Alfred Aho, Peter Weinberger, and Brian Kernighan.

Uses:

  • Text processing,
  • Producing formatted text reports,
  • Performing arithmetic operations,
  • Performing string operations, and many more.
Syntax:
awk option pattern action source_file


#ls -l |awk '{print $1}'
#ls -l|awk '{print $1,$4,$6}'
#ls -l|awk '{awk $0}'

List Directories
#ls -l|awk /^d/ {print $0}

List only Files:
#ls -l|awk /^-/ {print $0}

#ls -l /*  nl --> We can see multiple files and Directories

Link Files:
#ls -l /* |awk '/^l/ {print $0}'

Match with specific Field...

ls -l /* |awk '$2 ~ /^l/ {print $0}

ls -l /* |awk '$2 ~ /^2/ {print $0} '

ls -l  /* |awk '$2 ~/60/ {print $0}'  -- In 2nd number field it will check for 60
ls -l  /* |awk '$2 ~/1/ {print $0}' 
ls -l  /* |awk '$2 ~/^1$/ {print $0}' --> Start and end with 1

ls -l /* |awk '$4 ~ /root/ {print $0}'

ls -l /* |awk '$6 ~ /Nov/ {print $0}'

ls -l /* |awk '$9 ~ /mail/ {print $0}'

ls -l /*|awk '$9 ~ /^mail/ {print $0}'


Print Files which created in year 2021
ls -l /* |awk 

List files which size is 1kb

ls -l /*|awk '$5 > 1024 {print $0}'


ls -l /* |awk '$5 == 1024 {print $0}'

ls -l /*|awk '$5 == 6 && $2 == 2 {print  $0}'


ls -l /* |awk '$5 == 6 || $2 == 2{print $0}'

ls -l /* |awk '{print NR,$0}'  --> Record Number
ls -l /*|awk '{print NR,$0,NF}'  -->Number of record and Number of Field

ls -l /*|awk '{print NF,$0,NR}'  --> It will display NF first and Numbe rof record last

ls -l /* |awk 'NR == 750, NR == 800 {print NR, $0, NF}'

ls -l /* |awk '$1 ~ /rwxr-xr-x/ {print $0}'  -->Display Files with Perticular Permission

ls -l /*|awk 'BEGIN {count=0;} {if ($6 == "Nov") count+=1} END {print count}'  ==> Diplay total number of Nov word

Set Header and Footer:
ls -l /* | awk 'BEGIN {print "Welcome To Shell Scripting"} NR == 700 , NR ==750 {print NR, $0, NF} END {Good Bye Shell Script}'

ls -l /* | awk 'BEGIN {print "**************"} NR == 700 , NR ==750 {print NR, $0, NF} END {print "Good Bye Shell Script"}'


#df -h

Create File With Below Data:

vi awkdemo
ename depart  desgination Salary
kishor  IT          DevOps      10000
satish  IT          Developer    9000
ganesh admin  avp             11000
john     hr         manager      6000

#awk '{print $1}' awkdemo

#awk '{print $1,$2,$4}' awkdemo

#awk '{print $1,$2,$4}' OFS='   ' awkdemo

#awk  '{print $1,$2,$4}' OFS=' \t ' awkdemo

#awk  '{print $1,$2,$4}' OFS='-'  awkdemo

#awk  '{print $1,$2,$4}' OFS='-'  awkdemo|column  -t

#awk '/kishor/ {print $0}' awkdemo

#awk '$2 ~ /^kishor/ {print $0}' awkdemo

#awk '$1 ~ /^ganesh$/ {print $0}' awkdemo

#awk '$4 == 9000' awkdemo

#awk '$4 > 6000' awkdemo

#awk '$4 < 5000' awkdemo

#awk '{print NR,$0}' awkdemo
#awk '{print NR,$0,NF}' awkdemo

===========
awk '{print $1}' /etc/passwd

Use delimeter in awk

awk -F':'  '{print $1,$3}' /etc/passwd

awk -F':'  '{print $1,$3}' /etc/passwd|column

awk -F':'  '{print $1,$3}' OFS='-' /etc/passwd


work on logs files:

#awk "{print $1}' /var/log/messages

#awk "{print $1,$2,$3,$4}' /var/log/messages

Find localhost word how many times repeated in /var/log/message file...

#awk 'BEGIN {count=0;} {if ($4 == "localhost") count +=1} END {print count}' /var/log/messages







Shell Scripting Question:

Write a command delete all line except first line?

Write a command to print zero byte size files?

Write a command remove all empty lines?

Write a command to print the line number before each line?

How add a first record and last record to the current file in linux?

How to get only zero byte files which are present in the directory?

How to display even number of records into one file and odd number of records into another file?

How to print only blank line of file?

Write a command to print the second and third line of a file without using nr?

Write a command to find the total number of lines in a file without using nr?

Write a command to print all line except first line?

Write a command to rename the files in a directory with "_new" as postfix?


In the text file, some lines are delimited by colon and some are delimited by space. Write a command to print the third field of each line.?




1) Display the 10th line of the file?

[root@ip-172-31-42-179 log]# tail -n 10 /var/log/messages |tail -n 1
Jul  2 18:05:42 ip-172-31-42-179 dhclient[2884]: XMT: Solicit on eth0, interval 124840ms.


2)How to remove header from file?






























Reference Link:

ex: ls -l |cut -d' '  -f1 --Work perfect where actual row and column







Sample Game App Deployment on EKS cluster

 https://padmakshi.medium.com/setting-up-an-eks-cluster-and-deploying-a-game-application-a-step-by-step-guide-08790e0be117