More Shell Programming Learning Objectives: 1. To learn the usage of environment (shell) variables in shell programming 2. To understand the handling of.

Slides:



Advertisements
Similar presentations
More Shell Programming Software Tools. Slide 2 Keyword Shell Variables l The shell sets keyword shell variables. You can use (and change) them. HOME The.
Advertisements

More about Shells1-1 More about Shell  Shells (sh, csh, ksh) are m Command interpreters Process the commands you enter m High-level programming languages.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Shell Programming Software Tools. Slide 2 Shells l A shell can be used in one of two ways: n A command interpreter, used interactively n A programming.
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 Basics CS465 - Unix. Unix shells User’s default shell - specified in /etc/passwd file To show which shell you are currently using: $ echo $SHELL.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Introduction to Unix – CS 21 Lecture 13. Lecture Overview Finding files and programs which whereis find xargs Putting it all together for some complex.
Shell Scripts 4 A shell usually interprets a single line of input, but we can also create a file containing a number of lines of commands to be interpreted.
UNIX Utilities Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn Input/Output.
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 Script Examples.
7/17/2009 rwjBROOKDALE COMMUNITY COLLEGE1 Unix Comp-145 C HAPTER 2.
Shell Control Structures CSE 2031 Fall August 2015.
CTEC 1863 – Operating Systems Shell Scripting. CTEC F2 Overview How shell works Command line parameters –Shift command Variables –Including.
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.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
CS 6560 Operating System Design Lecture 3:Tour of GNU/Linux.
UNIX/LINUX Shells Shell is an UNIX/LINUX command interpreter. Shell command can be internal or external. The code to execute an internal command is part.
Introduction to Linux OS (IV) AUBG ICoSCIS Team Prof. Volin Karagiozov March, 09 – 10, 2013 SWU, Blagoevgrad.
Lecture 4  C Shell Scripts(Chapter 10). Shell script/program  Shell script: a series of shell commands placed in an ASCII text file  Commands include.
LINUX System : Lecture 6 Shell Programming
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where.
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.
CS465 - UNIX The Bourne Shell.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Lesson 2 1.Commands 2.Filename Substitution 3.I/O Redirection 4.Command Grouping 5.Shell Responisibilites Review of the Basics.
L&T Infotech1 UNIX – Getting Started - Aneesh Ramani.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
LIN Unix Lecture 5 Unix Shell Scripts. LIN Command Coordination ; && || command1 ; command2 Interpretation: Do command 1. Then do command.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
CIT 140: Introduction to ITSlide #1 CIT 140: Introduction to IT Shell Programming.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
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.
Shell Control Statements and More
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
More Shell Programming. Slide 2 Control Flow  The shell allows several control flow statements:  if  while  for.
Jozef Goetz, expanded by Jozef Goetz, 2006 Credits: Parts of the slides are based on slides created by textbook authors, Syed M. Sarwar, Robert.
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.
If condition1 then statements elif condition2 more statements […] else even more statements fi.
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.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Shell Control Structures CSE 2031 Fall June 2016.
INTRODUCTION TO SHELL SCRIPTING By Byamukama Frank
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
Shell Control Structures
The UNIX Shell Learning Objectives:
Part 1: Basic Commands/Utilities
LINUX System : Lecture 5 (English-Only Lecture)
Shell Script Assignment 1.
LING 408/508: Computational Techniques for Linguists
LING 408/508: Computational Techniques for Linguists
CS 60 Discussion Review.
UNIX Reference Sheets CSE 2031 Fall 2010.
Linux Shell Script Programming
Shell Control Structures
Shell Control Structures
More Shell Programming
Chapter 3 The UNIX Shells
Introduction to Bash Programming, part 3
Review.
Presentation transcript:

More Shell Programming Learning Objectives: 1. To learn the usage of environment (shell) variables in shell programming 2. To understand the handling of command line arguments 3. To illustrate more useful commands in shell programming using examples

COMP111 Lecture 8 / Slide 2 More Shell Programming Table of Content  Keyword Shell Variables  Keyword Example  Read-only Shell Variable  Command Line Argument  Command for handling arguments - shift  Command for handling arguments - set  Special Variable - $$  Looping using for  Looping using for – More Examples  Return Status Example  << Examples

COMP111 Lecture 8 / Slide 3 Keyword Shell Variables  The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where the shell looks for executables USER Your login name SHELL The name of the shell you are running PWD The current working directory PRINTER Can be loaded with your default printer

COMP111 Lecture 8 / Slide 4 Keyword Example $ cat env #!/bin/sh echo "Hi $USER!" echo "Your home is: $HOME" echo "Your path is: $PATH" echo "Your current directory is: $PWD" echo "Your shell is: $SHELL" echo "Your printer is: $PRINTER" $ env Hi qyang! Your home is: /homes/qyang Your path is:/usr/bin:.:.:/homes/horner/Unix/bin:... Your current directory is: /homes/qyang/111 Your shell is: /bin/csh Your printer is: csl3

COMP111 Lecture 8 / Slide 5 Read only Shell Variables  Read Only means you cannot change it; just read its value  $0 is the program name (the filename the user typed to invoke the shell script): $ cat print1 #!/bin/sh echo "This script is called $0" $ print1 This script is called print1 $./print1 This script is called./print1 $ ~/111/print1 This script is called /homes/qyang/111/print1

COMP111 Lecture 8 / Slide 6 Command Line Arguments – (1)  The command line arguments that you call a script with are stored in variables $1, $2,..., $9. $ cat args1 #!/bin/sh echo "The args are $1 $2 $3 $4 $5 $6 $7 $8 $9" $ args1 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The args are a1 a2 a3 a4 a5 a6 a7 a8 a9  With more than 9 arguments, they are still stored, but they have to be moved using the shift command before they can be accessed.

COMP111 Lecture 8 / Slide 7 Command Line Arguments – (2)  Example: How to write a command to swap two files? $ cat swap #!/bin/sh mv $1 /tmp/$1 mv $2 $1 mv /tmp/$1 $2 $ cat it1 contents of file1 $ cat it2 contents of file2 $ swap it1 it2 $ cat it1 contents of file2 $ cat it2 contents of file1 $

COMP111 Lecture 8 / Slide 8 Command Line Arguments – (3)  $* lists all the command line args: $ cat args2 #!/bin/sh echo "The args are $*" $ args2 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The args are a1 a2 a3 a4 a5 a6 a7 a8 a9 a10  $# contains the number of args: $ cat args3 #!/bin/sh echo "The number of args is $#" $ args3 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 The number of args is 10

COMP111 Lecture 8 / Slide 9  The for statement executes a loop once for each of a list of possibilities: $ cat printall #!/bin/sh for file in * do if [ -f $file ] then echo "Print $file [y/n]? " read resp if [ $resp = "y" ] then lpr -Pcll3 $file fi done $ printall Print letter1 [y/n]? y Print names [y/n]? n Looping using for – Example 1

COMP111 Lecture 8 / Slide 10  The in clause of the for statement accepts as many parameters as you wish in many forms: $ for i in ; do echo $i ; done $ for pid in `ps -a | tail +2 | cut -c1-6 | sort` > do > kill -9 $pid > done kill: permission denied (you will then be logout!) Looping using for - Example 2

COMP111 Lecture 8 / Slide 11  We can make use of the find command to generate a list of files in a sub-directory tree for processing: #!/bin/sh if [ $# = 0 ] then echo "usage: treeproc [ ]"; exit fi pattern="$1"; shift if [ $1 ] ; then cmd=$*; else cmd=echo; fi echo "Running \"$cmd\" for $pattern in dir: `pwd`" for file in `find. -name "$pattern" -print` do $cmd $file done $ find. -name ‘letter*’ -print./secret/letter1./secret/letter2 Looping using for – Example 3  The shift command promotes each command line argument by one (e.g., the value in $2 moves to $1, $3 moves to $2, etc.)

COMP111 Lecture 8 / Slide 12 $ ls -R.: namesletter0secret/./secret: letter1letter2 $ treeproc usage: treeproc [ ] $ treeproc "*1" Running "echo" for *1 in dir: /home/kwchiu./secret/letter1 $ treeproc "letter*" rm -i Running "rm -i" for letter* in dir: /home/kwchiu rm: remove./letter0 (yes/no)? rm: remove./secrete/letter1 (yes/no)? rm: remove./secrete/letter2 (yes/no)? $ find. -name "letter*" -exec rm -i {} \; (same as the above) Looping using for – Example 3 testing l {} substitutes each filename l \ denotes end of the exec command l {} substitutes each filename l \ denotes end of the exec command

COMP111 Lecture 8 / Slide 13 Example 4: Here documents <<  Documents can be embedded in a shell script with << with shell variable processing (e.g., mail merge). w | tail +3 | ( while read user tty login idle other do cmd=`echo $other | tr -s '[0-9]' ' '` if [ `echo $idle | grep -c day` -ge 1 ] then mail $user << EOF Dear $user, May I remind you that your session has been idle for $idle. The running program was: $cmd Regards, SysAdmin. EOF fi done ) 1:20pm up 41 day(s), 3:36, 5 users, load average: 0. User tty idle JCPU PCPU what wfung pts/2 Thu10pm 10: telnet wfung pts/4 Sat12am 11:23 1 telnet dominic pts/3 Fri10am 2days -bash You may use any words – EOF is just an example