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:
Symbol | Meaning | Example | Description |
# | Comment | # comment | Line ignored by shell |
! | Negation/history | #!/bin/bash | Starts script with interpreter |
$ | Variable access | echo $USER | Prints current user |
\ | Escape character | echo \$HOME | Prints literal $HOME |
' ' | Single quotes (no expand) | '${USER}' | Prints literal ${USER} |
" " | Double quotes (expand) | "${USER}" | Expands to current username |
: | Null operation | : > file.txt | Clears file without deleting it |
~ | Home directory | cd ~ | Changes to user’s home directory |
. | Current directory | ./ script.sh | Executes from current directory |
.. | Parent directory | cd .. | Moves up one level |
/ | Root/path separator | cd /var/log | Navigates to full path |
- | Previous directory | cd - | Switches to last directory |
> | Redirect stdout to file | echo "Hello" > hello.txt | Overwrites hello.txt with "Hello" |
>> | Append stdout to file | echo "Again" >> hello.txt | Appends "Again" to hello.txt |
&& | AND (if previous passes) | make && echo "Success" | Executes echo if make succeeds |
; | Run both regardless | echo Hi; echo Bye | Executes 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’
Subscribe to my newsletter
Read articles from Sandhya Kalikiri directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
