Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.

Slides:



Advertisements
Similar presentations
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Advertisements

CIS 118 – Intro to UNIX Shells 1. 2 What is a shell? Bourne shell – Developed by Steve Bourne at AT&T Korn shell – Developed by David Korn at AT&T C-shell.
CS 497C – Introduction to UNIX Lecture 22: - The Shell Chin-Chih Chang
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.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Compunet Corporation Introduction to Unix (CA263) More on Parameters By Tariq Ibn Aziz Dammam Community College.
Introduction to Unix (CA263) Decisions, Decisions By Tariq Ibn Aziz Dammam Community College.
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.
Shell Programming 1. Understanding Unix shell programming language: A. It has features of high-level languages. B. Convenient to do the programming. C.
QUOTATION This chapter teaches you about a unique feature of the shell programming language: the way it interprets quote characters. Basically, the shell.
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.
Shell Programming, or Scripting Shirley Moore CPS 5401 Fall August 29,
1 Bourne Shell Programming l Web Sources l Bourne Shell Tutorials: l l
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
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.
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.
The UNIX Shell. The Shell Program that constantly runs at terminal after a user has logged in. Prompts the user and waits for user input. Interprets command.
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.
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.
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 Operating Systems Lecture 2 UNIX and Shell Scripts.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
Introduction to Unix (CA263) Passing Arguments By Tariq Ibn Aziz Dammam Community College.
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.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
1 © 2000 John Urrutia. All rights reserved. Session 5 The Bourne Shell.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.
Compunet Corporation Introduction to Unix (CA263) Your Environment By Tariq Ibn Aziz Dammam Community College.
Environment After log in into the system, a copy of the shell is given to the user Shell maintains an environment which is distinct from one user to another.
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
Chapter 5: The Shell The Man in the Middle. In this chapter … The command line Input, output, and redirection Process management Wildcards and expansion.
Introduction to Unix (CA263) Command File By Tariq Ibn Aziz.
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
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.
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]
Compunet Corporation Introduction to Unix (CA263) Reading Data By Tariq Ibn Aziz Dammam Community College.
Various 2. readonly readonly x=4 x=44 #this will give an error (like what in java?)
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
Chapters 13 and 14 in Quigley's "UNIX Shells by Example"
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
Lecture 9 Shell Programming – Command substitution
Agenda Control Flow Statements Purpose test statement
What is Bash Shell Scripting?
Agenda Functions: What are functions? / Purpose of Functions
Shell Control Structures
Shell Control Structures
Introduction to Bash Programming, part 3
Introduction to Unix (CA263) Passing Arguments
Presentation transcript:

Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College

Compunet Corporation Objectives In this lecture you will learn the following loops –the for; –the while; and –the until.

Compunet Corporation The for Command The for command is used to execute a set of commands a specified number of times. Here is a loop example that will execute 3 times for i in do echo $i done Try directly on terminal $ for i in >do >echo $i >done $

Compunet Corporation Example for Command $cat run tbl $1 |nroff –mm –Tlp |lp $ If you want to run the files memo1 through memo2 you can use the for command $for file in memo1 memo2 >do >run $file >done $ The shell permits filename substitution in the list of words in the for command. for file in memo[1-2] do run $file done

Compunet Corporation Example for Command If you want to run all of the files in your current directory. for file in * do run $file done $ If the file filelist contains a list of the files that you want to run through run. files=`cat filelist` for file in $files do run $file done

Compunet Corporation The $* Variable If you found that you were using the run program to process several files at once. $ cat run for file in $* do tbl $file |nroff –mm Tlp | lp done $ $ run memo1 memo2 memo3 memo4 The $* will be replaced by the four arguments memo1, memo2, memo3, and memo4.

Compunet Corporation The $* Variable[1] The $* in for command will be replaced by shell with a b c $ cat args echo Number of argument passed is $# for arg in $* do echo $arg done $ $ args a b c Number of argument passed is 3 a b c $

Compunet Corporation The $* Variable[2] $ args 'a b' c Number of argument passed is 2 a b c $ Even though a b was passed as a single argument to args, the $* in the for command was replaced by shell with a b c, which is three words.

Compunet Corporation The Variable[1] The special variable “ ” will be replaced with "$1", "$2" …, the double quotes are necessary around otherwise it will behave has $*. $ cat args echo Number of argument passed is $# for arg in do echo $arg done $ $ args a b c Number of argument passed is 3 a b c $

Compunet Corporation The Variable[2] $ args 'a b' c Number of argument passed is 2 a b c $ The special variable “ ” will be replaced with "$1", "$2" …, the double quotes are necessary around otherwise it will behave has $*.

Compunet Corporation The while Command The second type of looping command is while. $cat twhile i=1 While [ "$i" –le 5 ] do echo $i i=`expr $i + 1` done $twhile $

Compunet Corporation The while Command The program print each of the command-line argument one per line. $ cat prargs While [ "$#" –ne 0 ] do echo $i shift done $ $ prargs $ $ prargs a b c a b c $ prargs 'a b' c a b c $ prargs * addresses nu name phonebook stat

Compunet Corporation The until Command The while command continues execution as long as the command listed after the while returns a zero exit status. The until command is similar to the while, only it continues execution as long as the command follows the until returns a nonzero exit status.

Compunet Corporation Example-1 until Command[1] $ cat mon if [ "$#" –ne 1 ] then echo "Usage: mon user" exit 1 fi until who | grep "^$user ">/dev/null do sleep 60 done echo " $user has logged on“ $

Compunet Corporation Example-1 until Command[2] $ mon sandy sandy has logged on $ mon sandy & 4392 $ nroff newmemodo other work … sandy has logged on

Compunet Corporation Example-2 until Command[1] $ cat mon if [ "$1" = -m ] then mailopt=TRUE shift else mailopt=FALSE fi if [ "$#" –eq 0 –o "$#" –gt 1 ] then echo "Usage: mon [-m] user" echo "-m means to be informed by mail" exit 1 fi user="$1" until who|grep "^$user ">/dev/null do sleep 60 done if [ "$mailopt" =FALSE ] then echo " $user has logged on" else echo " $user has logged on"|mail steve fi $ $ man sandy –m Usage: mon [-m] user -m means to be informed by mail

Compunet Corporation Example-2 until Command[2] $ mon –m sandy & $ mon sandy & 5435 $ nroff newmemodo other work … you have mail $ mail From steve Mon Jul 22 11:05 EDT 1985 sandy has logged on ?d $

Compunet Corporation More on Loops Breaking Out of a Loop –Sometime you want to make an immediate exit from a loop. You can use the break command. –If the break command is used in break n form, then the n innermost loops are immediately exited.

Compunet Corporation Example break Command Both the while and for loop will be exited if error is nonnull for file do … while [ "$count" –lt 10 ] do … if [ -n "$error" ] then break 2 fi … done … done

Compunet Corporation Skipping the Remaining Commands in Loop The continue command is similar to break, only it doesn’t cause the loop to be exited, but the remaining commands in the loop to be skipped. Like the break, an optional number can follow the continue, so continue n causes the commands in the innermost n loops to be skipped; but execution of loop then continues as normal.

Compunet Corporation Example continue Command Each value of file is checked to make sure that file exist. for file do if [ ! -f "$file" ] then echo "$file not found" continue fi … done

Compunet Corporation Executing a loop in the Background An entire loop can be sent to the background simply by placing an ampersand after the done: $ for file in memo[1-4] >do >run $file >done & 9932 $

Compunet Corporation I/O Redirection on a Loop You can also perform I/O redirection on the entire loop. $ for i in >do >echo $i >done > loopout $ cat loopout

Compunet Corporation I/O Redirection on a Loop You can override redirection of the entire loop’s input or output by explicitly redirecting the input and/or output of commands inside the loop. for file do echo "Processing file $file" >/dev/tty … done > output echo ’s output is redirected to the terminal while the rest goes to the file output.

Compunet Corporation I/O Redirection on a Loop You can also redirect the standard error output from a loop, simply by tacking on a 2> file after the done: while [ "$endofdata" –ne TRUE ] do … done 2> errors

Compunet Corporation Piping Data Into and Out of a Loop A command output can be piped into a loop, and the entire output from a loop can be piped into another command in the executed manner. $ for i in >do >echo $i >done | wc –l 4 $

Compunet Corporation Typing a Loop on One Line for i in do echo $i done becomes for i in ; do echo $i; done

Compunet Corporation Typing condition on One Line The same rules apply to while and until loops. if commands can also be typed on the same line using a similar format: $ if [ 1 = 1 ]; then echo yes; fi yes $ if [ 1 = 2 ]; then echo yes; else echo no; fi no Note that no semicolons appear after the then and the else.

Compunet Corporation The getopts Command The shell provide a built-in command getopts that exist for the express purpose of processing command line argument. The general format of the command is: getopts options variable

Compunet Corporation The getopts Command The getopts command is designed to be executed inside a loop. –Each time through the loop, getopts examines the next command line argument and determine if it is a valid option. –This check if the argument begins with a minus sign and is followed by any single letter contained inside option. If it does, getopts stores the matching option letter inside the specified variable and returns a zero exit status.

Compunet Corporation The getopts Command If the letter that follows the minus sign is not listed in options, getopts stores a question mark inside variable before returning with a zero exit status. It also writes an error message to standard error. If there are no more arguments left on the command line or if the next argument doesn’t begin with a minus sign, getopts return a nonzero exit status.

Compunet Corporation The getopts Command Suppose you want getopts to recognize the options –a, -i, and –r for a command called foo. Your getopts call might look like this: getopts air option