0% found this document useful (0 votes)
3 views37 pages

DevOps - Bash Scripting

The document provides a comprehensive guide to Bash scripting in Linux, covering topics such as variable declaration, control flow statements, functions, and command substitution. It explains the purpose of scripting for automation and routine tasks, along with syntax and examples for creating scripts. Additionally, it includes sections on debugging, input/output redirection, and exit status codes, along with practical lab exercises for hands-on learning.

Uploaded by

Yogesh Mungase
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views37 pages

DevOps - Bash Scripting

The document provides a comprehensive guide to Bash scripting in Linux, covering topics such as variable declaration, control flow statements, functions, and command substitution. It explains the purpose of scripting for automation and routine tasks, along with syntax and examples for creating scripts. Additionally, it includes sections on debugging, input/output redirection, and exit status codes, along with practical lab exercises for hands-on learning.

Uploaded by

Yogesh Mungase
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

DevOps – Bash Scripting

Creating Programs in Linux


Topics

▪ What is Scripting
▪ Declaring variables
▪ Declaring arrays
▪ Basic syntax
▪ Control flow statements
▪ Creating Functions

2
Bash/Shell Scripting
What is Shell Scripting:
▪ Executing series of commands, instead of writing commands one by one
every time
▪ they can be arranged and put in a file and then by executing the file, all
commands can be executed
▪ Like batch files in MS-DOS
Why do we script:
▪ Admins write scripts for routine activities, clearing logs, taking backups
▪ Automate other repetitive work
▪ Sharing common complex tasks with other user
▪ creating new commands using combination of

3
Shell Scripting
▪ Every script file starts with - #!/bin/bash – called “shebang” first line in bash
script indicating which scripting language we use. (sharp - # and bang - !)
▪ /bin/bash is going to interpret our commands inside the shell
▪ Instead of this, one can use Python to write Python scripts
▪ Shell scripts typically have extension .sh but it is not mandatory
▪ Comments with #
▪ .sh file should have execute permission (chmod +x myscript.sh)
▪ Executing script – give absolute or relative path or use path env

Your first script


$ echo '#!/bin/bash’ > firstscript.sh
$ echo 'echo Hello World' >> firstscript.sh
$ chmod 755 firstscript.sh
$ ./firstscript.sh
basics/firstscript.sh
Hello World
4
Shell Scripting - variables
▪ Declaring a variable – Variable name followed by equal to and then
value. Then variables can be used inside the script preceded by $
sign. E.g. JDKPATH=“/bin/java/jdk” if [ $JDKPATH ==
“/bin/java/jdk” ]
▪ Read a variable from command line – read Name and then use in the
shell as $Name

#!/bin/bash
#Accepting variable from command Line
surname=“Thyself"
echo "Enter your name"
read Name
echo "You provided $Name $surname as a
parameter"
basics/input.sh

5
Shell Scripting – variables types
▪ Syntax - declare OPTION(s) VARIABLE=value
▪ declare –p VariableName – gives what type of variable it is, int,
string etc.
▪ declare –i variablename – declares the variable as integer. Now
you cannot assign string to this variable.
▪ declare –r variablename – declares variable as read only. We
cannot change the value of this variable and a session.
▪ Unsetting a variable = unset name

basics/variables.sh

6
Shell Scripting – arrays
▪ Variable has indexes associated with individual values
▪ Declaring array
▪ company=(“Infosys” “TCS” “IBM”) – space and NOT comma delimited
▪ echo ${company[0]}
▪ echo ${company[*]} or echo ${company[@]}
▪ Adding a value after declaration
▪ Company[3]=“TechM”
▪ But one cannot decrease the size of array
▪ NUM=(1 3 6 2 6 2 5)
▪ echo $NUM – only the first value
▪ echo ${NUM[3]} - 2
▪ mixed=(1 three 6 two “Hello” 2 5)
▪ echo $NUM – only the first value
arrays/company.sh
▪ echo ${NUM[3]} - two
arrays/num.sh

7
Command Substitution
▪ Substitute the result of a command as a portion of another command
▪ Using backticks(`)
▪ Enclosing the inner command in $()
▪ Innermost command will be executed in a newly launched shell
environment, and the standard output of the shell will be inserted
where the command substitution was done.
▪ $( ) method allows command nesting. Use this one over the
backticks
▪ cd /lib/modules/$(uname -r)/

basics/externprograms.sh

8
Shell Scripting – External commands
$ MYNAME=`grep "^${USER}:" /etc/passwd | cut -d: -f5`
$ echo $MYNAME

#!/bin/sh
find / -name "*.html" -print | grep "/index.html$“
find / -name "*.html" -print | grep "/contents.html$"

#!/bin/sh
HTML_FILES=`find / -name "*.html" -print`
echo "$HTML_FILES" | grep "/index.html$"
echo "$HTML_FILES" | grep "/contents.html$"
basics/externprograms.sh

9
Return values
▪ All scripts return a value
▪ It is 0 for success
▪ Any other numeric value for error
▪ One can set the return value using “exit” at the end of the script
▪ Using “$?” will give you the status of previous command execution
▪ Try searching a file on command line which exist and then echo the
value of $?
▪ Do the same thing for file which do not exist

10
Special Characters

Character Description
# Used to add a comment, except when used as #! when starting a
script
\ Used at the end of a line to indicate continuation on to the next
line
; Used to interpret what follows as a new command
$ Indicates what follows is a variable

11
Multiple commands – Single line
▪ The ; (semicolon) character is used to separate these commands.
Following commands will execute even if the ones preceding them
fail:
$ make ; make install ; make clean
▪ If you may want to abort subsequent commands if one fails. use the
&& (and) operator as in:
$ make && make install && make clean
▪ To proceed until something succeeds and then you stop executing
any further steps :
$ cat file1 || cat file2 || cat file3

12
Single Command – multiple line
▪ Chaining using concatenation operator (\)
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

13
Script Parameters
▪ Create script which will print the first, last name which are
passed to the script from command line

Parameter Meaning

$0 Script name

$1 First parameter

$2, $3, etc. Second, third parameter, etc.

$* All parameters

$# Number of arguments
basics/scriptpara.sh

14
Input/Output redirection
▪ Sending output of a command to a file
▪ ls –la > abc.txt – creating/overwrite
▪ ls –la >> abc.txt – append

▪ Reading input of a command from a file


▪ wc -l < /etc/passwd

basics/scriptpara.sh

15
Arithmetic expression
▪ Using the expr utility: deprecated
▪ expr 8 + 8
▪ echo $(expr 8 + 8)

▪ Using the $((...)) syntax: This is the built-in shell format.


▪ echo $((x+1))

▪ Using the built-in shell command let. :


▪ let x=(1+2); echo $x

basics/arithmetic.sh

16
Shell Scripting – conditional statements

▪ Conditional statements
if [ “$PWD” == “$HOME” ]
then
echo “You are home”
else
echo “You are not home, you are in $PWD”
fi

if/athome.sh

17
Shell Scripting – Comparing Strings
if [ string1 == string2 ] ; then
ACTION
fi

if [ 2 -ne 5 ] ; then
ACTION
fi

if/execops.sh

18
Numerical Tests
Operator Meaning
-eq Equal to
-ne Not equal to
-gt Greater than
-lt Less than
-ge Greater than or equal to
-le Less than or equal to

19
loops/IFSwhile.sh

if/ifelseif.sh
File Conditionals
Condition Meaning
-e file Check if the file exists.
-d file Check if the file is a directory.
-f file Check if the file is a regular file (i.e., not a symbolic link, device
node, directory, etc.)
-s file Check if the file is of non-zero size.
-g file Check if the file has sgid set.
-u file Check if the file has suid set.
-r file Check if the file is readable.
-w file Check if the file is writable.
-x file Check if the file is executable.

20
Boolean Expressions
Operator Operation Meaning

&& AND The action will be performed only if both the conditions
evaluate to true.

|| OR The action will be performed if any one of the


conditions evaluate to true.

! NOT The action will be performed only if the condition


evaluates to false.

21
Shell Scripting – Conditional Statements
loops/listcompfor.sh
For loop

▪ Construct for i in Infosys TCS CTS Accenture


for variable-name in list do

do echo “$i”

Statements to be repeated done

done loops/listnumfor.sh
Another example
for i in {7..18}
do
echo “$i”
done

22
Shell Scripting – conditional statements

▪ Construct #!/bin/bash
while condition is true INPUT=Hi
do
while [ "$INPUT" != "bye" ]
Statements to be repeated
done do
echo "Please type something in
(bye to quit)"
read INPUT
echo "You typed: $INPUT"
done

if/daily.sh
23
Shell Scripting – until loop

▪ Construct #!/bin/bash
until condition is false echo "NUMBER"
do
min=1
Statements to be repeated
done max=10
until [ $min -gt $max ]
do
echo "$min"
min=$(($min + 2))
done

loops/untilexample.sh
24
Shell Scripting – case statements

case $INPUT_STRING in
hello)
echo "Hello!"
;;
bye)
echo "See you again!"
break
;;
*)
echo "Sorry, does not match valid inputs"
;;
esac

loops/caseexample.sh
25
Shell Scripting – functions
# A simple script with a function... ###
# you can also declare as function # Main body of script starts here
add_a_user instead of add_a_user() ###
add_a_user() echo "Start of script..."
{ add_a_user "$1" "$2"
USER=$1 add_a_user fred badpassword Fred
PASSWORD=$2 Durst the singer
shift; shift; add_a_user bilko worsepassword Sgt.
COMMENTS=$@ Bilko the role model
# Having shifted twice, the rest is now echo "End of script..."
comments ...
echo "Adding user $USER ..."
echo useradd -c "$COMMENTS" $USER
echo passwd $USER $PASSWORD
echo "Added user $USER ($COMMENTS) with
password $PASSWORD"
} functions/functionexample.sh
26
Shell Scripting – Debugging scripts

▪ you can run a script in debug mode by


▪ bash –x ./script_file.
Debug mode helps identify the error because:
▪ It traces and prefixes each command with the + character.
▪ It displays each command before executing it
▪ It can debug only selected parts of a script (if desired)
with the following inserted in the script file:
set -x # turns on debugging
set +x # turns off debugging

debug/debugging.sh
27
Standard Input, Output and Error

▪ A process structure is constructed with numbered channels (file descriptors)


to manage open files.
▪ Processes connect to files to reach data content or devices these files
represent.
▪ Processes are created with default connections for channels 0, 1 and 2,
known as standard input, standard output and standard error.

28
Channel

▪ Redirecting Output to a file


▪ Channel redirection replaces default channel destinations with file names
representing either output file or devices.
▪ Using redirection, process output and error messages can be captured as
file contents, sent to a device, or discarded.
▪ The special file /dev/null quietly discards channel output redirected to it.

29
Questions?

30
Labs – Exit Status

▪ Write a script which does an ls for a nonexistent file,


followed by a display of the exit status code. Then create a
file and display the new exit code. In each task, send the
ls output to /dev/null

31
Labs – Working with files

▪ Write the script that will ask the user for a directory
name, which the script will create. Change the working
directory to the new directory and tell the user where you
are using the pwd command
▪ Use touch to create a few files followed by displaying the
filenames. Use echo and redirection to put some content into
the files and show the user the content. Finally say goodbye
to the user to end the script.

32
Labs – Working with numbers

▪ Ask the user to enter the first number


▪ Read the first number
▪ Ask user to enter the second number
▪ Read the second number
▪ Compare two and if both are zero then display “both are
zero”
▪ If not then check if both are equal print “they are equal”
▪ If not then check if first is great than second
▪ Else if print “ second is greater than first”

33
https://access.redhat.com/articles/1189123

Exit Status
Exit Code Number Meaning Example Comments

1 Catchall for general errors let "var1 = 1/0" Miscellaneous errors, such as "divide by
zero" and other impermissible operations
2 Misuse of shell builtins empty_function() {} Missing keyword or command, or permission
(according to Bash problem (and diff return code on a failed
documentation) binary file comparison).
126 Command invoked cannot /dev/null Permission problem or command is not an
execute executable
127 "command not found" illegal_command Possible problem with $PATH or a typo

128 Invalid argument to exit exit 3.14159 exit takes only integer args in the range 0 -
255 (see first footnote)
128+n Fatal error signal "n" kill -9 $PPID of script $? returns 137 (128 + 9)

130 Script terminated by Ctl-C Control-C is fatal error signal 2, (130 = 128 + 2,
Control-C see above)
255* Exit status out of range exit -1 exit takes only integer args in the range 0 - 255

34
Labs-Exist Status Solution

35
Labs-Working with files

36
Labs-Working with numbers

37

You might also like