Scripting Languages M2
Scripting Languages M2
Module 2
Introduction to Shell Script (bash)
Learning Objectives
awk utility Non-compiled scripting language utility used for simple to complex
data manipulation data and report generation
basename command Strips directory and suffix from filenames
bc utility Arbitrary precision calculator language
cat command Concatenate and print (display) the content of files
chmod command Change access permissions to files and folders
clear command Clear terminal screen of all text
cp command Copy one or more files/folders to another location
cut command Rules-based division of a file’s or variable’s argument resident data
grep utility Search file(s) for lines that match a given pattern
if keyword Conditionally perform a command
Common bash Commands
Shell Script
Commands to create
~/CSI6203/
~/CSI6203/
shell script file, make it
~/CSI6203/ executable and run it
~/CSI6203/
~/CSI6203/
Working with variables
and arguments
Variables
• Information in bash scripts can be stored in variables
• Once information is stored in a variable, it can be called upon in
the script by invoking the variable’s name
IMPORTANT NOTE:
When you declare a variable
and/or assign it an argument,
you do so without the $
prepend, as shown in the yellow
boxes. However, when you call
upon a variable to use its
argument in the code, you must
use the $ prepend in each case
as shown in the blue boxes.
Passing Arguments to Default Variables
• There are some special variables that are used for receiving input from
the command line as they are executed
• Each argument typed into the command line is referred to by a special
numeric variable
Special Argument
Variable
$ ./hello.sh Tom $0 Holds the name of the script being run
Hello Tom
$1, $2, $3 Arguments passed along with the running of the
etc script on the command line, e.g. myscript.sh 20
#!/bin/bash 30 40 - $1 is the first argument, i.e. 20, $2 the
echo "Hello $1!" second arguments, i.e 30 and $3 is the third
argument, i.e. 40 ..and so on
$# Holds the number of arguments passed to the
script
$? Holds the last output of the script
Note: This can change constantly as the script
executes
Arguments
• Shell scripting
• Bash shell and scripting
• Text Editors
• Variables
• Git and github.com