2 nd Intro to Shell Scripting. Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due.

Slides:



Advertisements
Similar presentations
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Advertisements

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.
Information Networking Security and Assurance Lab National Chung Cheng University 1 if condition Condition is defined as: "Condition is nothing but comparison.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Introduction to Linux and Shell Scripting Jacob Chan.
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 Control Structures CSE 2031 Fall August 2015.
Unix/Linux basics 0011 Operating systems lab Gergely Windisch room 4.12
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.
Bash shell programming Part II – Control statements Deniz Savas and Michael Griffiths November 2005 Corporate Information and Computing Services The University.
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.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Lecture 2: Advanced UNIX, shell scripts (ol)‏ Programming Tools And Environments.
Summer 2015 SILICON VALLEY UNIVERSITY CONFIDENTIAL 1 Introduction to UNIX / Linux - 13 Dr. Jerry Shiao, Silicon Valley University.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
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.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
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.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
CISC3130 Spring 2013 Fordham Univ. 1 Bash Scripting: control structures.
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Shell Control Statements and More
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.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Sed. Class Issues vSphere Issues – root only until lab 3.
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
CSCI 330 UNIX and Network Programming Unit X: Shell Scripts II.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Week Two Agenda Announcements Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Next lab assignments.
Linux Administration Working with the BASH Shell.
Shell Control Structures CSE 2031 Fall June 2016.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Bash Shell Scripting 10 Second Guide.
Shell Control Structures
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Part 1: Basic Commands/Utilities
CSE 303 Concepts and Tools for Software Development
What is Bash Shell Scripting?
LING 408/508: Computational Techniques for Linguists
Unix Scripting Dave Yearke
UNIX Reference Sheets CSE 2031 Fall 2010.
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
CSC 4630 Meeting 4 January 29, 2007.
Introduction to Bash Programming, part 3
Basic shell scripting CS 2204 Class meeting 7
Unix Scripting Session 2 March 13, 2008.
Presentation transcript:

2 nd Intro to Shell Scripting

Dates for Last Week of Class Homework 7 – Due Tuesday 5/1 by midnight Labs 7 & 8 or late ones – 8 is extra credit – Due Thursday 5/3 by midnight – 5/4 time stamps mean no grade! Final – Thursday 5/3 – 6 – 9:30 in classroom – open [book|notes|computer] – will need working VM

Student Evaluations The online evaluation system will be open for evaluation of full term courses starting Friday, April 27th. Evaluations will close at 11:59pm, Sunday May 6th. Don't do them, grade get held hostage until May 23.

Shell Script 101 List of commands interpreted by a shell cd /var/tmp mkdir boks cd boks gzip -dc /var/tmp/boks_install.tar.gz | tar xf -./install.jvss0001 cd /var/tmp rm -r boks* cd /etc/rc2.d mv S99boksm _S99boksm

Why need? Add users List bad perms Parsing log files Startup of system (most of startup of os uses scripts) etc

Variables Assignments (no spaces) I=0 E=$((C+D)) Comparison ( needs spaces) I = 0 X = A

Input/Output/Assignment Input –read Output –Echo –printf Assignment X=$I Not X=I

Comparisions Numeric –-eq, -ne, -lt, -le, -gt, -ge Strings –=, != And is && Or is ||

Comparisions II File testing: –-d – is the item a directory? –-e – does the file already exist? –-f – does the file exist and is it a regular file? –-h (or –L) – is the item a symbolic link? (current user) –-r – does the file exist and is it readable? –-w – does the file exist and is it writable? –-x – does the file exist and is it executable?

Sample Comparisons [ -f my_file ] (don’t forget spaces) [ $x –ge 0 ] [ $1 = start ] [ “$1” = “” ] – empty string test!

if then else if [ $count –ne 0 ] then avg=$((sum/count)) else avg=0 fi

for for var in do echo $var done

Command Line Parameters Accessed via $# parameters –$0 is script name –$1, $2 … $9 = parameters on the command line are all parameters –$# or $* is total number passed Assignments say receive as parameter, or list of parameters assume it’s this one!

Accessing Parameters #!/bin/bash small= is all cmd line params for num in do if [ $num -lt $small ] then small=$num fi eone echo the smallest is $small

Advanced Shell Scripting

while secretcode=agent007 echo Guess the code! echo -e 'Enter your guess: \c' read yourguess while [ $secretcode != $yourguess ] do echo Good guess but wrong. Try again! echo -e 'Enter your guess: \c' read yourguess done echo Wow! You are a genius!! exit 0

I/O Redirection > = overwrite >> = append < = read from a file << = read stdin until keyword entered

while read cat web_servers.txt | while read REMOTE_MACHINE USER_PASSWORD do echo $REMOTE_MACHINE == $USER_PASSWORD scp update_solaris_www.sh bld01 done web_servers.txt web1password1 web2password2

while read issues? Watch for spaces in input IFS defaults to white space

case case $FOO in 1) FOO=$((FOO+1)) echo FOO is now $FOO ;; *) FOO=0 echo FOO is now $FOO ;; esac

Accessing Files? for var in `ls -1 /etc/rc5.d/S*` do echo -e “$var:\c” for lines in `cat $var` do echo $lines | grep ^# done | wc -l done

Scripts and Awk #!/bin/bash sum=0 for i in * do if [[ -f $i && -r $i ]] then temp=`ls -l $i | awk '{print $5}'` sum=$((sum+temp)) fi done echo The readable files in this directory are $sum in size