DevOps-Day'15&'16- Shell Scripting

what is shell?

shell refers to the interface, that will allows users to interact with the OS.

It acts as a interface between user and OS

shell types:

  • Bourne shell - sh

  • bash

  • korn shell - ksh

  • c shell - csh

  • z shell - zsh

→To find types of shell present in the system

shell details are in /etc/shell directory

echo - is an linux command to print the output

echo “what we want to print”

→To know the default shell, that our linux is using

echo $SHELL

echo $0

Shell Scripting:

It is a process of writing a group of command in a file to automate the task.

These scripts typically uses a bash shell, to execute

shell script is a file with .sh extension

The file contains linux commands and control logics(loops, conditions, functions)

Shell Scripting purpose:

→To automate repetitive task

→Batch processing

→Devops CI/CD

→User/group management

→Software installation

→Logs parsing and monitoring

→Health checks

#! - shebang line

tell the OS, to which interpreter to use

#!/bin/bash

To execute the shell script the following commands are used

sh demo.sh

bash demo.sh

./demo.sh

. demo.sh

→ sh -x demo.sh

It displays the command and its output (debug Mode)

→ To execute only the particular lines of code in the system we use

set -x

……lines of code

set +x

whatever in between this can be executed in a debug mode

Some Linux commands that used in the shell scripting:

SymbolMeaningExampleDescription
#Comment# commentLine ignored by shell
!Negation/history#!/bin/bashStarts script with interpreter
$Variable accessecho $USERPrints current user
\Escape characterecho \$HOMEPrints literal $HOME
' 'Single quotes (no expand)'${USER}'Prints literal ${USER}
" "Double quotes (expand)"${USER}"Expands to current username
:Null operation: > file.txtClears file without deleting it
~Home directorycd ~Changes to user’s home directory
.Current directory./script.shExecutes from current directory
..Parent directorycd ..Moves up one level
/Root/path separatorcd /var/logNavigates to full path
-Previous directorycd -Switches to last directory
>Redirect stdout to fileecho "Hello" > hello.txtOverwrites hello.txt with "Hello"
>>Append stdout to fileecho "Again" >> hello.txtAppends "Again" to hello.txt
&&AND (if previous passes)make && echo "Success"Executes echo if make succeeds
;Run both regardlessecho Hi; echo ByeExecutes both commands

To execute a basic sh command:

→ In shell scripting one command fails to execute, the rest of the commands will execute successfully.

set -e :

when a command fails to execute, then comes of the script we use set -e, it fails the remaining all commands below the failed command.

If we set the set -e in the script, it exits immediately when a command failed to execute.

→when we are piping(one command output is input of the another command) this set -e will not work.

variables :

→ used to store data/values, that can be refered and maintained throughout the script

why we need variables :

→ stores value

→ make script re-usable

→ allow script to give different output

→ improve readability

syntax:

varname=value

eg: name=sandhya

age=25

→ For refering the value, we use

$followed by variable name

eg: name=sandhya

echo “ my name is:$name”

naming conventions:

→ Variables names can consists of letters(a-z, A-Z), digits(0-9), underscore(_)

→Variable name should start with either letter or underscore, but no digit.

eg: san, san45, san_d

but no 33, 33h

→ Variable names are case sensitive

→ cant use reserved keywords

reserved keywords are if, for loop, while

eg:

#!bin/bash

#basic script

name=sweety

age=25

echo “ my name is:$name”

echo “my age is:$age”

Types of variables:

→ User defined variables

→ System defined variables

  • User defined variables defined within a script are local by default and that are only within script

  • system defined variables created and maintained by OS

  • they are defined in capital letters

    These are set in /etc/profilerc directory

→ To see all the system defined variables in system, we use ‘env’ command

read -

It gives output while executing the script

#!bin/bash

echo “your name:”

read name

echo “your name is:$name”

we can use -p instead of echo

#!bin/bash

read -p “enter name” name

read -p “enter age” age

echo “ my name is:$name”

echo “ age is:$age”

→ If we define (), in script whatever data is there in that braces will be executed.

#!bin/bash

today=$(date)

today=’date’

0
Subscribe to my newsletter

Read articles from Sandhya Kalikiri directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sandhya Kalikiri
Sandhya Kalikiri