SlideShare a Scribd company logo
Bash Script
By
Prokopto Platform Team
sh is a specification, bash is the implementation.
(1) sh
Strictly speaking, sh (or the Shell Command Language) is a programming language described by
the POSIX standard. The key here is that sh isn’t really a programming language in itself. It is just a
specification. Specification is a detailed description of the syntax and semantics of that language.
Specification doesn’t include an implementation.
(2) bash
There are different operating systems with their own shells and kernel. Bash is the shell for the GNU
operating system. But we can also say that bash is a programming language.
sh vs bash
(1) Variables
(2) Conditionals
(3) Loops
(4) Regex
(5) Mathematical Skill
(6) Debugging Skill
(7) Understand Problem life cycle
Basics for better bash scripts
(1) Variables
#!/usr/bin/env bash
LINK_OR_DIR="/tmp/coordinator"
if [ -d "$LINK_OR_DIR" ]; then
# If Condition is satisfied
else:
# If Condition is not satisfies
fi
(2) Conditionals
(3) Loops
(4) Regex
(5) Mathematical Skill
(6) Debugging Skill
(7) Understand Problem life cycle
Basics for better bash scripts
(1) Variables
(2) Conditionals
#!/usr/bin/env bash
if [ -d "$LINK_OR_DIR" ]; then
# If Condition is satisfied
else:
# If Condition is not satisfies
fi
## With the same syntax you can use:
## -e: any kind of archive
## -f: file
## -h: symbolic link
## -r: readable file
## -w: writable file
## -x: executable file
## -s: file size greater than zero
## -d: check if it's a directory
## -L: check if it's a symbolic link
## -z: if the length of string is zero
## -n: length of string is non-zero
(3) Loops
(4) Regex
(5) Mathematical Skill
(6) Debugging Skill
(7) Problem life cycle
Basics for better bash scripts
(1) Variables
(2) Conditionals
(3) Loops
#!/usr/bin/env bash
## While loop
while [ <some test> ]
do
<commands>
done
## Until loop
until [ <some test> ]
do
<commands>
done
## For loop
for var in <list>
do
<commands>
done
(1) Regex
(2) Mathematical Skill
(3) Debugging Skill
(4) Understand Problem life cycle
Basics for better bash scripts
(1) Variables
(2) Conditionals
(3) Loops
(4) Regex
#!/usr/bin/env bash
t="abc123"
[[ "$t" == abc* ]] # true (globbing)
[[ "$t" == "abc*" ]] # false (literal matching)
[[ "$t" =~ [abc]+[123]+ ]] # true (regular expression)
[[ "$t" =~ "abc*" ]] # false (literal matching)
Note, that starting with bash version 3.2 the regular or globbing expression
must not be quoted. If your expression contains whitespace you can store it in a variable:
r="a b+"
[[ "a bbb" =~ $r ]] # true
(5) Mathematical Skill
(6) Debugging Skill
(7) Understand Problem life cycle
Basics for better bash scripts
(1) Variables
(2) Conditionals
(3) Loops
(4) Regex
(5) Mathematical Skill
Matrix algebra
Logical gates
(6) Debugging Skill
(7) Understand Problem life cycle
Basics for better bash scripts
(1) Variables
(2) Conditionals
(3) Loops
(4) Regex
(5) Mathematical Skill
(6) Debugging Skill
1. Reproduce
Before you scramble and try to fix the bug as soon as possible, make sure you can actually reproduce it first.
2. Investigate
Find the bug in the code and the cause!
3. Know Your Error Codes
This is a bit of strange one to hear, but given how fundamental are worth here.
4. Fix & Verify
Now verify that it is actually fixed and, more importantly, that your fix didn't break anything else.
(7) Understand Problem life cycle
Basics for better bash scripts
(1) Variables
(2) Conditionals
(3) Loops
(4) Regex
(5) Mathematical Skill
(6) Debugging Skill
(7) Understand Problem life cycle
Basics for better bash scripts
#!/usr/bin/env bash
##
## HEADER
# Include other bash file
source helper.sh
# Read a file and execute line by line
for server in $(cat servers.txt); do
echo "Server Info: ${server} n"
done
# Ask for confirmation
read -p "You need choice?" yn
case $yn in
[yY] | [yY][Ee][Ss] )
# YOUR YES CODE HERE
;;
[nN] | [nN][Oo] )
# YOUR NO CODE HERE
;;
* ) echo "Please answer yes or no.";;
Esac
# Create File/Directory if not Exists
[ ! -f /tmp/testfile.log ] && touch /tmp/testfile.log
[ ! -d /tmp/mydir ] && mkdir -p /tmp/mydir
# If not active node version 8 then activate using nvm
NVM=https://raw.githubusercontent.com/nvm-
sh/nvm/v0.38.0/install.sh
if [ -x "$(command -v nvm)" ]; then
sh -c "$(curl -fsSL $NVM)"
source ~/.profile
fi
# finds out where bash is on your system
Whereisbash=$(whereis bash)
# Check command executed or not
<command-here>
if [ $? -eq 0 ]; then
echo OK
else
echo FAIL
Fi
# Check profile data on execution time
start=$(date +%s)
# YOUR NO CODE HERE
end=$(date +%s)
echo "Elapsed Time: $(($end-$start)) seconds"
Example:
5 Tips for better bash scripts
(1) Always start with a shebang with header description
(2) Start debug options when developing
(3) Create descriptive variable names
(4) Use $(command) for command substitution
(5) Before you exit on error describe the problem
5 Tips for better bash scripts
(1) Always start with a shebang with header description
#!/usr/bin/env bash
##
## DESCRIPTION: [Description]
## AUTHOR: [Author]
## DATE: [XX_XX_XXXX.XX_XX_XX]
## VERSION: [Version]
## USAGE: [Usage]
##
NOTE:
# How To Start A shebang
#!/bin/sh [For sh scripting]
#!/bin/bash [For bash scripting]
#!/usr/bin/perl [For perl programming]
#!/bin/awk -f [For awk scripting]
#!/usr/bin/env bash [For more portable than #!/bin/bash]
(2) Start debug options when developing
(3) Create descriptive variable names
(4) Use $(command) for command substitution
(5) Before you exit on error describe the problem
5 Tips for better bash scripts
(1) Always start with a shebang with header description
(2) Start debug options when developing
SHORTFORM FULLFORM DESCRIPTION
set -f set -o noglob Disable file name generation using metacharacters (globbing).
set –v set -o verbose Prints shell input lines as they are read.
set –x set -o xtrace Print command traces before executing command.
set -o pipefail Return the exit status of the last command piped command
set -u set -o nounset Exit if an unsed variable is used
set –e set -o errexit let script exit if a command fails
set -n dry run shell script commands
Usage:
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
OR
#!/usr/bin/env bash
set -xv # this line will enable debug
# --- YOUR CODE HERE --- #
set +xv # this line will disable debug
(3) Create descriptive variable names
(4) Use $(command) for command substitution
(5) Before you exit on error describe the problem
5 Tips for better bash scripts
(1) Always start with a shebang with header description
(2) Start debug options when developing
(3) Create descriptive variable names
## Strings should have the form name=value.
## Ideally variable names should only consist uppercase letters, digits, and the ‘_’ (underscore).
## Variable Names shall not contain the character ‘=’ or ‘-‘
## No spaces before or after that equal sign
## Double quotes around every parameter expansion
## Don’t start Variable Name with special characters or Numbers
Example: song="My song.mp3"
(4) Use $(command) for command substitution
(5) Before you exit on error describe the problem
5 Tips for better bash scripts
(1) Always start with a shebang with header description
(2) Start debug options when developing
(3) Create descriptive variable names
(4) Use $(command) for command substitution
#!/usr/bin/env bash
##
## DESCRIPTION: [Description]
## AUTHOR: [Author]
## DATE: [XX_XX_XXXX.XX_XX_XX]
## VERSION: [Version]
## USAGE: [Usage]
##
users=$(cat users.txt)
(5) Before you exit on error describe the problem
5 Tips for better bash scripts
(1) Always start with a shebang with header description
(2) Start debug options when developing
(3) Create descriptive variable names
(4) Use $(command) for command substitution
(5) Before you exit on error describe the problem
Bash Exit Codes
The exit code is a number between 0 and 255
• Success – A zero (0) value represents success.
• failure – A non-zero exit-code represents failure.
#!/usr/bin/env bash
listall=$(ls –la)
if [ $? -eq 0 ]; then
echo OK
else
echo FAIL
fi
NOTE: $?-- Holds the status of the last pipe close, back-quote string, or system() function.
5 Tips for better bash scripts
High level view for better bash scripts
(1) Prerequisites
(2) Program Logic
(3) Create and Moving files/directories
(4) Change Owner access
(5) Create Soft/Hard links
(6) Check installation
High level view for better bash scripts
#!/usr/bin/env bash
##
## HEADER
# PREREQUISITES
# Include other bash file
source helper.sh
# PROGRAM LOGIC
# Read a file and execute line by line
for server in $(cat servers.txt); do
echo "Server Info: ${server} n"
done
# Ask for confirmation
read -p "You need choice?" yn
case $yn in
[yY] | [yY][Ee][Ss] )
# YOUR YES CODE HERE
;;
[nN] | [nN][Oo] )
# YOUR NO CODE HERE
;;
* ) echo "Please answer yes or no.";;
esac
# CREATE FILES, DIRECTORIES
# Create File/Directory if not Exists
[ ! -f /tmp/testfile.log ] && touch /tmp/testfile.log
[ ! -d /tmp/mydir ] && mkdir -p /tmp/mydir
# If not active node version 8 then activate using nvm
NVM=https://raw.githubusercontent.com/nvm-
sh/nvm/v0.38.0/install.sh
if [ -x "$(command -v nvm)" ]; then
sh -c "$(curl -fsSL $NVM)"
source ~/.profile
fi
# finds out where bash is on your system
Whereisbash=$(whereis bash)
# CHECK INSTALLATION
# Check command executed or not
<command-here>
if [ $? -eq 0 ]; then
echo OK
else
echo FAIL
Fi
# PROFILE YOUR CODE
# Check profile data on execution time
start=$(date +%s)
# YOUR NO CODE HERE
end=$(date +%s)
echo "Elapsed Time: $(($end-$start)) seconds"
Example:
QUESTIONS/QUERIES
https://madewithlove.com/blog/software-engineering/follow-these-4-steps-to-take-your-debugging-skills-to-the-next-level/
https://kvz.io/bash-best-practices.html
https://wiki.bash-hackers.org/dict/terms/exit_status
https://tldp.org/LDP/abs/html/exitcodes.html
https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash
References:

More Related Content

PPT
Bash shell
xylas121
 
PPT
Shell Scripting
Gaurav Shinde
 
PDF
Shell scripting
Manav Prasad
 
PPT
390aLecture05_12sp.ppt
mugeshmsd5
 
PDF
Bash production guide
Adrien Mahieux
 
PDF
Module 03 Programming on Linux
Tushar B Kute
 
PPT
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ewout2
 
PPTX
Bash Shell Scripting
Raghu nath
 
Bash shell
xylas121
 
Shell Scripting
Gaurav Shinde
 
Shell scripting
Manav Prasad
 
390aLecture05_12sp.ppt
mugeshmsd5
 
Bash production guide
Adrien Mahieux
 
Module 03 Programming on Linux
Tushar B Kute
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ewout2
 
Bash Shell Scripting
Raghu nath
 

Similar to KT on Bash Script.pptx (20)

PPTX
Linux Shell Scripting
Raghu nath
 
PPTX
shellScriptAlt.pptx
NiladriDey18
 
PPTX
First steps in C-Shell
Brahma Killampalli
 
PPT
Talk Unix Shell Script 1
Dr.Ravi
 
PPT
Shell programming
Moayad Moawiah
 
PPT
2-introduction_to_shell_scripting
erbipulkumar
 
PDF
Unit 11 configuring the bash shell – shell script
root_fibo
 
PPT
Talk Unix Shell Script
Dr.Ravi
 
PDF
Lets make better scripts
Michael Boelen
 
PPT
ShellAdvanced shell scripting programm.ppt
ubaidullah75790
 
DOCX
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
adkinspaige22
 
DOCX
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
MARRY7
 
PDF
extending-php
tutorialsruby
 
PDF
extending-php
tutorialsruby
 
PDF
extending-php
tutorialsruby
 
PDF
extending-php
tutorialsruby
 
PDF
extending-php
tutorialsruby
 
PDF
extending-php
tutorialsruby
 
PPT
34-shell-programming asda asda asd asd.ppt
poyotero
 
PPT
Airlover 20030324 1
Dr.Ravi
 
Linux Shell Scripting
Raghu nath
 
shellScriptAlt.pptx
NiladriDey18
 
First steps in C-Shell
Brahma Killampalli
 
Talk Unix Shell Script 1
Dr.Ravi
 
Shell programming
Moayad Moawiah
 
2-introduction_to_shell_scripting
erbipulkumar
 
Unit 11 configuring the bash shell – shell script
root_fibo
 
Talk Unix Shell Script
Dr.Ravi
 
Lets make better scripts
Michael Boelen
 
ShellAdvanced shell scripting programm.ppt
ubaidullah75790
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
adkinspaige22
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
MARRY7
 
extending-php
tutorialsruby
 
extending-php
tutorialsruby
 
extending-php
tutorialsruby
 
extending-php
tutorialsruby
 
extending-php
tutorialsruby
 
extending-php
tutorialsruby
 
34-shell-programming asda asda asd asd.ppt
poyotero
 
Airlover 20030324 1
Dr.Ravi
 
Ad

Recently uploaded (20)

PPTX
World-population.pptx fire bunberbpeople
umutunsalnsl4402
 
PDF
202501214233242351219 QASS Session 2.pdf
lauramejiamillan
 
PDF
Key_Statistical_Techniques_in_Analytics_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
short term project on AI Driven Data Analytics
JMJCollegeComputerde
 
PPTX
Web dev -ppt that helps us understand web technology
shubhragoyal12
 
PPT
From Vision to Reality: The Digital India Revolution
Harsh Bharvadiya
 
PPTX
HSE WEEKLY REPORT for dummies and lazzzzy.pptx
ahmedibrahim691723
 
PPTX
Introduction to Data Analytics and Data Science
KavithaCIT
 
PPTX
Introduction to computer chapter one 2017.pptx
mensunmarley
 
PPTX
Fluvial_Civilizations_Presentation (1).pptx
alisslovemendoza7
 
PPTX
INFO8116 -Big data architecture and analytics
guddipatel10
 
PDF
Classifcation using Machine Learning and deep learning
bhaveshagrawal35
 
PPTX
Pipeline Automatic Leak Detection for Water Distribution Systems
Sione Palu
 
PDF
Fundamentals and Techniques of Biophysics and Molecular Biology (Pranav Kumar...
RohitKumar868624
 
PPTX
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
PDF
Blitz Campinas - Dia 24 de maio - Piettro.pdf
fabigreek
 
PPTX
IP_Journal_Articles_2025IP_Journal_Articles_2025
mishell212144
 
PPTX
Databricks-DE-Associate Certification Questions-june-2024.pptx
pedelli41
 
PPTX
The whitetiger novel review for collegeassignment.pptx
DhruvPatel754154
 
PPTX
Presentation on animal welfare a good topic
kidscream385
 
World-population.pptx fire bunberbpeople
umutunsalnsl4402
 
202501214233242351219 QASS Session 2.pdf
lauramejiamillan
 
Key_Statistical_Techniques_in_Analytics_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
short term project on AI Driven Data Analytics
JMJCollegeComputerde
 
Web dev -ppt that helps us understand web technology
shubhragoyal12
 
From Vision to Reality: The Digital India Revolution
Harsh Bharvadiya
 
HSE WEEKLY REPORT for dummies and lazzzzy.pptx
ahmedibrahim691723
 
Introduction to Data Analytics and Data Science
KavithaCIT
 
Introduction to computer chapter one 2017.pptx
mensunmarley
 
Fluvial_Civilizations_Presentation (1).pptx
alisslovemendoza7
 
INFO8116 -Big data architecture and analytics
guddipatel10
 
Classifcation using Machine Learning and deep learning
bhaveshagrawal35
 
Pipeline Automatic Leak Detection for Water Distribution Systems
Sione Palu
 
Fundamentals and Techniques of Biophysics and Molecular Biology (Pranav Kumar...
RohitKumar868624
 
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
Blitz Campinas - Dia 24 de maio - Piettro.pdf
fabigreek
 
IP_Journal_Articles_2025IP_Journal_Articles_2025
mishell212144
 
Databricks-DE-Associate Certification Questions-june-2024.pptx
pedelli41
 
The whitetiger novel review for collegeassignment.pptx
DhruvPatel754154
 
Presentation on animal welfare a good topic
kidscream385
 
Ad

KT on Bash Script.pptx

  • 2. sh is a specification, bash is the implementation. (1) sh Strictly speaking, sh (or the Shell Command Language) is a programming language described by the POSIX standard. The key here is that sh isn’t really a programming language in itself. It is just a specification. Specification is a detailed description of the syntax and semantics of that language. Specification doesn’t include an implementation. (2) bash There are different operating systems with their own shells and kernel. Bash is the shell for the GNU operating system. But we can also say that bash is a programming language. sh vs bash
  • 3. (1) Variables (2) Conditionals (3) Loops (4) Regex (5) Mathematical Skill (6) Debugging Skill (7) Understand Problem life cycle Basics for better bash scripts
  • 4. (1) Variables #!/usr/bin/env bash LINK_OR_DIR="/tmp/coordinator" if [ -d "$LINK_OR_DIR" ]; then # If Condition is satisfied else: # If Condition is not satisfies fi (2) Conditionals (3) Loops (4) Regex (5) Mathematical Skill (6) Debugging Skill (7) Understand Problem life cycle Basics for better bash scripts
  • 5. (1) Variables (2) Conditionals #!/usr/bin/env bash if [ -d "$LINK_OR_DIR" ]; then # If Condition is satisfied else: # If Condition is not satisfies fi ## With the same syntax you can use: ## -e: any kind of archive ## -f: file ## -h: symbolic link ## -r: readable file ## -w: writable file ## -x: executable file ## -s: file size greater than zero ## -d: check if it's a directory ## -L: check if it's a symbolic link ## -z: if the length of string is zero ## -n: length of string is non-zero (3) Loops (4) Regex (5) Mathematical Skill (6) Debugging Skill (7) Problem life cycle Basics for better bash scripts
  • 6. (1) Variables (2) Conditionals (3) Loops #!/usr/bin/env bash ## While loop while [ <some test> ] do <commands> done ## Until loop until [ <some test> ] do <commands> done ## For loop for var in <list> do <commands> done (1) Regex (2) Mathematical Skill (3) Debugging Skill (4) Understand Problem life cycle Basics for better bash scripts
  • 7. (1) Variables (2) Conditionals (3) Loops (4) Regex #!/usr/bin/env bash t="abc123" [[ "$t" == abc* ]] # true (globbing) [[ "$t" == "abc*" ]] # false (literal matching) [[ "$t" =~ [abc]+[123]+ ]] # true (regular expression) [[ "$t" =~ "abc*" ]] # false (literal matching) Note, that starting with bash version 3.2 the regular or globbing expression must not be quoted. If your expression contains whitespace you can store it in a variable: r="a b+" [[ "a bbb" =~ $r ]] # true (5) Mathematical Skill (6) Debugging Skill (7) Understand Problem life cycle Basics for better bash scripts
  • 8. (1) Variables (2) Conditionals (3) Loops (4) Regex (5) Mathematical Skill Matrix algebra Logical gates (6) Debugging Skill (7) Understand Problem life cycle Basics for better bash scripts
  • 9. (1) Variables (2) Conditionals (3) Loops (4) Regex (5) Mathematical Skill (6) Debugging Skill 1. Reproduce Before you scramble and try to fix the bug as soon as possible, make sure you can actually reproduce it first. 2. Investigate Find the bug in the code and the cause! 3. Know Your Error Codes This is a bit of strange one to hear, but given how fundamental are worth here. 4. Fix & Verify Now verify that it is actually fixed and, more importantly, that your fix didn't break anything else. (7) Understand Problem life cycle Basics for better bash scripts
  • 10. (1) Variables (2) Conditionals (3) Loops (4) Regex (5) Mathematical Skill (6) Debugging Skill (7) Understand Problem life cycle Basics for better bash scripts
  • 11. #!/usr/bin/env bash ## ## HEADER # Include other bash file source helper.sh # Read a file and execute line by line for server in $(cat servers.txt); do echo "Server Info: ${server} n" done # Ask for confirmation read -p "You need choice?" yn case $yn in [yY] | [yY][Ee][Ss] ) # YOUR YES CODE HERE ;; [nN] | [nN][Oo] ) # YOUR NO CODE HERE ;; * ) echo "Please answer yes or no.";; Esac # Create File/Directory if not Exists [ ! -f /tmp/testfile.log ] && touch /tmp/testfile.log [ ! -d /tmp/mydir ] && mkdir -p /tmp/mydir # If not active node version 8 then activate using nvm NVM=https://raw.githubusercontent.com/nvm- sh/nvm/v0.38.0/install.sh if [ -x "$(command -v nvm)" ]; then sh -c "$(curl -fsSL $NVM)" source ~/.profile fi # finds out where bash is on your system Whereisbash=$(whereis bash) # Check command executed or not <command-here> if [ $? -eq 0 ]; then echo OK else echo FAIL Fi # Check profile data on execution time start=$(date +%s) # YOUR NO CODE HERE end=$(date +%s) echo "Elapsed Time: $(($end-$start)) seconds" Example:
  • 12. 5 Tips for better bash scripts
  • 13. (1) Always start with a shebang with header description (2) Start debug options when developing (3) Create descriptive variable names (4) Use $(command) for command substitution (5) Before you exit on error describe the problem 5 Tips for better bash scripts
  • 14. (1) Always start with a shebang with header description #!/usr/bin/env bash ## ## DESCRIPTION: [Description] ## AUTHOR: [Author] ## DATE: [XX_XX_XXXX.XX_XX_XX] ## VERSION: [Version] ## USAGE: [Usage] ## NOTE: # How To Start A shebang #!/bin/sh [For sh scripting] #!/bin/bash [For bash scripting] #!/usr/bin/perl [For perl programming] #!/bin/awk -f [For awk scripting] #!/usr/bin/env bash [For more portable than #!/bin/bash] (2) Start debug options when developing (3) Create descriptive variable names (4) Use $(command) for command substitution (5) Before you exit on error describe the problem 5 Tips for better bash scripts
  • 15. (1) Always start with a shebang with header description (2) Start debug options when developing SHORTFORM FULLFORM DESCRIPTION set -f set -o noglob Disable file name generation using metacharacters (globbing). set –v set -o verbose Prints shell input lines as they are read. set –x set -o xtrace Print command traces before executing command. set -o pipefail Return the exit status of the last command piped command set -u set -o nounset Exit if an unsed variable is used set –e set -o errexit let script exit if a command fails set -n dry run shell script commands Usage: #!/usr/bin/env bash set -o errexit set -o nounset set -o pipefail OR #!/usr/bin/env bash set -xv # this line will enable debug # --- YOUR CODE HERE --- # set +xv # this line will disable debug (3) Create descriptive variable names (4) Use $(command) for command substitution (5) Before you exit on error describe the problem 5 Tips for better bash scripts
  • 16. (1) Always start with a shebang with header description (2) Start debug options when developing (3) Create descriptive variable names ## Strings should have the form name=value. ## Ideally variable names should only consist uppercase letters, digits, and the ‘_’ (underscore). ## Variable Names shall not contain the character ‘=’ or ‘-‘ ## No spaces before or after that equal sign ## Double quotes around every parameter expansion ## Don’t start Variable Name with special characters or Numbers Example: song="My song.mp3" (4) Use $(command) for command substitution (5) Before you exit on error describe the problem 5 Tips for better bash scripts
  • 17. (1) Always start with a shebang with header description (2) Start debug options when developing (3) Create descriptive variable names (4) Use $(command) for command substitution #!/usr/bin/env bash ## ## DESCRIPTION: [Description] ## AUTHOR: [Author] ## DATE: [XX_XX_XXXX.XX_XX_XX] ## VERSION: [Version] ## USAGE: [Usage] ## users=$(cat users.txt) (5) Before you exit on error describe the problem 5 Tips for better bash scripts
  • 18. (1) Always start with a shebang with header description (2) Start debug options when developing (3) Create descriptive variable names (4) Use $(command) for command substitution (5) Before you exit on error describe the problem Bash Exit Codes The exit code is a number between 0 and 255 • Success – A zero (0) value represents success. • failure – A non-zero exit-code represents failure. #!/usr/bin/env bash listall=$(ls –la) if [ $? -eq 0 ]; then echo OK else echo FAIL fi NOTE: $?-- Holds the status of the last pipe close, back-quote string, or system() function. 5 Tips for better bash scripts
  • 19. High level view for better bash scripts
  • 20. (1) Prerequisites (2) Program Logic (3) Create and Moving files/directories (4) Change Owner access (5) Create Soft/Hard links (6) Check installation High level view for better bash scripts
  • 21. #!/usr/bin/env bash ## ## HEADER # PREREQUISITES # Include other bash file source helper.sh # PROGRAM LOGIC # Read a file and execute line by line for server in $(cat servers.txt); do echo "Server Info: ${server} n" done # Ask for confirmation read -p "You need choice?" yn case $yn in [yY] | [yY][Ee][Ss] ) # YOUR YES CODE HERE ;; [nN] | [nN][Oo] ) # YOUR NO CODE HERE ;; * ) echo "Please answer yes or no.";; esac # CREATE FILES, DIRECTORIES # Create File/Directory if not Exists [ ! -f /tmp/testfile.log ] && touch /tmp/testfile.log [ ! -d /tmp/mydir ] && mkdir -p /tmp/mydir # If not active node version 8 then activate using nvm NVM=https://raw.githubusercontent.com/nvm- sh/nvm/v0.38.0/install.sh if [ -x "$(command -v nvm)" ]; then sh -c "$(curl -fsSL $NVM)" source ~/.profile fi # finds out where bash is on your system Whereisbash=$(whereis bash) # CHECK INSTALLATION # Check command executed or not <command-here> if [ $? -eq 0 ]; then echo OK else echo FAIL Fi # PROFILE YOUR CODE # Check profile data on execution time start=$(date +%s) # YOUR NO CODE HERE end=$(date +%s) echo "Elapsed Time: $(($end-$start)) seconds" Example: