Chapter Four I/O Redirection1 System Programming Shell Operators.

Slides:



Advertisements
Similar presentations
UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
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.
Introduction to UNIX CSE 2031 Fall May 2015.
1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin
Shell Basics CS465 - Unix. Shell Basics Shells provide: –Command interpretation –Multiple commands on a single line –Expansion of wildcard filenames –Redirection.
Guide To UNIX Using Linux Third Edition
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.
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
UNIX Filters.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Chapter 4: UNIX File Processing Input and Output.
BILKENT UNIVERSITY DEPARTMENT OF COMPUTER TECHNOLOGY AND INFORMATION SYSTEMS CTIS156 INFORMATION TECHNOLOGIES II CHAPTER 10: ADVANCED FILE PROCESSING.
Sydney Opera House. Week Three Agenda Administrative Issues Link of the week Review week two lab assignment This week’s expected outcomes Next lab assignment.
3 File Processing Mauro Jaskelioff. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash.
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Chapter Four UNIX File Processing. 2 Lesson A Extracting Information from Files.
CIT 140: Introduction to ITSlide #1 CSC 140: Introduction to IT I/O Redirection.
Guide To UNIX Using Linux Fourth Edition
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.
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.
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
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
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
1 Operating Systems Lecture 2 UNIX and Shell Scripts.
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.
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.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
UNIX shell environments CS 2204 Class meeting 4 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
Basic of UNIX For fresh members of SPARCS
Agenda Basic Unix Commands (Chapters 2 & 3) Miscellaneous Commands: which, passwd, date, ps / kill Working with Files: file, touch, cat, more, less, grep,
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Jessica Miller & Ruth Anderson
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Learning Unix/Linux Based on slides from: Eric Bishop.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some of the.
CIRC Summer School 2016 Baowei Liu
Exploring Shell Commands, Streams, and Redirection
Lesson 5-Exploring Utilities
CIRC Summer School 2017 Baowei Liu
Linux Commands Help HANDS ON TRAINING Author: Muhammad Laique
CIRC Winter Boot Camp 2017 Baowei Liu
Some Linux Commands.
Chapter 6 Filters.
Exploring Shell Commands, Streams, and Redirection
CSE 303 Concepts and Tools for Software Development
Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Chapter Four UNIX File Processing.
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
Linux Shell Script Programming
Lecture 4 Redirecting standard I/O & Pipes
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
LPI Linux Certification
Presentation transcript:

Chapter Four I/O Redirection1 System Programming Shell Operators

Chapter Four I/O Redirection2 Three Standard Files stdin – standard input input character stream Defaults to keyboard stdout – standard output output character stream Defaults to terminal stderr – standard error receives error messages Defaults to terminal

Chapter Four I/O Redirection3 Redirecting stdout Instead of sending stdout to the terminal, you can tell a program to write to a file > filename : redirect stdout to a file file is created if it does not exist file is zeroed if it does >> filename : append stdout to an existing file Examples: man ls > ls_help.txt echo $PWD > current_directory cat file1 >> file2

Chapter Four I/O Redirection4 Redirecting stdin Instead of reading from a terminal, you can tell a program to read from a file < filename : redirect stdin from an existing file << word : redirect stdin from lines that follow up until a line containing just word Parameter substitution, back-quoted commands and backslash character on the lines are interpreted Examples: mail < message.txt at 3am < cmds or at 0945 < cmds sort sorted_friends cat << end

Chapter Four I/O Redirection5 Standard File Descriptors A file can be associated with a file descriptor The shell associates three standard files with three standard file descriptors for every command respectively 0 : standard input (STDIN) 1 : standard output (STDOUT) 2 : standard error (STDERR) Standart descriptors are associated with the user's terminal, but they can also be redirected into other files.

Chapter Four I/O Redirection6 File Descriptor Creation To open a file for writing, use one of these exec n>filename exec n>>filename where n is an integer, and filename is the name of the file opened for writing. The first form overwrites the specified filename if it exists. The second form appends to the specified filename. To open a file for reading, use exec n<filename To open a file for both reading and writing, use exec n<>filename

Chapter Four I/O Redirection7 Redirection with File Descriptors To redirect standard output to the file associated with file descriptor n, use command >&n To redirect standard input from the file associated with file descriptor n, use command <&n exec n>&–: output file descriptor n is closed exec 1>&–, exec >&– : standard output is closed exec n<&–: input file descriptor n is closed exec 0<&–, exec <&– : standard input is closed

Chapter Four I/O Redirection8 Redirection with File Descriptors Writing to a file exec 4> file# Open "file", assigning fd 4 to it. ls >&4# Write ls output to "file" Reading from a file exec 5< file# Open "file", assigning fd 4 to it. wc <&5# Read input from “file" Writing at a specified place in a file echo > file# Write string to "file". exec 3<> file # Open "file", assigning fd 3 to it. read -n 4 <&3 # Read only 4 characters. echo -n. >&3 # Write a decimal point there. exec 3>&- # Close fd 3. cat file # ==>

Chapter Four I/O Redirection9 General Input/Output Redirection The standard output or input redirection is explicitly indicated in the general form as command 1> file command 1>> file command 0< file The basic syntax to redirect STDOUT and STDERR to separate files is command 1> fileA 2> fileB the STDOUT of the specified command is redirected to fileA, and the STDERR (error messages) is redirected to fileB.

Chapter Four I/O Redirection10 Redirection To Separate Files The append operator can be also used. command >> fileA 2> fileB command > fileA 2>> fileB command >> fileA 2>> fileB The first form appends STDOUT to fileA and redirects STDERR to fileB. The second form redirects STDOUT to fileA and appends STDERR to fileB. The third form appends STDOUT to fileA and appends STDERR to fileB.

Chapter Four I/O Redirection11 Redirection To A Single File The basic syntax to redirect or append STDOUT and STDERR to the same file is command > file 2>&1 or command &> file command >> file 2>&1 STDOUT (file descriptor 1) and STDERR (file descriptor 2) are redirected or appended into the specified file. command 2>&1 >> file (compare this to above) m>&n: file descriptors m is redirected to n >&n: standart output is redirected to file descriptor n Example rm -rf /tmp/my_tmp_dir > /dev/null 2>&1 rdate ntp.nasa.gov >> /var/log/rdate.log 2>&1 if [ ! -f $FILE ]; then echo "$FILE is not a file" >&2; fi

Chapter Four I/O Redirection12 Pipes Pipe ( | ) : connect stdout of one command to stdin of another Examples ls –la | less ls –al | wc ls-al | sort +4r cat file | wc man bash | grep " history “ ps aux | grep user1 | wc –l who | sort > current_users

Chapter Four I/O Redirection13 Processes Run more than one program at a time Separate commands with a semicolon ( ; ) date ; who Run more than one program simultaneously Use an ampersand ( & ) at the end of a command ls –al & wc *

Chapter Four I/O Redirection14 Filters Filter – a program that takes input and transforms it in some way wc – gives a count of lines/words/characters grep – searches for lines with a given pattern grep (pattern can be RE) sort – sorts lines alphabetically or numerically sort -r : reverse normal order of sorting sort -n : sort in numeric order sort +2n : sort items in the second column cut – select parts of each line to send to stdout cut -c1-5: select the first 5 characters of each line cut -c1,5: select the first and fifth chars of each line cut -d: -f1,3 /etc/passwd : map user names to IDs

Chapter Four I/O Redirection15 Filters (cont.) head – display first few lines of files head -n n: an integer tail – display the last part of a file tail -n : the last n lines tail +n : lines after the nth line diff – report on all the lines that are different cmp – find the first place where two files differ od – display octal representation of a file e.g. od –c : visual representation of all bytes ls –lt : list file in time order crypt – encode or decode a file e.g. crypt key encrypted.file

Chapter Four I/O Redirection16 Filters (cont.) tr – transliterate the characters in its input tr "[:lower:]" "[:upper:]" - map lower case to upper uniq – report or filter out repeated lines in a file uniq –d - display lines repeated in uniq –u - display lines not repeated in uniq –c - display with number of lines repeated pr – print files in various forms ls -a | pr -n -h $(pwd) - print a numbered list of all files in the current directory What does the following command do? cat * | tr -sc A-Za-z '\012' | sort | uniq –c | sort –n | tail | pr -5 –t

Chapter Four I/O Redirection17 More Commands: Communication talk – interactive chat with another user e.g. talk smith pts/2 write – send message to another user e.g. write smith pts/2 mesg [n|y]– permit/deny messages mail, pine – text- based program ftp, sftp – text-based FTP program telnet, ssh – connect to other machines directly lynx – text-based web browser

Chapter Four I/O Redirection18 More commands: Processes ps – list current processes top – dynamic display of system’s utilization by processes kill – terminate a process (default: SIGTERM) kill –9 (sending SIGKILL signal) time – keep timing information for a process time ls(displaying real/user/sys time) wait – waiting for all procs initiated with & nohup – keep command running after logging out nice – keep command running with lower priority nohup/nice &

Chapter Four I/O Redirection19 More commands: File system file – determine file type file /bin/ed /bin/ed:pure executable A runnable program is marked by a binary “magic number” at its beginning. od /bin/ed du – tell how much disc space is consumed du ( in terms of disc blocks ) df – report space on the mounted file subsystems df –k ( in terms of disc blocks )( a block = 512 or 1024 bytes)