CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Advanced Shell Programming.

Slides:



Advertisements
Similar presentations
Shell Scripting Printing; Loops and Logic printf: formatted print printf is a more standardized method to print text to the screen. It can be used instead.
Advertisements

CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Linux Shell Script. Shell script The first line is used to specify shell program #!/bin/sh Variables variable=“text” variable=0 variable=`program arguments`
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
More Shell Programming Learning Objectives: 1. To learn the usage of environment (shell) variables in shell programming 2. To understand the handling of.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Programming. Shell Scripts (1) u Basically, a shell script is a text file with Unix commands in it. u Shell scripts usually begin with a #! and.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
Introduction to UNIX / Linux - 11
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
1 Shell Programming – Extra Slides. 2 Counting the number of lines in a file #!/bin/sh #countLines1 filename=$1#Should check if arguments are given count=0.
An Introduction to Unix Shell Scripting
The if construct The general format of the if command is: Every command has exit status If the exit status is zero, then the commands that follow between.
Shell Scripting Todd Kelley CST8207 – Todd Kelley1.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 11: Shell.
#!/bin/sh echo Hello World cat Firstshellscript.sh Firstshellscript.sh.
Shell Programming. Introducing UNIX Shells  Shell is also a programming language and provides various features like variables, branching, looping and.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Shell Programming. An example u tr abcdefghijklmnopqrstuvwxyz \ thequickbrownfxjmpsvalzydg file2 –encrypts file1 into file2 u record this command with.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) More Shell Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
CISC3130, Spring 2011 Dr. Zhang 1 Bash Programming Review.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Week Five Agenda Link of the week Review week four lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
Sed. Class Issues vSphere Issues – root only until lab 3.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Group, group, group One after the other: cmd1 ; cmd2 One or both: cmd1 && cmd2 Only one of them: cmd1 || cmd2 Cuddling (there):( cmd1 ; cmd2 ) Cuddling.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Shell Control Structures
CIT 470: Advanced Network and System Administration
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
Unix Scripting Session 4 March 27, 2008.
INTRODUCTION TO UNIX: The Shell Command Interface
Writing Shell Scripts ─ part 3
CIT 470: Advanced Network and System Administration
What is Bash Shell Scripting?
LING 408/508: Computational Techniques for Linguists
Presented by, Mr. Satish Pise
Shell Control Structures
Chapter 5 The Bourne Shell
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Introduction to Bash Programming, part 3
Review The Unix Shells Graham Glass and King Ables,
Presentation transcript:

CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Advanced Shell Programming

CIT 140: Introduction to ITSlide #2 Topics 1.Numbers in the shell. 2.Here Documents. 3.Reading and parsing files with read. 4.Functions. 5.Debugging

CIT 140: Introduction to ITSlide #3 Numeric Data Processing Bourne shell variables Store character strings, not numbers. To perform arithmetic/logic ops Need expr command.

CIT 140: Introduction to ITSlide #4 Numeric Data Processing expr args Purpose:Evaluate the expression arguments, ‘args’, and send the result to standard output. Commonly used operators/features: \| return the first expression if it is not null or 0; else return the second expression \& Return the first expression if neither is null or 0;else return 0 =, \>, \>=, Integer comparison operators;equal, \<, \<=,!= greater than, greater than or equal to, less than,less than or equal to, not equal +, -, \*, /, % Integer arithmetic operators, add, subtract, multiply, integer divide (return quotient), remainder

CIT 140: Introduction to ITSlide #5 Numeric Data Processing $ var1=10 $ var1=`expr $var1 + 1` $ echo $var1 11 $ var1=`expr $var1 \* $var1` $ echo $var1 121 $ echo `expr $var1 / 10` 12 $ echo `expr $var1 % 10` 1 $ See ~waldenj/cit140/expr_example.sh for more examples.

CIT 140: Introduction to ITSlide #6 Counting wtih expr #!/bin/sh if [ $# -ne 1 ]; then echo "Usage: countup number" exit 1 fi count=0 limit=$1 while [ "$count" -lt "$limit" ] do echo -n "$count " count=`expr $count + 1` done echo

CIT 140: Introduction to ITSlide #7 Here Documents We use echo for single line printing. What do we do for multi-line printing? We use I/O redirection from our script. Use the previously unused << operator. cat <<HERE Cat’ing a here document will print every line that follows to the screen until it encounters the word following <<. HERE

CIT 140: Introduction to ITSlide #8 Here example: disk usage #!/bin/sh if [ $# -ne 1 ]; then cat <<EOF Usage: mail username Sends a notice mail to username. EOF fi diskusage=`du -k $HOME` mail $1 <<EOF This is a disk usage notice. You are using the following disk space: $diskusage EOF

CIT 140: Introduction to ITSlide #9 Reading Files The read command can use I/O redirection. > read line </etc/passwd > echo $line root:x:0:1:Super-User:/:/sbin/sh

CIT 140: Introduction to ITSlide #10 Reading Files Loops can use I/O redirection with read. while read line do echo $line done </etc/passwd

CIT 140: Introduction to ITSlide #11 Example: cat #!/bin/sh if [ $# -lt 1 ]; then echo "Usage: cat file1 [file2,...]" fi for file in do while read line do echo $line done <$file done

CIT 140: Introduction to ITSlide #12 Parsing files using read > cat hosts.sh #!/bin/sh egrep -v "^#" /etc/hosts >hosts.txt while read ipaddress hostname ignore do echo "Host $hostname has IP address $ipaddress." done <hosts.txt rm hosts.txt >./hosts.sh Host localhost has IP address Host zappa has IP address Host mailfe2 has IP address Host axp1 has IP address

CIT 140: Introduction to ITSlide #13 Functions function () { commands; } “Mini-scripts” inside your script Invoked like another shell script. Handle arguments like shell scripts do. Exit status is exit status of last command.

CIT 140: Introduction to ITSlide #14 Function Example #!/bin/sh findstr() { dir=$1 string=$2 find $dir -type f -print | xargs fgrep $string } if [ $# -ne 2 ]; then echo "Usage: findstring dir string" exit 1 fi findstr $1 $2

CIT 140: Introduction to ITSlide #15 Function Reuse Example #!/bin/sh backupfile() { target=$1 if [ -f $target ]; then cp -p $target $target.bak fi } if [ $# -lt 1 ]; then echo "Usage: backupdir dir" exit 1 fi cd $1 for file in * do backupfile $file done

CIT 140: Introduction to ITSlide #16 Why use Functions? 1.Organize your program. –Break up program into pieces that fit on one screen so you can read and understand it. 2.Code reuse –Can call a function multiple times. –DRY (Don’t Repeat Yourself) Principle.

CIT 140: Introduction to ITSlide #17 Debugging Shell Programs Noexec: check errors but don’t run > sh –n script set –o noexec Verbose trace (can be combined with –n) > sh –v script set –o verbose Short trace > sh –x script set –o xtrace

CIT 140: Introduction to ITSlide #18 Debugging Shell Scripts > sh –n backupdir.sh tmp > sh -x backupdir.sh tmp + [ 1 -lt 1 ] + cd tmp + backupfile crypt.txt target=crypt.txt + [ -f crypt.txt ] + cp -p crypt.txt crypt.txt.bak + backupfile lcdays.txt target=lcdays.txt + [ -f lcdays.txt ] + cp -p lcdays.txt lcdays.txt.bak + backupfile who.txt target=who.txt + [ -f who.txt ] + cp -p who.txt who.txt.bak