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.

Slides:



Advertisements
Similar presentations
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
Advertisements

1 The Shell and some useful administrative Unix Commands How Unix works along with some additional, useful administrative Unix commands you might need.
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
BIF703 Redirection Continued: Pipes. Redirection Recall from the previous slides we defined stdin, stdout, and stderr and we learned how to redirect these.
Guide To UNIX Using Linux Third Edition
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.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Shell Control Structures CSE 2031 Fall August 2015.
1 Bourne Shell Programming l Web Sources l Bourne Shell Tutorials: l l
Introduction to UNIX / Linux - 11
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Agenda Basic Shell Operations Standard Input / Output / Error Redirection of Standard Input / Output / Error ( >, >>,
CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT I/O Redirection.
Guide To UNIX Using Linux Fourth Edition
Chapter 5 Bourne Shells Scripts By C. Shing ITEC Dept Radford University.
BIF703 stdin, stdout, stderr Redirection. stdin, stdout, stderr Recall the Unix philosophy “do one thing well”. Unix has over one thousand commands (utilities)
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 8 Shell.
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.
Linux Shell Programming Tutorial 3 ENGR 3950U / CSCI 3020U Operating Systems Instructor: Dr. Kamran Sartipi.
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Linux+ Guide to Linux Certification, Third Edition
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
LINUX programming 1. INDEX UNIT-III PPT SLIDES Srl. No. Module as per Session planner Lecture No. PPT Slide No. 1.Problem solving approaches in Unix,Using.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
1 © 2001 John Urrutia. All rights reserved. Chapter 10 using the Bourne Again Shell.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
Pipes and Redirection in Linux ASFA Programming III C. Yarbrough.
Lesson 2 1.Commands 2.Filename Substitution 3.I/O Redirection 4.Command Grouping 5.Shell Responisibilites Review of the Basics.
Unix/Linux cs3353. The Shell The shell is a program that acts as the interface between the user and the kernel. –The shell is fully programmable and will.
Agenda  Redirection: Purpose Redirection Facts How to redirecting stdin, stdout, stderr in a program  Pipes: Using Pipes Named Pipes.
I/O and Redirection. Standard I/O u Standard Output (stdout) –default place to which programs write u Standard Input (stdin) –default place from which.
1 Unix Seminar #1 T.J. Borrelli Lecturer for CS and NSSA February 6th, 2009.
Chapter Four I/O Redirection1 System Programming Shell Operators.
Operating Systems Lecture 10. Agenda for Today Review of previous lecture Input, output, and error redirection in UNIX/Linux FIFOs in UNIX/Linux Use of.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
Shell Control Statements and More
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.
Linux+ Guide to Linux Certification, Second Edition
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.
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) Round and Round By Tariq Ibn Aziz Dammam Community College.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
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.
Shell Control Structures
CIRC Summer School 2017 Baowei Liu
Part 1: Basic Commands/Utilities
Some Linux Commands.
Lecture 9 Shell Programming – Command substitution
Shell Programming (ch 10)
stdin, stdout, stderr Redirection
CSE 303 Concepts and Tools for Software Development
File redirection ls > out
Introduction to Computer Organization & Systems
Linux Shell Script Programming
Lecture 4 Redirecting standard I/O & Pipes
Shell Control Structures
Shell Control Structures
Chapter 5 Bourne Shells Scripts
Chapter 3 The UNIX Shells
CSC 4630 Meeting 4 January 29, 2007.
Presentation transcript:

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 while read aline do count=`expr $count + 1` done < $filename echo "$filename has $count lines“ From the command line: $wc –l data.txt 11 data.txt $countLines1 data.txt Data.txt has 0 lines

3 Counting the number of lines in a file #!/bin/sh #countLines2 filename=$1#Should check if arguments are given count=0 Cat $filename | while read aline do count=`expr $count + 1` done echo "$filename has $count lines“ From the command line: $wc –l data.txt 11 data.txt $countLines2 data.txt Data.txt has 0 lines WHY? Subshell execution takes place if input is piped into a for, while, until, if or case command or if the output is piped out.

4 Counting the number of lines in a file Solution #!/bin/sh #countLines3 filename=$1#Should check if arguments are given count=0 exec < $filename #any commands that will read from stand in # will read from $filename while read aline do count=`expr $count + 1` done echo "$filename has $count lines“ From the command line: $countLines3 data.txt Data.txt has 11 lines exec < /dev/tty--- Reassign the standard input back to terminal

5 trap l The pressing of DELETE key at the terminal, when a program is in execution, sends a signal to the executing program. l Using the trap command, the program can specify the action to be taken on receiving the signal. l Usage: trap commands signals, where commands are the actions to be taken on receiving the signals. l Some Commonly used signal numbers l 0Exit from Shell l 1 Hangup l 2Interrupt (eg: Delete key) l 15Software termination (sent by kill, for example)

6 Example Example 1: #!/bin/sh i=1 JUNK=junkfile trap ‘rm $JUNK$$;exit’ 2 while [ $i -le 100 ] Do # remove the file when interrupt is received echo $i >> $JUNK$$ i=`expr $i + 1` done

7 Examples Example 2: trap 'echo Caught SIGINT - exiting; exit' 2 X=0 while :#loop forever do echo "X=$X" X=`expr ${X} + 1` sleep 1 Done Example 3 #!/bin/sh trap 'echo `pwd` $$ >>./errdir' while (true) do echo 'Hi' done

8 Example #!/bin/sh #What does this program do? trap 'increment' 2 increment() { echo "Caught SIGINT..." X=`expr ${X} + 500` if [ "${X}" -gt "2000" ] then echo "Okay, I'll quit..." exit 1 fi } ### main script X=0 while : do echo "X=$X" X=`expr ${X} + 1` sleep 1 done

9 Input and Output Redirection l We know that Unix shell allows us to redirect the input and output of programs using redirection (> and <) and piping(|). l When the Unix kernel starts any process, for example, grep, ls and so on, it sets up several places, called open files, for that process t read from and write to. Each of these files is given a number to identify with, called a file descriptor. l A file descriptor (also known as a file handle) is a non-negative digit that points at a file. l The file descriptors for stdin, stdout, and stderr are 0, 1, and 2, respectively. Any of these may be redirected to a file or to each other. l In other words, it is quite possible to send the output from stdin and stderr to the same file. This is quite useful when a user would rather check a script's results after it has completed processing. l By default, the file that is opened for stdin, stdout and sterr is /dev/tty (your terminal). l But, when the shell starts a process, you can tell the shell what file to connect to any of those file descriptors. l For example in the following command, grep aPattern somefile > output, the file descriptor 1 is connected to the file, output.

10 More On I/O l What if you want to send the standard out to screen and capture the standard error in a pipe or a file? l It is easy to redirect any file descriptor to any file. Eg: command 2>errorFile command 2> file – redirects the standard error from any command cd JUNK 2>>out #the directory JUNK does not exist cat out sh: JUNK: not found.

11 More On I/O l What if you want to send the standard out to screen and capture the standard error in a pipe or a file? l It is easy to redirect any file descriptor to any file. Eg: command 2>errorFile command 2> file – redirects the standard error from any command cd JUNK 2>>out #the directory JUNK does not exist cat out sh: JUNK: not found. l Let us take a look at a few cases: l Sending both standard output and errors to the pipe or backquotes. command 2>&1 |… or var=`command 2>&1` This means that send standard error (with file descriptor 2) to the same place standard output is going(down the pipe or backquotes)

12 More On I/O l Sending stderr go down a pipe and stdout to the screen. command 2>&1 1>&2|…Will Not Work We should use file descriptors 3 to 9,as holding places, to accomplish this. command 3>&2 2>&1 1>&3 |… or var=` command 3>&2 2>&1 1>&3`

13 More On I/O Example: For the following grep command, assume that afile contains one matching line for “unix” and bfile does not exist. var=`grep “unix” afile bfile` echo $var#What is the output? var=`grep “unix” afile bfile 3>&2` echo $var#What is the output? var=`grep “unix” afile bfile 3>&2 2>&1` echo $var#What is the output? var=`grep “unix” afile bfile 3>&2 2>&1 1>&3` echo $var#What is the output?