KEMBAR78
Buoi 07 ShellScript | PDF | Regular Expression | Computing
0% found this document useful (0 votes)
11 views83 pages

Buoi 07 ShellScript

This document provides an introduction to shell scripting, covering basic concepts such as file manipulation, program execution, and decision-making structures like if statements and loops. It includes examples of mathematical calculations, string comparisons, and the use of arrays and functions in shell scripts. Additionally, it touches on the use of AWK for pattern matching and processing text files.

Uploaded by

huynhanthai31
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)
11 views83 pages

Buoi 07 ShellScript

This document provides an introduction to shell scripting, covering basic concepts such as file manipulation, program execution, and decision-making structures like if statements and loops. It includes examples of mathematical calculations, string comparisons, and the use of arrays and functions in shell scripts. Additionally, it touches on the use of AWK for pattern matching and processing text files.

Uploaded by

huynhanthai31
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/ 83

SHELL Scripts introduction

A shell script is a computer program designed to be run by


the Unix shell, a command-line interpreter.[1] The various
dialects of shell scripts are considered to be scripting
languages. Typical operations performed by shell scripts
include file manipulation, program execution, and printing
text. A script which sets up the environment, runs the
program, and does any necessary cleanup, logging, etc. is
called a wrapper.
Math Caculation

➢ let
➢ (( ))
➢ []
➢ expr
➢ bc
Math Calculation - let
⚫ #!/bin/bash
⚫ NUMBER=7
⚫ let RESULT=NUMBER+5
Math Calculation - let
⚫ #!/bin/bash
⚫ NUMBER=7
⚫ let RESULT=NUMBER+5

⚫ Increment operation
⚫ let NUMBER++ (let NUMBER+=5)
⚫ Decrement operation
⚫ let NUMBER-- (let NUMBER-=5)
Math Calculation - (( ))
⚫ RESULT=$(( NUMBER + 5 ))
Math Calculation - [ ]
⚫ RESULT=$[ NUMBER + 5 ]
Math Calculation - expr
⚫ RESULT=$(expr $NUMBER + 5)
⚫ RESULT=`expr $NUMBER + 5`

⚫ RESULT=`expr 2 + 3`
IF Statements & Decision Making
⚫ Explaining if
⚫ if [ condition ]
⚫ then
− statement(s) when the condition is true
⚫ fi
IF Statements & Decision Making
⚫ Explaining if
⚫ if [ condition ]
⚫ then
− statement(s) when the condition is true
⚫ else
− statement(s) for when condition is false
⚫ fi
IF Statements & Decision Making
⚫ Explaining if
⚫ if [ condition ]
⚫ then
− statement(s) when the condition is true
⚫ elif [ condition ]
⚫ then
− statement(s) for when the condition is true
⚫ elif [ condition ]
⚫ then
− statement(s) for when the condition is true
⚫ else
− statement(s) for when the no previously typed conditions were true
IF Statements & Decision Making
⚫ Explaining if
⚫ if [ condition ]; then
− statement(s) when the condition is true
⚫ elif [ condition ]; then
− statement(s) for when the condition is true
⚫ elif [ condition ]; then
− statement(s) for when the condition is true
⚫ else
− statement(s) for when the no previously typed conditions were true
IF Statements & Decision Making
⚫ Explaining if
⚫ AND with if statement ?
⚫ if [ condition ] && [ condition ]; then
− statement(s) when the condition is true
⚫ fi
⚫ OR with if statement ?
⚫ if [ condition ] || [ condition ]; then
⚫ statement(s) for when at least one condition is true
⚫ fi
IF Statements & Decision Making
⚫ Explaining if
⚫ Negate if condition
⚫ if [ ! condition ]; then
− statement(s) when the condition is true
⚫ fi

⚫ if ! [ condition ]; then
⚫ statement(s) for when at least one condition is true
⚫ fi
IF Statements & Decision Making
⚫ Explaining if
⚫ Condition
⚫ Mathematical comparison
⚫ String comparison
⚫ Filesystem related tests
Mathematical Comparison
⚫ Explaining if
⚫ Operands
➢ -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
Mathematical Comparison
⚫ Explaining if
⚫ Examples
⚫ if [ $VAR -eq 5 ]; then
⚫ if [ $VAR -gt 5 ]; then
⚫ if [ $VAR -eq 5 ] && [ $COUNT -ne 1 ]; then
Mathematical Comparison
⚫ Explaining if
⚫ Examples
⚫ if [ $VAR -eq 5 ]; then
⚫ if [ $VAR -gt 5 ]; then
⚫ if [ $VAR -eq 5 ] && [ $COUNT -ne 1 ]; then
⚫ Where are spaces ? Every where
⚫ if◘[◘$VAR◘-eq◘5◘];◘then
⚫ if◘[◘$VAR◘-gt◘5◘];◘then
⚫ if◘[◘$VAR◘-eq◘5◘]◘&&◘[◘$COUNT◘-ne◘1◘];◘then
String comparison

⚫ Compare two strings


⚫ [ “$STR1” = “$STR2” ]
⚫ [ “$STR1” != “$STR2” ]
String comparison

⚫ Compare two strings


⚫ [ “$STR1” = “$STR2” ]
⚫ [ “$STR1” != “$STR2” ]

⚫ [ “$STR1” = “hello” ]
⚫ [ “$STR1” != “hello” ]
String comparison

⚫ Compare two strings – don’t forget on spaces


⚫ [◘“$STR1”◘=◘“$STR2”◘]
⚫ [◘“$STR1”◘!=◘“$STR2”◘]

⚫ [◘“$STR1”◘=◘“hello”◘]
⚫ [◘“$STR1”◘!=◘“hello”◘]
String comparison

⚫ Compare two strings – quotation marks ?


⚫ [ “$STR1” = “$STR2” ]
⚫ [ “$STR1” != “$STR2” ]

⚫ [ “$STR1” = “hello” ]
⚫ [ “$STR1” != “hello” ]

⚫ If STR1 or STR2 are not defined or empty, we could get error message:
Unary operator expected when not using “ ” quotation marks
String comparison

⚫ Compare two strings – quotation marks ?


⚫ Bash can handle it in other way [[ ]]
⚫ [[ $STR1 = $STR2 ]]
⚫ [[ $STR1 != $STR2 ]] This is not
widely used and most script
⚫ are using quotation marks with single
brackets

⚫ [[ “$STR1” = “hello” ]]
⚫ [[ “$STR1” != “hello” ]]
String comparison

⚫ Test if string is empty


⚫ [ -z “$STR1” ] return true if STR1 holds an empty string
⚫ [[ -z $STR1 ]] possible, but not widely used

⚫ [ -n “$STR1” ] return true if STR1 holds a non-empty string


⚫ [[ -n $STR1 ]] possible, but not widely used
String comparison

⚫ Alphabetically compare two strings


⚫ [[ $STR1 > $STR2 ]]

⚫ [[ $STR1 < $STR2 ]]


String comparison

⚫ Wildcards
⚫ [[ $STR1 == string-with-wildcards ]]

⚫ Regular expressions
⚫ [[ $STR1 =~ $regex ]]
Wildcards (globbing patterns)

⚫ Used by/for:
⚫ cp
⚫ rm
⚫ ls
⚫ mv
⚫ Bash string comparison
⚫ … many more
Wildcards (globbing patterns)
Wildcards (globbing patterns)


Wildcards (globbing patterns)

⚫ Wildcards in string comparison


⚫ [[ $STRING == partern_with_wildcards ]]
Wildcards (globbing patterns)

⚫ Wildcards in string comparison


⚫ [[ $STRING == partern_with_wildcards ]]
⚫ [[ $STRING == file[0-9].txt ]]
⚫ [[ $STRING == rich* ]]
Regular Expession
Regular Expession
Regular Expession
Regex in Bash
Filesystem Related Tests
Filesystem Related Tests

⚫ if [ -f $FILE ]; then
− echo File exits
⚫ else
− echo File does not exits
⚫ fi
For Loop

⚫ A loop is a block of code that iterates a list of commands


as long as the loop control condition is true
For Loop

⚫ for arg in [list]


⚫ do
⚫ command(s)...
⚫ done
For Loop

⚫ for arg in [list] for PLANET in “Mercury Venus


Earth”
⚫ do do
⚫ command(s)… echo $PLANET
⚫ done done

⚫ #Mercury
⚫ #Venus
⚫ #Earth ===> IFS=$’ \t\n’
For Loop

⚫ for arg in [list] for FILE in *.txt


⚫ do
do
⚫ command(s)… echo $FILE
⚫ done done

⚫ #hello.txt
⚫ #work.txt
⚫ #something.txt
For Loop

⚫ for arg in [list] for NUMBER in {1..10}


⚫ do do
⚫ command(s)… echo $NUMBER
⚫ done done

⚫ #1
⚫ #2
⚫ #..
⚫ #10
For Loop

⚫ for arg in [list] for ARG in $@


⚫ do do
⚫ command(s)… echo $ARG
⚫ done done

⚫ #$1
⚫ #$2
⚫ #..
⚫ #$@
While Loop

⚫ while [ condition ]
⚫ do
⚫ command(s)…
⚫ done
While Loop

⚫ while [ condition ] while [ “$NAME” != “stop” ]


do do
⚫ command(s)… echo -n Enter your name:
⚫ done read NAME
⚫ done
While Loop

⚫ C-style While Loop


⚫ while (( condition )) A=1 LIMIT=10
do while (( A < LIMIT )) //no $
⚫ command(s)… do
⚫ done touch $A
⚫ let A++
⚫ done
Reading File with While Loop

⚫ while read line


⚫ do
⚫ command(s)…
⚫ done < “$FILENAME”

Reading File with While Loop

⚫ while read line cat “$FILENAME” |


⚫ do
while read line
⚫ command(s)… do
⚫ done < “$FILENAME” command(s)
...
⚫ done
Reading File with While Loop

⚫ while read line cat “$FILENAME” |


⚫ do
while read line
⚫ echo $line do
⚫ done < “$FILENAME” echo $line
⚫ done
Arrays

⚫ Declaration
⚫ ARRAY=(value1 value2 … valueN)

Arrays

⚫ Declaration
⚫ ARRAY=(value1 value2 … valueN)
⚫ ARRAY=(one, two, three)

⚫ Calling
⚫ ${ARRAY[0]} #one
⚫ ${ARRAY[1]} #two
⚫ ${ARRAY[2]} #three

Arrays

⚫ ARRAYS
⚫ ARRAY=(one,two,three)
⚫ ${ARRAY[0]} #one
⚫ ${ARRAY[1]} #two
⚫ ${ARRAY[2]} #three
⚫ ${ARRAY[@]} #all items in array
⚫ ${ARRAY[*]} #all items in array, delimited by first character of IFS
⚫ ${!ARRAY[@]} #all indexes in the array (@/*)
⚫ ${#ARRAY[@]} #number of items in the array (@/*)
⚫ ${ARRAY[0]} # length of item zero

Functions

⚫ Lines of code which can be called multiple time within a script




Functions

⚫ Functions
⚫ function_name() {
⚫ <command>
⚫}

⚫ Function function_name{
⚫ <command>
⚫}


Functions

⚫ Functions

⚫ function hello {
⚫ echo hello
⚫}
⚫ echo “output from function hello: ”
⚫ hello


Functions

⚫ Functions - arguments

⚫ function hello {
⚫ echo hello $1
⚫}
⚫ echo “output from function hello: ”
⚫ hello Richard


Functions

⚫ Functions – return values

⚫ function hello {
⚫ echo hello $1
⚫ return 11
⚫ }
⚫ echo “output from function hello: ”
⚫ hello Richard
⚫ echo “return value from function hello is : $?”


Functions

⚫ Functions – variables

⚫ function hello { function hello {


⚫ name=$1 local name=$1
⚫ echo “hello $name” echo “hello $name”
⚫ } }

⚫ echo “enter your name: ” echo “enter your name: ”


⚫ read name #Richard read name #Richard
⚫ hello Peter #hello Peter hello Peter #hello Peter
⚫ echo $name #Peter echo $name #Richard
Awk

⚫ Search files for lines that contain certain patterns


⚫ Perform operation described in AWK body on line with
certain pattern
⚫ Perform operation described in AWK body on choosen line
Awk – Basic structure

⚫ awk ‘program_you_will_write’ input-file1 input-file2


Awk – Basic structure

⚫ awk ‘program_you_will_write’ input-file1 input-file2

⚫ Awk ‘BEGIN{
⚫ code_in_BEGIN_section
⚫ }
⚫ {code_in_body(main)_section}
⚫ END{
⚫ code_in_END_section
⚫ }’ input-file1 input-file2
Awk – Basic structure

⚫ awk ‘program_you_will_write’ input-file1 input-file2

⚫ echo “one two three four” | awk


‘BEGIN{code_in_BEGIN_section}{code_in_body_section}
END{code_in_END_section}’
Awk – Basic structure

⚫ awk ‘program_you_will_write’ input-file1 input-file2

⚫ echo “one two three four” | awk


‘BEGIN{code_in_BEGIN_section}{code_in_body_section}
END{code_in_END_section}’
⚫ echo “one two three four” | awk ‘{code_in_body_section}’
Awk – Basic structure

⚫ In AWK body bash feature doesn’t work


Awk – Basic structure

⚫ In AWK body bash feature doesn’t work


⚫ Completely new language in AWK body
⚫ Print
⚫ If-else
⚫ For loop
⚫ ….

Awk – Basic structure
⚫ In AWK body bash feature doesn’t work
⚫ Completely new language in AWK body
⚫ Print
⚫ If-else
⚫ For loop
⚫ ….
⚫ Completely new feature we can call on specific line(specific text)
⚫ NR (Number of fields)
⚫ NR (Number of records)
⚫ FS (Fields separator)
⚫ RS (record separator)
⚫ $1, $2 …. (specific fields)
Awk – Variables

⚫ Assignement
⚫ A=1
⚫ RS=”/n”
⚫ FS=”:”
Awk – Variables

⚫ Mathematical operations
⚫ Increment / Decrement
⚫ a++ (a—)
⚫ a=a+1 (a=a-1)
⚫ Classical math operations
⚫ a=b+c
⚫ a=b*c
⚫ a=b/c
⚫ a=b-c
Awk – Variables

⚫ Mathematical operations
⚫ var += increment // add increment to the value of var
⚫ var -= decrement // subtract decrement from the value of var.
⚫ var *= coefficient // Multiply the value of var by coefficient.
⚫ Var /= divisor // Divide the value of var by divisor
⚫ var %= modulus // set var to its remainder by modulus
⚫ var ^= power-number // Raise var to the power power-number.
⚫ Var **= power-number // Raise var to the power power-number
Awk – If statement

⚫ If (condition) {
⚫ command(s)
⚫ }
⚫ else {
⚫ command(s)
⚫ }
Awk – If statement

⚫ Comparison
⚫ var == 5 var == “hello”
⚫ var > 5, var >=5
⚫ var < 5, var =<5
⚫ var != 5 var != “hello”
Awk – For loop

⚫ for (intitalization; condition; increment) {


⚫ command(s)
⚫ }
Awk – For loop

⚫ for (intitalization; condition; increment) {


⚫ command(s)
⚫ }
⚫ awk ‘BEGIN{for i=1; i<=10; i++) {print “hello”, i}}’
Sed

⚫ sed OPTION … [SCRIPT] [INPUTFILE]


⚫ cat [INPUTFILE] | sed OPTION … [SCRIPT]

⚫ SCRIPT:
⚫ [addr]X[option]
⚫ X is single-letter sed command
⚫ [addr] can be single line number, a regular expression, or a range of lines. If
[addr] is specified, the command X will be executed only on the matched
lines.
⚫ Additional [options] are used for some sed commands
Sed

⚫ sed OPTION … [SCRIPT] [INPUTFILE]


⚫ cat [INPUTFILE] | sed OPTION … [SCRIPT]

⚫ SCRIPT:
⚫ [addr]X[option]
⚫ X is single-letter sed command
⚫ [addr] can be single line number, a regular expression, or a range of lines. If
[addr] is specified, the command X will be executed only on the matched
lines.
⚫ Additional [options] are used for some sed commands
Sed

⚫ sed OPTION … [SCRIPT] [INPUTFILE]


⚫ cat [INPUTFILE] | sed OPTION … [SCRIPT]

⚫ SCRIPT:
⚫ [addr]X[option]
⚫ X is single-letter sed command
⚫ [addr] can be single line number, a regular expression, or a range of lines. If
[addr] is specified, the command X will be executed only on the matched
lines.
⚫ Additional [options] are used for some sed commands
Sed

⚫ sed OPTIONS ...[SCRIPT] [INPUTFILE] ⚫ sed ‘30,35d’ input.txt


⚫ cat [INPUTFILE] | sed OPTIONS … > output.txt
[SCRIPT]
⚫ The following
example deletes
SCRIPT: lines 30 to 35 in the
[addr]X[option] input
X is single-letter sed command ⚫ 30,35 is an address
range
[addr] can be single line number, a regular
expression, or a range of lines. If [addr] is ⚫ d is the delete
specified, the command X will be executed only command
on the matched lines.
Additional [options] are used for some sed
Sed

⚫ sed OPTIONS ...[SCRIPT] [INPUTFILE] ⚫ sed ‘30,35d’ input.txt


⚫ cat [INPUTFILE] | sed OPTIONS … > output.txt
[SCRIPT]
⚫ The following
example deletes
SCRIPT: lines 30 to 35 in the
[addr]X[option] input
X is single-letter sed command ⚫ 30,35 is an address
range
[addr] can be single line number, a regular
expression, or a range of lines. If [addr] is ⚫ d is the delete
specified, the command X will be executed only command
on the matched lines.
Additional [options] are used for some sed
Sed

⚫ sed OPTIONS ...[SCRIPT] [INPUTFILE] ⚫ sed ‘30,35d’ input.txt


⚫ cat [INPUTFILE] | sed OPTIONS … > output.txt
[SCRIPT]
⚫ The following
example deletes
SCRIPT: lines 30 to 35 in the
[addr]X[option] input
X is single-letter sed command ⚫ 30,35 is an address
range
[addr] can be single line number, a regular
expression, or a range of lines. If [addr] is ⚫ d is the delete
specified, the command X will be executed only command
on the matched lines.
Additional [options] are used for some sed
Sed

⚫ SED commands:
⚫ a text append text after a line
⚫ d delete the pattern space
⚫ i text insert text before a line
⚫ p print the pattern space
⚫ q[exit-code] (quit) Exit sed without processing any more commands or input
⚫ s/regexp/replacement/[flags]
⚫ (substitute) Match the regular-expression againts the content of the
pattern space. If found, replace mattched string with replacement.

Sed

⚫ SED
⚫ Command-Line-Options
⚫ -n disable automatic printing; sed only produce output when
explicitly told to via the p command.
⚫ -e sctipt add script
⚫ -r use extended regular expressions rather than basic regular
expressions.
⚫ ….
Sed

⚫ SED
⚫ Command-Line-Options
⚫ -n disable automatic printing; sed only produce output when explicitly told to
via the p command.
⚫ -e sctipt add script
⚫ -r use extended regular expressions rather than basic regular expressions.

⚫ ...
Sed - substitute

⚫ s/regexp/replacement/[flags]
⚫ Search for string that match regular-expression.If found, matched string
with replacement
⚫ Example
⚫ Echo “one five three” | sed ‘s/five/two’ # one two three
⚫ Echo “Hana is 50 years old” | sed ‘s/[[:digit:]]\+/***/’ # Hana is *** years
old
⚫ G flag – globally
⚫ Echo “0 2 9 0 5” | sed ‘s/0/1/’ # 1 2 9 0 5
⚫ Echo “0 2 9 0 5” | sed ‘/0/1/g’ # 1 2 9 1 5
For Loop

You might also like