SlideShare a Scribd company logo
CISC3130, Spring 2013
Dr. Zhang
1
Bash Scripting: Advanced Topics
Outline
2
 Review HW1, HW2 and Quiz2
 Review of standard input/output/error
 How to redirect them ?
 Pipeline
 Review of bash scripting
 Functions
 Here documents
 Arrays
Homework 2
 match phone numbers in text file
 7188174484, 718-817-4484, (718)817,4484
 817-4484, or 817,4484, 8174484.
 (01)718,817,4484, 01,718-817-4484
 grep -f phone.grep file.txt , where phone.grep:
[^0-9][0-9]{10}$
[^0-9][0-9]{10}[^0-9]
[^0-9][0-9]{3}-[0-9]{3}-[0-9]{4}$
[^0-9][0-9]{3}-[0-9]{3}-[0-9]{4}[^0-9]
[^0-9][0-9]{3},[0-9]{4}$
[^0-9][0-9]{3},[0-9]{4}[^0-9]
[^0-9][0-9]{3}-[0-9]{4}$
[^0-9][0-9]{3}-[0-9]{4}[^0-9]
3
Match 10 digits at end of line
Match 10 digits, and a non-digit char
718-817,4484 at end of line
Homework 2
[^0-9]*([0-9]{2})([0-9]{3})[0-9]{3},[0-9]{4}$
[^0-9]*([0-9]{2})([0-9]{3})[0-9]{3},[0-9]{4}[^0-9]
[^0-9]*([0-9]{2})([0-9]{3}))?[0-9]{3}-[0-9]{4}$
[^0-9]*([0-9]{2})([0-9]{3}))?[0-9]{3}-[0-9]{4}[^0-9]
[^0-9]*[0-9]{2},[0-9]{3},[0-9]{3},[0-9]{4}$
[^0-9]*[0-9]{2},[0-9]{3}-[0-9]{3}-[0-9]{4}[^0-9]
[^0-9]*[0-9]{2},[0-9]{3}-[0-9]{3}-[0-9]{4}$
[^0-9]*[0-9]{2},[0-9]{3},[0-9]{3},[0-9]{4}[^0-9]
4
Homework 2
 Write a sed script file that remove all one-line comments from
C/C++ source code files. Note that such comments starting with
//, and ends at the end of line. You need to take care the cases
where // appears in double quote, or single quote, in thsse cases,
what comes after // is not comment.
 rmcnt.sed :
#!/bin/sed -f
## remove one-line comments from C/C++ code
/^[^'"]*/// s///.*$/ /g
 rmcnt.sed sample.cpp
5
Apply to lines that contain // not preceding by ' or "
Replace // and following chars
with space
Quiz 2
 How to write to standard output in shell script:
#!/bin/bash
echo "Hello world";
echo "Something is wrong" 1>& 2
ls ABCDEF 2>&1
 Try it out:
 ./test_redirect.sh 2> err_out ## what happens?
 ./test_redirect.sh > std_out ## what happens?
 ./test_redirect.sh > std_out 2>&1
6
Quiz2
 Mark constants with < and >
#/bin/bash
# First find numerica constants in the code
#grep -E '[^a-zA-Z_][0-9]+.?[0-9]+' $1
# now mark constants with <>
echo mark constants in file $1
sed 's/([^a-zA-Z0-9_])([0-9][0-9]*.{0,1}[0-9][0-9]*)/1<2>/g' $1
7
The char before constant:
not alphabet, not _, and not
digit
A numeric constant:
Optional decimal points: .{0,1}
cannot use ?, as sed use BRE
Standard input/output/error
8
 By default, link to keyboard and terminal window respectively
 Can be redirected to files
 Can be redirected to pipeline
 input can be redirected to reading end of a pipe
 output and error can be redirected to writing end of a pipe
 When a bash script’s input/output/error is redirected:
 E.g., headtail 3 10 .bash_profile > output
 ls –l | headtail 10 24 | wc –l
 input/output/error for every command in the script are
redirected !
Save standard input if necessary
9
#!/bin/bash
# Count # of lines, and search for phone in a file; if a file is
# not specified, process standard input
set -x ## turn on execution tracing
if [ $# -eq 0 ]
then
cat > stdinput ## save standard input to a file
set stdinput
fi
## so that we can use as many times as we want
wc –l $*
grep -f phone.grep $*
rm stdinput
exit 0
Or use –x in
first line, i.e., #!/bin/bash –x
Or type
$ bash -x countlines_searchphoneno.sh
to run the scripts
Code at:
countlines_searchphoneno.sh
Redirection can be applied to loop
10
rm all_shellscripts
for i in `ls *.sh`
do
echo $i >>all_shellscripts
cat $i >>all_shellscripts
done
for i in `ls *.sh`
do
echo $i
cat $i
done > all_shellscripts
Similar for <, |
case construct: branching
 case construct is analogus to switch in C/C++.
case "$variable" in
shellpattern1 )
command...
;;
shellpattern2)
command …
;;
shell pattern n)
command...
;;
esac
11
• Quoting variables is not mandatory
• Each pattern can contain shell wildcard
(*,?,[a-z]), ends with a )
• Each condition block ends with ;;
• If a condition tests true, then associated
commands execute and the case block
terminates.
• entire case block ends with an esac
Calculator using case block
case "$op" in
"+" ) result=$(($x + $y))
echo $x $op $y = $result;;
"-" ) result=$(($x - $y))
echo $x $op $y = $result;;
"*" ) result=$(($x * $y))
echo $x * $y = $result;;
"/" ) result=$(($x / $y))
echo $x $op $y = $result;;
* ) echo Unknow operator $op;;
esac
12
#!/bin/bash
OPT=$1 # option
FILE=$2 # filename
# test -e and -E command line args matching
case $OPT in
-e|-E)
echo "Editing $2 file..."
# make sure filename is passed else an error displayed
[ -z $FILE ] && { echo "File name missing"; exit 1; } || vi $FILE ;;
-c|-C)
echo "Displaying $2 file...“
[ -z $FILE ] && { echo "File name missing"; exit 1; } || cat $FILE ;;
-d|-D)
echo "Today is $(date)" ;;
*)
echo "Bad argument!"
echo "Usage: $0 -ecd filename"
echo " -e file : Edit file."
echo " -c file : Display file."
echo " -d : Display current date and time." ;;
esac
13
test if string is null
Lazy evaluation of && and ||
Case example
case $1 in
-f)
## case for –f option
;;
-d | --directory)
## -f or –directory option
;;
*)
echo $1: unknown option >&2
exit 1;
esac
14
More about bash loop structures
15
Infinite loop
16
while [ 1 ]
do
echo -n "Enter your password"
read input
if [ $input = "secret" ]
then
break ## break out of the loop
else
echo -n "Try again... "
fi
done
continue command
 Continue from the top of the for loop
 Ignore rest of commands in the loop, and continue the loop from the top again (for the
next value in the list)
i=1
for day in Mon Tue Wed Thu Fri Sat Sun
do
echo -n "Day $((i++)) : $day"
if [ $i -eq 7 -o $i -eq 8 ];
then
echo " (WEEKEND)"
continue;
fi
echo " (weekday)"
done
17
For loop without a list
#!/bin/bash
for i
do
echo hello $i
done
18
For loop
i=1
for username in `awk -F: '{print $1}' /etc/passwd`
do
echo "Username $((i++)) : $username"
done
19
Loop through files/directories
 loop through files and directories under a specific directory
i=1
cd ~
for item in *
do
echo "Item $((i++)) : $item"
done
20
C-Style for loop
for (( EXP1; EXP2; EXP3 ))
do
Command1
…
Commandn
done
 Example:
#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo "Welcome $c times“
done
21
EXP1: initializer
EXP2: a loop-test or condition
EXP3: counting expression
Select loop
 select construct: allows easy menu generation
select WORD [in LIST]
do
RESPECTIVE-COMMANDS;
done
1. List of items printed to standard error, each item preceded by a number.
 If in LIST is not present, positional parameters (command line arguments) are used
2. A prompt is printed, one line from standard input is read.
1.If input is a number corresponding to one of items, value of WORD is set to name of that item.
2.If line is empty, items and the PS3 prompt are displayed again.
3. If an EOF (End Of File) is read, loop exits.
3. RESPECTIVE-COMMANDS are executed after each selection
4. Go back to 1
22
select construct: example
23
#!/bin/bash
OPTIONS="Hello Quit“
select opt in $OPTIONS; do
if [ "$opt" = "Quit" ]
then
echo done
exit
elif [ "$opt" = "Hello" ]
then
echo Hello World
else
echo bad option
fi
done
~zhang/public_html/cs3130/Codes/select_ex
Next:
24
 More advanced bash scripting
 Array
 Function
 Inline input, or here document
Array
 Bash provides one-dimensional array variables
 Assign values to array:
array=( one two three )
files=( "/etc/passwd" "/etc/group" "/etc/hosts" )
limits=( 10 20 26 39 48)
 Access array element : ${array_name[index]}
 indexed using integers and are zero-based.
${array[1]}
 To access all items in arary: ${array_name[*]}, ${array_name[@]}
 To access array length: len=${#x[@]}
25
To Iterate Through Array Values
26
#!/bin/bash
# declare an array called array and define 3 vales
array=( one two three )
for i in "${array[@]}"
do
echo $i
done
Exercise/Example
 Write a script that read a sequence of numbers and save
them in an array, print out the array content and size.
 Usage: EchoNumber [file]
 If no file is specified, read from standard input
 Example script:
 LargestSmallest.sh
27
#!/bin/bash
i=0
if [ $# -eq 0 ]
then
echo "Enter the numbers, Ctrl-D to end";
cat > stdinput
set stdinput
fi
while read num
do
a[$i]=$num
i=$((i+1))
done < $1
echo Array is ${a[*]}, with ${#a[*]} numbers
28
Bash function
 Functions: to increase modularity and readability
 More efficient than breaking scripts into many smaller ones
 Syntax to define a function:
function functionname()
{
commands . .
}
 function is a keyword which is optional.
 functionname is the name of the function
 No need to specify argument in ( )
 commands – List of commands to be executed I
 exit status of the function is exit status of last command executed in the function
body.
29
Function call
 Call bash function from command line or script
 $ functionname arg1 arg2
 When shell interprets a command line, it first looks into the
special built-in functions like break, continue, eval, exec etc.,
then it looks for shell functions.
 function defined in a shell start up file (e.g.,.bash_profile ).
 available for you from command line every time you log on
30
About functions
31
 Parameter passing: $1, $2, …
 Result returning
 Use echo command
 Through setting a variable
 return command: to return an exit status
#! /bin/bash
calsum(){
echo `expr $1 + $2`
}
x=1;y=2;
sum=`calsum $x $y`
calsum(){
sum=`expr $1 + $2`
}
x=1;y=2;
calsum $x $y
echo z=$sum
About functions
32
 Local variable: its scope is within the function
#! /bin/bash
calsumsqr(){
local sum=`expr $1 + $2`;
echo `expr $sum * $sum`
}
x=1;y=2;
z=`calsum $x $y`
Exercise/Example
 Write a function that check whether a user is log on or not
(CheckUser.sh)
function UserOnline()
{
if who | grep $1 ## UserOnline takes a parameter
then
return 0 ## 0 indicates success
else
return 1 ##1 for failure, i.e., offline
fi
}
if UserOnline $1 ## function’s return value as condition/test
then
echo User $1 is online
else
echo User $1 is offline
fi
33
Here document (inline document)
34
 A special way to pass standard input to a command: here
document, i.e., from shell script itself
 Benefits: store codes and data together, easier to maintain
#!/bin/bash
cat <<!FUNKY!
Hello
This is a here
Document
!FUNKY!
Here document starts with <<,
followed by a special string which is
repeated at the end of the document.
Note: the special string should be
chosen to be rare one.
Here document:2
35
 Example: 411 script that looks up a phone book
 Usage example: 411 joke
#!/bin/bash
grep “$*” << End
Dial-a-joke 212-976-3838
Dial-a-prayer 212-246-4200
Dial santa 212-976-141
End
Phone #
searching script
#!/bin/bash
cat > phone.pattern << PATTERNS
[^0-9][0-9]{10}$
[^0-9][0-9]{10}[^0-9]
[^0-9][0-9]{3}-[0-9]{3}-[0-9]{4}$
[^0-9][0-9]{3}-[0-9]{3}-[0-9]{4}[^0-9]
[^0-9][0-9]{3},[0-9]{4}$
PATTERNS
grep -f phone.pattern $*
rm phone.pattern ##no need to keep this file …
36
A case study:
bundle program
37
 Suppose a friend asks for copies of shell files in your bin
directory
$ cd ~/bin
$ for i in *.sh
> do
> echo ===== This is file $i =============
> cat $i
> done | mail yourfriend@hotmail.com
Pipeline & input/output redirection can be applied to for, while, until loop.
Make it better ?
38
 Construct a mail message that could automatically unpack
itself, i.e., to generate the original files packed inside
 E.g., A shell script contains instructions for unpacking, and the
files content themselves
 Use here document mechanism
A bash script contains two files
39
#To unbundle, bash this file
echo file1 1>&2
cat >file1 <<'End of file1'
A
B
C
End of file1
echo file2 1>&2
cat >file2 <<'End of file2'
1
2
3
end of file2
What does this script do?
How to create such bundle file automatically?
Use a script, bundle.sh file1 file2 file3 …
Bundle script
40
#!/bin/bash
## write a shell script that contains files specified in arguments
echo '#!/bin/bash'
echo ‘# To unbundle, bash this file’
for i ## without a list of items, loop through ##
command line arguments
do
echo “echo $i 1>&2”
echo “cat >$i <<‘End of $i’”
cat $i
echo “End of $i”
done
codes/examples/bundle.sh
For homework 3, use bundle.sh to generate a hw3_bundle file, and submit it.
Summary
 Review of shell scripting
 Examples
 Array, function, inline document
41
Outline
42
 Coding standard: how to get good grades in lab assignment
 Review of standard input/output/error
 How to redirect them ?
 Pipeline
 Review of bash scripting
Bash scripting: general hints
43
 Use echo command to trace (like cout, printf in C/C++)
 Sometimes there are alternatives ways to do things, choose
one and remember it:
 $(( …)), and $[ … ]
 [[ ]] for test
 Be careful about typo, shell wont complain variable not
declared/assigned …
 The price of freedom
 A walk-through of basic bash scripting
Bash Scripting
44
 Variables
 Environment variable: affect behavior of shell
 User defined variable: default type is string, can declare it to be
other type
 Positional variables: used to pass command line arguments to bash
script
 Variable assignment:
x=10 ## assign value 10 to variable x, no space around =
x=$x+1 ## add 1 to x’s value and assign to x
PATH=$PATH:.:~/bin
 To refer to a variable’s value, precede variable name with $
A script that display positional variable
45
echo All arguments: $*
echo Number of arguments: $#
echo Script name: $0
echo argument 1: $1
echo argument 2: $2
for arg in $*
do
echo argument $arg
done
arithmetic operation
46
 As variable’s default type is string, to perform arithmetic
operation, use the following syntax
$[$x+1] or $(($x+1))
 For simpler syntax: declare variable to be numerical
declare –i x
x=$x*10+2
 Above are for integer arithmetic operations only ..
Command bc
47
 An arbitrary precision calculator
$ bc
3.14159*10^2
314.15900
130^2
16900
sqrt(1000)
31
scale=4
sqrt(1000)
31.6277
quit
An interactive calculator:
* user input shown in normal font,
* result shown in italics font
Internal variable scale:
* control the number of decimal points after decimal
point
bc in command line/script
48
 To evaluate an expression, simply send it using pipe to bc
echo "56.8 + 77.7" | bc
 Write a script that read a Fahrenheit degree from standard
input, convert it to Celsius degree (up to 2 digits after
decimal point):
C=(F-32)*5/9
 Base conversion, from base 10 (decimal) to base 16
(hexadecimal)
echo "obase=16; ibase=10; 56" | bc
Test/Conditions
49
 Any command or script, if it return 0, then test is successful
if rm tmp.txt
then
echo file tmp.txt has been deleted
else
echo fail to remove file tmp.txt
Fi
 Use ! to negate
Test Condition
50
 Numerical comparisons
 -lt, -ne, -ge, …
 String comparision
 Pattern matching: using double brackets
 To test if first argument is “–” followed by a number:
if [[ "$1" == -[0-9]* ]]
then
….

More Related Content

Similar to ShellAdvanced shell scripting programm.ppt (20)

Operating_System_Lab_ClassOperating_System_2.pdf
Operating_System_Lab_ClassOperating_System_2.pdfOperating_System_Lab_ClassOperating_System_2.pdf
Operating_System_Lab_ClassOperating_System_2.pdf
DharmatejMallampati
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt
poyotero
 
Chapter 2: Introduction to Bash Scripting
Chapter 2: Introduction to Bash ScriptingChapter 2: Introduction to Bash Scripting
Chapter 2: Introduction to Bash Scripting
azzamhadeel89
 
KT on Bash Script.pptx
KT on Bash Script.pptxKT on Bash Script.pptx
KT on Bash Script.pptx
gogulasivannarayana
 
Bash and regular expressions
Bash and regular expressionsBash and regular expressions
Bash and regular expressions
plarsen67
 
ShellProgramming and Script in operating system
ShellProgramming and Script in operating systemShellProgramming and Script in operating system
ShellProgramming and Script in operating system
vinitasharma749430
 
AOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptxAOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptx
rapiwip803
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
Gaurav Shinde
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
Mufaddal Haidermota
 
Shell programming
Shell programmingShell programming
Shell programming
Moayad Moawiah
 
Bash Scripting Workshop
Bash Scripting WorkshopBash Scripting Workshop
Bash Scripting Workshop
Ahmed Magdy Ezzeldin, MSc.
 
Bash production guide
Bash production guideBash production guide
Bash production guide
Adrien Mahieux
 
Bash and regular expressions
Bash and regular expressionsBash and regular expressions
Bash and regular expressions
plarsen67
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
Sasidhar Kothuru
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
Abhay Sapru
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Shell Programming Language in Operating System .pptx
Shell Programming Language in Operating System .pptxShell Programming Language in Operating System .pptx
Shell Programming Language in Operating System .pptx
SherinRappai
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
Brahma Killampalli
 
Operating_System_Lab_ClassOperating_System_2.pdf
Operating_System_Lab_ClassOperating_System_2.pdfOperating_System_Lab_ClassOperating_System_2.pdf
Operating_System_Lab_ClassOperating_System_2.pdf
DharmatejMallampati
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt
poyotero
 
Chapter 2: Introduction to Bash Scripting
Chapter 2: Introduction to Bash ScriptingChapter 2: Introduction to Bash Scripting
Chapter 2: Introduction to Bash Scripting
azzamhadeel89
 
Bash and regular expressions
Bash and regular expressionsBash and regular expressions
Bash and regular expressions
plarsen67
 
ShellProgramming and Script in operating system
ShellProgramming and Script in operating systemShellProgramming and Script in operating system
ShellProgramming and Script in operating system
vinitasharma749430
 
AOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptxAOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptx
rapiwip803
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
Bash and regular expressions
Bash and regular expressionsBash and regular expressions
Bash and regular expressions
plarsen67
 
Unix shell scripting basics
Unix shell scripting basicsUnix shell scripting basics
Unix shell scripting basics
Abhay Sapru
 
Unix Shell Scripting Basics
Unix Shell Scripting BasicsUnix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Shell Programming Language in Operating System .pptx
Shell Programming Language in Operating System .pptxShell Programming Language in Operating System .pptx
Shell Programming Language in Operating System .pptx
SherinRappai
 

More from ubaidullah75790 (20)

Chapter20 transaction processing system .pptx
Chapter20 transaction processing system .pptxChapter20 transaction processing system .pptx
Chapter20 transaction processing system .pptx
ubaidullah75790
 
Chapter22 database security in dbms.pptx
Chapter22 database security in dbms.pptxChapter22 database security in dbms.pptx
Chapter22 database security in dbms.pptx
ubaidullah75790
 
Chapter27 distributed database syst.pptx
Chapter27 distributed database syst.pptxChapter27 distributed database syst.pptx
Chapter27 distributed database syst.pptx
ubaidullah75790
 
File Organization in database management.pptx
File Organization in database management.pptxFile Organization in database management.pptx
File Organization in database management.pptx
ubaidullah75790
 
transaction processing databse management.pptx
transaction processing databse management.pptxtransaction processing databse management.pptx
transaction processing databse management.pptx
ubaidullah75790
 
physical database design distributed .ppt
physical database design distributed .pptphysical database design distributed .ppt
physical database design distributed .ppt
ubaidullah75790
 
module03-ipaddr ipv6 addressing in net.ppt
module03-ipaddr ipv6 addressing in net.pptmodule03-ipaddr ipv6 addressing in net.ppt
module03-ipaddr ipv6 addressing in net.ppt
ubaidullah75790
 
PDBD- Part2 physical database design.ppt
PDBD- Part2 physical database design.pptPDBD- Part2 physical database design.ppt
PDBD- Part2 physical database design.ppt
ubaidullah75790
 
Physical_Design system development life.PPT
Physical_Design system development life.PPTPhysical_Design system development life.PPT
Physical_Design system development life.PPT
ubaidullah75790
 
S3 application and network attacks in.ppt
S3 application and network attacks in.pptS3 application and network attacks in.ppt
S3 application and network attacks in.ppt
ubaidullah75790
 
Chapter 5 cyber security in computer.ppt
Chapter 5 cyber security in computer.pptChapter 5 cyber security in computer.ppt
Chapter 5 cyber security in computer.ppt
ubaidullah75790
 
1606802425-dba-w7 database management.pptx
1606802425-dba-w7 database management.pptx1606802425-dba-w7 database management.pptx
1606802425-dba-w7 database management.pptx
ubaidullah75790
 
ENCh18 database management system ss.ppt
ENCh18 database management system ss.pptENCh18 database management system ss.ppt
ENCh18 database management system ss.ppt
ubaidullah75790
 
Chapter07 database system in computer.ppt
Chapter07 database system in computer.pptChapter07 database system in computer.ppt
Chapter07 database system in computer.ppt
ubaidullah75790
 
Chapter05 database sytem in computer . ppt
Chapter05 database sytem in computer . pptChapter05 database sytem in computer . ppt
Chapter05 database sytem in computer . ppt
ubaidullah75790
 
Chapter04 database system in computer.ppt
Chapter04 database system in computer.pptChapter04 database system in computer.ppt
Chapter04 database system in computer.ppt
ubaidullah75790
 
Chapter03 database system in computer.ppt
Chapter03 database system in computer.pptChapter03 database system in computer.ppt
Chapter03 database system in computer.ppt
ubaidullah75790
 
Chapter02 database system in computer.ppt
Chapter02 database system in computer.pptChapter02 database system in computer.ppt
Chapter02 database system in computer.ppt
ubaidullah75790
 
Chapter01 database system in computer.ppt
Chapter01 database system in computer.pptChapter01 database system in computer.ppt
Chapter01 database system in computer.ppt
ubaidullah75790
 
MYCH8 database management system in .ppt
MYCH8 database management system in .pptMYCH8 database management system in .ppt
MYCH8 database management system in .ppt
ubaidullah75790
 
Chapter20 transaction processing system .pptx
Chapter20 transaction processing system .pptxChapter20 transaction processing system .pptx
Chapter20 transaction processing system .pptx
ubaidullah75790
 
Chapter22 database security in dbms.pptx
Chapter22 database security in dbms.pptxChapter22 database security in dbms.pptx
Chapter22 database security in dbms.pptx
ubaidullah75790
 
Chapter27 distributed database syst.pptx
Chapter27 distributed database syst.pptxChapter27 distributed database syst.pptx
Chapter27 distributed database syst.pptx
ubaidullah75790
 
File Organization in database management.pptx
File Organization in database management.pptxFile Organization in database management.pptx
File Organization in database management.pptx
ubaidullah75790
 
transaction processing databse management.pptx
transaction processing databse management.pptxtransaction processing databse management.pptx
transaction processing databse management.pptx
ubaidullah75790
 
physical database design distributed .ppt
physical database design distributed .pptphysical database design distributed .ppt
physical database design distributed .ppt
ubaidullah75790
 
module03-ipaddr ipv6 addressing in net.ppt
module03-ipaddr ipv6 addressing in net.pptmodule03-ipaddr ipv6 addressing in net.ppt
module03-ipaddr ipv6 addressing in net.ppt
ubaidullah75790
 
PDBD- Part2 physical database design.ppt
PDBD- Part2 physical database design.pptPDBD- Part2 physical database design.ppt
PDBD- Part2 physical database design.ppt
ubaidullah75790
 
Physical_Design system development life.PPT
Physical_Design system development life.PPTPhysical_Design system development life.PPT
Physical_Design system development life.PPT
ubaidullah75790
 
S3 application and network attacks in.ppt
S3 application and network attacks in.pptS3 application and network attacks in.ppt
S3 application and network attacks in.ppt
ubaidullah75790
 
Chapter 5 cyber security in computer.ppt
Chapter 5 cyber security in computer.pptChapter 5 cyber security in computer.ppt
Chapter 5 cyber security in computer.ppt
ubaidullah75790
 
1606802425-dba-w7 database management.pptx
1606802425-dba-w7 database management.pptx1606802425-dba-w7 database management.pptx
1606802425-dba-w7 database management.pptx
ubaidullah75790
 
ENCh18 database management system ss.ppt
ENCh18 database management system ss.pptENCh18 database management system ss.ppt
ENCh18 database management system ss.ppt
ubaidullah75790
 
Chapter07 database system in computer.ppt
Chapter07 database system in computer.pptChapter07 database system in computer.ppt
Chapter07 database system in computer.ppt
ubaidullah75790
 
Chapter05 database sytem in computer . ppt
Chapter05 database sytem in computer . pptChapter05 database sytem in computer . ppt
Chapter05 database sytem in computer . ppt
ubaidullah75790
 
Chapter04 database system in computer.ppt
Chapter04 database system in computer.pptChapter04 database system in computer.ppt
Chapter04 database system in computer.ppt
ubaidullah75790
 
Chapter03 database system in computer.ppt
Chapter03 database system in computer.pptChapter03 database system in computer.ppt
Chapter03 database system in computer.ppt
ubaidullah75790
 
Chapter02 database system in computer.ppt
Chapter02 database system in computer.pptChapter02 database system in computer.ppt
Chapter02 database system in computer.ppt
ubaidullah75790
 
Chapter01 database system in computer.ppt
Chapter01 database system in computer.pptChapter01 database system in computer.ppt
Chapter01 database system in computer.ppt
ubaidullah75790
 
MYCH8 database management system in .ppt
MYCH8 database management system in .pptMYCH8 database management system in .ppt
MYCH8 database management system in .ppt
ubaidullah75790
 
Ad

Recently uploaded (20)

最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
Taqyea
 
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptxInter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
secretarysocom
 
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animationUV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
17218
 
LpQuantueer rtwrt 1e erere errerqer m.ppt
LpQuantueer rtwrt 1e erere errerqer m.pptLpQuantueer rtwrt 1e erere errerqer m.ppt
LpQuantueer rtwrt 1e erere errerqer m.ppt
cyberesearchprof
 
Unlocking Business Growth Through Targeted Social Engagement
Unlocking Business Growth Through Targeted Social EngagementUnlocking Business Growth Through Targeted Social Engagement
Unlocking Business Growth Through Targeted Social Engagement
Digital Guider
 
Internet & Protocols : A Blueprint of the Internet System
Internet & Protocols : A Blueprint of the Internet SystemInternet & Protocols : A Blueprint of the Internet System
Internet & Protocols : A Blueprint of the Internet System
cpnabil59
 
PPT 18.03.2023.pptx for i smart programme
PPT 18.03.2023.pptx for i smart programmePPT 18.03.2023.pptx for i smart programme
PPT 18.03.2023.pptx for i smart programme
AbhimanShastry
 
SAP_S4HANA_ChatGPT_Integration_Presentation.pptx
SAP_S4HANA_ChatGPT_Integration_Presentation.pptxSAP_S4HANA_ChatGPT_Integration_Presentation.pptx
SAP_S4HANA_ChatGPT_Integration_Presentation.pptx
vemulavenu484
 
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
Taqyea
 
05-introduction-to-operating-systems.pptx
05-introduction-to-operating-systems.pptx05-introduction-to-operating-systems.pptx
05-introduction-to-operating-systems.pptx
pepecompany1
 
AWS Basics for non technical people.pptx
AWS Basics for non technical people.pptxAWS Basics for non technical people.pptx
AWS Basics for non technical people.pptx
sanjeevkumar123577
 
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdfCommon Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
CartCoders
 
simple-presentationtestingdocument2007.pptx
simple-presentationtestingdocument2007.pptxsimple-presentationtestingdocument2007.pptx
simple-presentationtestingdocument2007.pptx
ashokjayapal
 
In order to install and use the device software, your computer must meet the ...
In order to install and use the device software, your computer must meet the ...In order to install and use the device software, your computer must meet the ...
In order to install and use the device software, your computer must meet the ...
raguclc
 
COMPUTER ETHICS AND CRIME.......................................................
COMPUTER ETHICS AND CRIME.......................................................COMPUTER ETHICS AND CRIME.......................................................
COMPUTER ETHICS AND CRIME.......................................................
FOOLKUMARI
 
Quantiuwewe e3er14e we3223 32222 m2.pptx
Quantiuwewe e3er14e we3223 32222 m2.pptxQuantiuwewe e3er14e we3223 32222 m2.pptx
Quantiuwewe e3er14e we3223 32222 m2.pptx
cyberesearchprof
 
Lecture 3.1 Analysing the Global Business Environment .pptx
Lecture 3.1 Analysing the Global Business Environment .pptxLecture 3.1 Analysing the Global Business Environment .pptx
Lecture 3.1 Analysing the Global Business Environment .pptx
shofalbsb
 
SAP_S4HANA_eCommerce_Integration_Presentation.pptx
SAP_S4HANA_eCommerce_Integration_Presentation.pptxSAP_S4HANA_eCommerce_Integration_Presentation.pptx
SAP_S4HANA_eCommerce_Integration_Presentation.pptx
vemulavenu484
 
Timeline Infographics Para utilização diária
Timeline Infographics Para utilização diáriaTimeline Infographics Para utilização diária
Timeline Infographics Para utilização diária
meslellis
 
NOC Services for maintaining network as MSA.ppt
NOC Services for maintaining network as MSA.pptNOC Services for maintaining network as MSA.ppt
NOC Services for maintaining network as MSA.ppt
ankurnagar22
 
最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
最新版美国威斯康星大学绿湾分校毕业证(UWGB毕业证书)原版定制
Taqyea
 
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptxInter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
Inter-Mirifica-Navigating-Media-in-the-Modern-World.pptx
secretarysocom
 
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animationUV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
UV_Unwrapping_Lecture_with_Figures.pptx presentation for lecture of animation
17218
 
LpQuantueer rtwrt 1e erere errerqer m.ppt
LpQuantueer rtwrt 1e erere errerqer m.pptLpQuantueer rtwrt 1e erere errerqer m.ppt
LpQuantueer rtwrt 1e erere errerqer m.ppt
cyberesearchprof
 
Unlocking Business Growth Through Targeted Social Engagement
Unlocking Business Growth Through Targeted Social EngagementUnlocking Business Growth Through Targeted Social Engagement
Unlocking Business Growth Through Targeted Social Engagement
Digital Guider
 
Internet & Protocols : A Blueprint of the Internet System
Internet & Protocols : A Blueprint of the Internet SystemInternet & Protocols : A Blueprint of the Internet System
Internet & Protocols : A Blueprint of the Internet System
cpnabil59
 
PPT 18.03.2023.pptx for i smart programme
PPT 18.03.2023.pptx for i smart programmePPT 18.03.2023.pptx for i smart programme
PPT 18.03.2023.pptx for i smart programme
AbhimanShastry
 
SAP_S4HANA_ChatGPT_Integration_Presentation.pptx
SAP_S4HANA_ChatGPT_Integration_Presentation.pptxSAP_S4HANA_ChatGPT_Integration_Presentation.pptx
SAP_S4HANA_ChatGPT_Integration_Presentation.pptx
vemulavenu484
 
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
最新版英国北安普顿大学毕业证(UoN毕业证书)原版定制
Taqyea
 
05-introduction-to-operating-systems.pptx
05-introduction-to-operating-systems.pptx05-introduction-to-operating-systems.pptx
05-introduction-to-operating-systems.pptx
pepecompany1
 
AWS Basics for non technical people.pptx
AWS Basics for non technical people.pptxAWS Basics for non technical people.pptx
AWS Basics for non technical people.pptx
sanjeevkumar123577
 
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdfCommon Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
Common Pitfalls in Magento to Shopify Migration and How to Avoid Them.pdf
CartCoders
 
simple-presentationtestingdocument2007.pptx
simple-presentationtestingdocument2007.pptxsimple-presentationtestingdocument2007.pptx
simple-presentationtestingdocument2007.pptx
ashokjayapal
 
In order to install and use the device software, your computer must meet the ...
In order to install and use the device software, your computer must meet the ...In order to install and use the device software, your computer must meet the ...
In order to install and use the device software, your computer must meet the ...
raguclc
 
COMPUTER ETHICS AND CRIME.......................................................
COMPUTER ETHICS AND CRIME.......................................................COMPUTER ETHICS AND CRIME.......................................................
COMPUTER ETHICS AND CRIME.......................................................
FOOLKUMARI
 
Quantiuwewe e3er14e we3223 32222 m2.pptx
Quantiuwewe e3er14e we3223 32222 m2.pptxQuantiuwewe e3er14e we3223 32222 m2.pptx
Quantiuwewe e3er14e we3223 32222 m2.pptx
cyberesearchprof
 
Lecture 3.1 Analysing the Global Business Environment .pptx
Lecture 3.1 Analysing the Global Business Environment .pptxLecture 3.1 Analysing the Global Business Environment .pptx
Lecture 3.1 Analysing the Global Business Environment .pptx
shofalbsb
 
SAP_S4HANA_eCommerce_Integration_Presentation.pptx
SAP_S4HANA_eCommerce_Integration_Presentation.pptxSAP_S4HANA_eCommerce_Integration_Presentation.pptx
SAP_S4HANA_eCommerce_Integration_Presentation.pptx
vemulavenu484
 
Timeline Infographics Para utilização diária
Timeline Infographics Para utilização diáriaTimeline Infographics Para utilização diária
Timeline Infographics Para utilização diária
meslellis
 
NOC Services for maintaining network as MSA.ppt
NOC Services for maintaining network as MSA.pptNOC Services for maintaining network as MSA.ppt
NOC Services for maintaining network as MSA.ppt
ankurnagar22
 
Ad

ShellAdvanced shell scripting programm.ppt

  • 1. CISC3130, Spring 2013 Dr. Zhang 1 Bash Scripting: Advanced Topics
  • 2. Outline 2  Review HW1, HW2 and Quiz2  Review of standard input/output/error  How to redirect them ?  Pipeline  Review of bash scripting  Functions  Here documents  Arrays
  • 3. Homework 2  match phone numbers in text file  7188174484, 718-817-4484, (718)817,4484  817-4484, or 817,4484, 8174484.  (01)718,817,4484, 01,718-817-4484  grep -f phone.grep file.txt , where phone.grep: [^0-9][0-9]{10}$ [^0-9][0-9]{10}[^0-9] [^0-9][0-9]{3}-[0-9]{3}-[0-9]{4}$ [^0-9][0-9]{3}-[0-9]{3}-[0-9]{4}[^0-9] [^0-9][0-9]{3},[0-9]{4}$ [^0-9][0-9]{3},[0-9]{4}[^0-9] [^0-9][0-9]{3}-[0-9]{4}$ [^0-9][0-9]{3}-[0-9]{4}[^0-9] 3 Match 10 digits at end of line Match 10 digits, and a non-digit char 718-817,4484 at end of line
  • 5. Homework 2  Write a sed script file that remove all one-line comments from C/C++ source code files. Note that such comments starting with //, and ends at the end of line. You need to take care the cases where // appears in double quote, or single quote, in thsse cases, what comes after // is not comment.  rmcnt.sed : #!/bin/sed -f ## remove one-line comments from C/C++ code /^[^'"]*/// s///.*$/ /g  rmcnt.sed sample.cpp 5 Apply to lines that contain // not preceding by ' or " Replace // and following chars with space
  • 6. Quiz 2  How to write to standard output in shell script: #!/bin/bash echo "Hello world"; echo "Something is wrong" 1>& 2 ls ABCDEF 2>&1  Try it out:  ./test_redirect.sh 2> err_out ## what happens?  ./test_redirect.sh > std_out ## what happens?  ./test_redirect.sh > std_out 2>&1 6
  • 7. Quiz2  Mark constants with < and > #/bin/bash # First find numerica constants in the code #grep -E '[^a-zA-Z_][0-9]+.?[0-9]+' $1 # now mark constants with <> echo mark constants in file $1 sed 's/([^a-zA-Z0-9_])([0-9][0-9]*.{0,1}[0-9][0-9]*)/1<2>/g' $1 7 The char before constant: not alphabet, not _, and not digit A numeric constant: Optional decimal points: .{0,1} cannot use ?, as sed use BRE
  • 8. Standard input/output/error 8  By default, link to keyboard and terminal window respectively  Can be redirected to files  Can be redirected to pipeline  input can be redirected to reading end of a pipe  output and error can be redirected to writing end of a pipe  When a bash script’s input/output/error is redirected:  E.g., headtail 3 10 .bash_profile > output  ls –l | headtail 10 24 | wc –l  input/output/error for every command in the script are redirected !
  • 9. Save standard input if necessary 9 #!/bin/bash # Count # of lines, and search for phone in a file; if a file is # not specified, process standard input set -x ## turn on execution tracing if [ $# -eq 0 ] then cat > stdinput ## save standard input to a file set stdinput fi ## so that we can use as many times as we want wc –l $* grep -f phone.grep $* rm stdinput exit 0 Or use –x in first line, i.e., #!/bin/bash –x Or type $ bash -x countlines_searchphoneno.sh to run the scripts Code at: countlines_searchphoneno.sh
  • 10. Redirection can be applied to loop 10 rm all_shellscripts for i in `ls *.sh` do echo $i >>all_shellscripts cat $i >>all_shellscripts done for i in `ls *.sh` do echo $i cat $i done > all_shellscripts Similar for <, |
  • 11. case construct: branching  case construct is analogus to switch in C/C++. case "$variable" in shellpattern1 ) command... ;; shellpattern2) command … ;; shell pattern n) command... ;; esac 11 • Quoting variables is not mandatory • Each pattern can contain shell wildcard (*,?,[a-z]), ends with a ) • Each condition block ends with ;; • If a condition tests true, then associated commands execute and the case block terminates. • entire case block ends with an esac
  • 12. Calculator using case block case "$op" in "+" ) result=$(($x + $y)) echo $x $op $y = $result;; "-" ) result=$(($x - $y)) echo $x $op $y = $result;; "*" ) result=$(($x * $y)) echo $x * $y = $result;; "/" ) result=$(($x / $y)) echo $x $op $y = $result;; * ) echo Unknow operator $op;; esac 12
  • 13. #!/bin/bash OPT=$1 # option FILE=$2 # filename # test -e and -E command line args matching case $OPT in -e|-E) echo "Editing $2 file..." # make sure filename is passed else an error displayed [ -z $FILE ] && { echo "File name missing"; exit 1; } || vi $FILE ;; -c|-C) echo "Displaying $2 file...“ [ -z $FILE ] && { echo "File name missing"; exit 1; } || cat $FILE ;; -d|-D) echo "Today is $(date)" ;; *) echo "Bad argument!" echo "Usage: $0 -ecd filename" echo " -e file : Edit file." echo " -c file : Display file." echo " -d : Display current date and time." ;; esac 13 test if string is null Lazy evaluation of && and ||
  • 14. Case example case $1 in -f) ## case for –f option ;; -d | --directory) ## -f or –directory option ;; *) echo $1: unknown option >&2 exit 1; esac 14
  • 15. More about bash loop structures 15
  • 16. Infinite loop 16 while [ 1 ] do echo -n "Enter your password" read input if [ $input = "secret" ] then break ## break out of the loop else echo -n "Try again... " fi done
  • 17. continue command  Continue from the top of the for loop  Ignore rest of commands in the loop, and continue the loop from the top again (for the next value in the list) i=1 for day in Mon Tue Wed Thu Fri Sat Sun do echo -n "Day $((i++)) : $day" if [ $i -eq 7 -o $i -eq 8 ]; then echo " (WEEKEND)" continue; fi echo " (weekday)" done 17
  • 18. For loop without a list #!/bin/bash for i do echo hello $i done 18
  • 19. For loop i=1 for username in `awk -F: '{print $1}' /etc/passwd` do echo "Username $((i++)) : $username" done 19
  • 20. Loop through files/directories  loop through files and directories under a specific directory i=1 cd ~ for item in * do echo "Item $((i++)) : $item" done 20
  • 21. C-Style for loop for (( EXP1; EXP2; EXP3 )) do Command1 … Commandn done  Example: #!/bin/bash for (( c=1; c<=5; c++ )) do echo "Welcome $c times“ done 21 EXP1: initializer EXP2: a loop-test or condition EXP3: counting expression
  • 22. Select loop  select construct: allows easy menu generation select WORD [in LIST] do RESPECTIVE-COMMANDS; done 1. List of items printed to standard error, each item preceded by a number.  If in LIST is not present, positional parameters (command line arguments) are used 2. A prompt is printed, one line from standard input is read. 1.If input is a number corresponding to one of items, value of WORD is set to name of that item. 2.If line is empty, items and the PS3 prompt are displayed again. 3. If an EOF (End Of File) is read, loop exits. 3. RESPECTIVE-COMMANDS are executed after each selection 4. Go back to 1 22
  • 23. select construct: example 23 #!/bin/bash OPTIONS="Hello Quit“ select opt in $OPTIONS; do if [ "$opt" = "Quit" ] then echo done exit elif [ "$opt" = "Hello" ] then echo Hello World else echo bad option fi done ~zhang/public_html/cs3130/Codes/select_ex
  • 24. Next: 24  More advanced bash scripting  Array  Function  Inline input, or here document
  • 25. Array  Bash provides one-dimensional array variables  Assign values to array: array=( one two three ) files=( "/etc/passwd" "/etc/group" "/etc/hosts" ) limits=( 10 20 26 39 48)  Access array element : ${array_name[index]}  indexed using integers and are zero-based. ${array[1]}  To access all items in arary: ${array_name[*]}, ${array_name[@]}  To access array length: len=${#x[@]} 25
  • 26. To Iterate Through Array Values 26 #!/bin/bash # declare an array called array and define 3 vales array=( one two three ) for i in "${array[@]}" do echo $i done
  • 27. Exercise/Example  Write a script that read a sequence of numbers and save them in an array, print out the array content and size.  Usage: EchoNumber [file]  If no file is specified, read from standard input  Example script:  LargestSmallest.sh 27
  • 28. #!/bin/bash i=0 if [ $# -eq 0 ] then echo "Enter the numbers, Ctrl-D to end"; cat > stdinput set stdinput fi while read num do a[$i]=$num i=$((i+1)) done < $1 echo Array is ${a[*]}, with ${#a[*]} numbers 28
  • 29. Bash function  Functions: to increase modularity and readability  More efficient than breaking scripts into many smaller ones  Syntax to define a function: function functionname() { commands . . }  function is a keyword which is optional.  functionname is the name of the function  No need to specify argument in ( )  commands – List of commands to be executed I  exit status of the function is exit status of last command executed in the function body. 29
  • 30. Function call  Call bash function from command line or script  $ functionname arg1 arg2  When shell interprets a command line, it first looks into the special built-in functions like break, continue, eval, exec etc., then it looks for shell functions.  function defined in a shell start up file (e.g.,.bash_profile ).  available for you from command line every time you log on 30
  • 31. About functions 31  Parameter passing: $1, $2, …  Result returning  Use echo command  Through setting a variable  return command: to return an exit status #! /bin/bash calsum(){ echo `expr $1 + $2` } x=1;y=2; sum=`calsum $x $y` calsum(){ sum=`expr $1 + $2` } x=1;y=2; calsum $x $y echo z=$sum
  • 32. About functions 32  Local variable: its scope is within the function #! /bin/bash calsumsqr(){ local sum=`expr $1 + $2`; echo `expr $sum * $sum` } x=1;y=2; z=`calsum $x $y`
  • 33. Exercise/Example  Write a function that check whether a user is log on or not (CheckUser.sh) function UserOnline() { if who | grep $1 ## UserOnline takes a parameter then return 0 ## 0 indicates success else return 1 ##1 for failure, i.e., offline fi } if UserOnline $1 ## function’s return value as condition/test then echo User $1 is online else echo User $1 is offline fi 33
  • 34. Here document (inline document) 34  A special way to pass standard input to a command: here document, i.e., from shell script itself  Benefits: store codes and data together, easier to maintain #!/bin/bash cat <<!FUNKY! Hello This is a here Document !FUNKY! Here document starts with <<, followed by a special string which is repeated at the end of the document. Note: the special string should be chosen to be rare one.
  • 35. Here document:2 35  Example: 411 script that looks up a phone book  Usage example: 411 joke #!/bin/bash grep “$*” << End Dial-a-joke 212-976-3838 Dial-a-prayer 212-246-4200 Dial santa 212-976-141 End
  • 36. Phone # searching script #!/bin/bash cat > phone.pattern << PATTERNS [^0-9][0-9]{10}$ [^0-9][0-9]{10}[^0-9] [^0-9][0-9]{3}-[0-9]{3}-[0-9]{4}$ [^0-9][0-9]{3}-[0-9]{3}-[0-9]{4}[^0-9] [^0-9][0-9]{3},[0-9]{4}$ PATTERNS grep -f phone.pattern $* rm phone.pattern ##no need to keep this file … 36
  • 37. A case study: bundle program 37  Suppose a friend asks for copies of shell files in your bin directory $ cd ~/bin $ for i in *.sh > do > echo ===== This is file $i ============= > cat $i > done | mail [email protected] Pipeline & input/output redirection can be applied to for, while, until loop.
  • 38. Make it better ? 38  Construct a mail message that could automatically unpack itself, i.e., to generate the original files packed inside  E.g., A shell script contains instructions for unpacking, and the files content themselves  Use here document mechanism
  • 39. A bash script contains two files 39 #To unbundle, bash this file echo file1 1>&2 cat >file1 <<'End of file1' A B C End of file1 echo file2 1>&2 cat >file2 <<'End of file2' 1 2 3 end of file2 What does this script do? How to create such bundle file automatically? Use a script, bundle.sh file1 file2 file3 …
  • 40. Bundle script 40 #!/bin/bash ## write a shell script that contains files specified in arguments echo '#!/bin/bash' echo ‘# To unbundle, bash this file’ for i ## without a list of items, loop through ## command line arguments do echo “echo $i 1>&2” echo “cat >$i <<‘End of $i’” cat $i echo “End of $i” done codes/examples/bundle.sh For homework 3, use bundle.sh to generate a hw3_bundle file, and submit it.
  • 41. Summary  Review of shell scripting  Examples  Array, function, inline document 41
  • 42. Outline 42  Coding standard: how to get good grades in lab assignment  Review of standard input/output/error  How to redirect them ?  Pipeline  Review of bash scripting
  • 43. Bash scripting: general hints 43  Use echo command to trace (like cout, printf in C/C++)  Sometimes there are alternatives ways to do things, choose one and remember it:  $(( …)), and $[ … ]  [[ ]] for test  Be careful about typo, shell wont complain variable not declared/assigned …  The price of freedom  A walk-through of basic bash scripting
  • 44. Bash Scripting 44  Variables  Environment variable: affect behavior of shell  User defined variable: default type is string, can declare it to be other type  Positional variables: used to pass command line arguments to bash script  Variable assignment: x=10 ## assign value 10 to variable x, no space around = x=$x+1 ## add 1 to x’s value and assign to x PATH=$PATH:.:~/bin  To refer to a variable’s value, precede variable name with $
  • 45. A script that display positional variable 45 echo All arguments: $* echo Number of arguments: $# echo Script name: $0 echo argument 1: $1 echo argument 2: $2 for arg in $* do echo argument $arg done
  • 46. arithmetic operation 46  As variable’s default type is string, to perform arithmetic operation, use the following syntax $[$x+1] or $(($x+1))  For simpler syntax: declare variable to be numerical declare –i x x=$x*10+2  Above are for integer arithmetic operations only ..
  • 47. Command bc 47  An arbitrary precision calculator $ bc 3.14159*10^2 314.15900 130^2 16900 sqrt(1000) 31 scale=4 sqrt(1000) 31.6277 quit An interactive calculator: * user input shown in normal font, * result shown in italics font Internal variable scale: * control the number of decimal points after decimal point
  • 48. bc in command line/script 48  To evaluate an expression, simply send it using pipe to bc echo "56.8 + 77.7" | bc  Write a script that read a Fahrenheit degree from standard input, convert it to Celsius degree (up to 2 digits after decimal point): C=(F-32)*5/9  Base conversion, from base 10 (decimal) to base 16 (hexadecimal) echo "obase=16; ibase=10; 56" | bc
  • 49. Test/Conditions 49  Any command or script, if it return 0, then test is successful if rm tmp.txt then echo file tmp.txt has been deleted else echo fail to remove file tmp.txt Fi  Use ! to negate
  • 50. Test Condition 50  Numerical comparisons  -lt, -ne, -ge, …  String comparision  Pattern matching: using double brackets  To test if first argument is “–” followed by a number: if [[ "$1" == -[0-9]* ]] then ….