1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 2 Dictionary.com, "bash," in Dictionary.com Unabridged. Source location: Random House, Inc.
Advertisements

UNIX Chapter 12 Redirection and Piping Mr. Mohammad Smirat.
การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
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.
NETW-240 Shells Last Update Copyright Kenneth M. Chipps Ph.D. 1.
David Notkin Autumn 2009 CSE303 Lecture 3 Dictionary.com, “shell," in Dictionary.com Unabridged. Source location: Random House, Inc.
1 CSE 303 Lecture 4 users/groups; permissions; intro to shell scripting read Linux Pocket Guide pp , 25-27, 61-65, , 176 slides created by.
Linux+ Guide to Linux Certification, Second Edition
CSE 303 Lecture 2 Introduction to bash shell
Guide To UNIX Using Linux Third Edition
Introduction to Unix – CS 21 Lecture 5. Lecture Overview Lab Review Useful commands that will illustrate today’s lecture Streams of input and output File.
Unix Files, IO Plumbing and Filters The file system and pathnames Files with more than one link Shell wildcards Characters special to the shell Pipes and.
1 UNIX essentials (hands-on) the directory tree running programs the shell (using the T-shell) → command line processing → special characters → command.
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.
"Two of the most famous products of Berkeley are LSD and Unix. I don't think that this is a coincidence.” Anonymous.
COMP1070/2002/lec4/H.Melikian COMP1070 Lecture #5  Files and directories in UNIX  Various types of files  File attributes  Notion of pathname  Commands.
Linux environment ● Graphical interface – X-window + window manager ● Text interface – terminal + shell.
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.
Guide To UNIX Using Linux Fourth Edition
BIF703 stdin, stdout, stderr Redirection. stdin, stdout, stderr Recall the Unix philosophy “do one thing well”. Unix has over one thousand commands (utilities)
– Introduction to the Shell 10/1/2015 Introduction to the Shell – Session Introduction to the Shell – Session 2 · Permissions · Users.
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+ Guide to Linux Certification, Third Edition
The Shell Chapter 7. Overview The Command Line Standard IO Redirection Pipes Running a Program in the Background Killing (a process!)
1 UNIX essentials (hands-on) the directory tree running programs the shell → command line processing → special characters → command types → shell variables.
System Administration Introduction to Unix Session 2 – Fri 02 Nov 2007 Reference:  chapter 1, The Unix Programming Environment, Kernighan & Pike, ISBN.
Week 3 Exploring Linux Filesystems. Objectives  Understand and navigate the Linux directory structure using relative and absolute pathnames  Describe.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
1 CSE 303 Lecture 3 bash shell continued: processes; multi-user systems; combining commands read Linux Pocket Guide pp , , , 118, 122,
Chapter Four I/O Redirection1 System Programming Shell Operators.
Week 9 - Nov 7, Week 9 Agenda I/O redirection I/O redirection pipe pipe tee tee.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 2a – A Unix Command Sampler (Courtesy of David Notkin, CSE 303)
Linux Commands C151 Multi-User Operating Systems.
Linux+ Guide to Linux Certification, Second Edition Chapter 4 Exploring Linux Filesystems.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
A Brief Overview of Unix Brandon Bohrer. Topics What is Unix? – Quick introduction Documentation – Where to get it, how to use it Text Editors – Know.
Lecture 1: Introduction, Basic UNIX Advanced Programming Techniques.
Agenda The Bourne Shell – Part I Redirection ( >, >>,
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.
File Management commands cat Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
Learning Unix/Linux Based on slides from: Eric Bishop.
Exploring Shell Commands, Streams, and Redirection
Linux 201 Training Module Linux Adv File Mgmt.
Some Linux Commands.
The Linux Operating System
stdin, stdout, stderr Redirection
Exploring Shell Commands, Streams, and Redirection
CSE 374 Programming Concepts & Tools
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
Exploring Shell Commands, Streams, Redirection, and Processes
CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection
CSE 303 Concepts and Tools for Software Development
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
A shell is a user interface.
Exploring Shell Commands, Streams, and Redirection
Exploring Shell Commands, Streams, and Redirection
Presentation transcript:

1 CSE 390a Lecture 2 Exploring Shell Commands, Streams, and Redirection slides created by Marty Stepp, modified by Josh Goodwin

2 Lecture summary Unix file system structure Commands for file manipulation, examination, searching Java compilation: using parameters, input, and streams Redirection and Pipes

3 Unix file system directorydescription / root directory that contains all others (drives do not have letters in Unix) /bin programs /dev hardware devices /etc system configuration files  /etc/passwd stores user info  /etc/shadow stores passwords /home users' home directories /media, /mnt,... drives and removable disks that have been "mounted" for use on this computer /proc currently running processes (programs) /tmp, /var temporary files /usr user-installed programs

4 Links hard link: Two names for the same file. $ ln foo bar  the above command links bar as a duplicate name for foo if one is modified, the other is too; follows file moves soft (symbolic) link: A reference to another existing file. $ ln -s foo bar  the above command creates a reference bar to the file foo bar can be used as though it were foo but if bar is deleted, foo will be unaffected commanddescription ln create a link to a file unlink remove a link to a file

5 File examination Let’s explore what we can do here… commanddescription cat output a file's contents on the console more or less output a file's contents, one page at a time head, tail output the first or last few lines of a file wc count words, characters, and lines in a file du report disk space used by a file(s) diff compare two files and report differences

6 Searching and sorting grep is actually a very powerful search tool; more later... Exercise : Given a text file students.txt, display the students arranged by the reverse alphabetical order of their last names. commanddescription grep search a file for a given string sort convert an input into a sorted output by lines uniq strip duplicate lines find search for files within a given directory locate search for files on the entire system which shows the complete path of a command

7 Keyboard shortcuts ^KEY means hold Ctrl and press KEY keydescription Up arrow repeat previous commands Home / End or ^A / ^E move to start/end of current line " quotes surround multi-word arguments and arguments containing special characters * "wildcard", matches any files; can be used as a prefix, suffix, or partial name Tab auto-completes a partially typed file/command name ^C or ^\ terminates the currently running process ^D end of input; used when a program is reading input from your keyboard and you are finished typing ^Z suspends (pauses) the currently running process ^S don't use this; hides all output until ^Q is pressed

8 Programming Exercise : Write/compile/run a program that prints "Hello, world!" $ javac Hello.java $ java Hello Hello, world! $ commanddescription javac ClassName.java compile a Java program java ClassName run a Java program python, perl, ruby, gcc, sml,... compile or run programs in various other languages

9 Programming Creating parameter input to programs  String[] args holds any provided parameters  Exercise: modify hello world to use parameters Parameters not the same as the input stream!  Exercise: modify hello world to also use a Scanner to grab input Let’s revisit the standard streams…

10 Streams in the Shell Stdin, stdout, stderr  These default to the console  Some commands that expect an input stream will thus read from the console if you don’t tell it otherwise. Example: grep hi  What happens? Why? We can change the default streams to something other than the console via redirection.

11 Output redirection command > filename  run command and write its output to filename instead of to console; think of it like an arrow going from the command to the file... if the file already exists, it will be overwritten (be careful) >> appends rather than overwriting, if the file already exists command > /dev/null suppresses the output of the command  Example: ls -l > myfiles.txt  Example: java Foo >> Foo_output.txt  Example: cat > somefile.txt (writes console input to the file until you press ^D )

12 Input redirection command < filename  run command and read its input from filename instead of console whenever the program prompts the user to enter input (such as reading from a Scanner in Java), it will instead read the input from a file some commands don't use this; they accept a file name as an argument  Example: java Guess < input.txt  Exercise: run hello world with the input stream as a file instead of the console  Exercise: Also change the output stream to write the results to file  again note that this affects user input, not parameters  useful with commands that can process standard input or files: e.g. grep, more, head, tail, wc, sort, uniq, write

13 Combining commands command1 | command2  run command1 and send its console output as input to command2  very similar to the following sequence: command1 > filename command2 < filename rm filename  Examples: diff students.txt names.txt | less grep Josh names.txt | uniq  Exercise : names.txt contains CSE student names, one per line, in "LASTNAME, FIRSTNAME" format. We are interested in students whose first names begin with "J", such as “Goodwin, Josh". Find out of how many such students are in the file. Then figure out how many total letters (including comma and spaces) are in the full name of the last student in alphabetical order from this group.

14 Misusing pipes and cat Why doesn't this work to compile all Java programs? ls *.java | javac Misuse of cat  bad: cat filename | command  good: command < filename  bad: cat filename | more  good: more filename  bad: command | cat  good: command

15 Commands in sequence command1 ; command2  run command1 and then command2 afterward (they are not linked) command1 && command2  run command1, and if it succeeds, runs command2 afterward  will not run command2 if any error occurs during the running of 1  Example: Make directory songs and move my files into it. mkdir songs && mv *.mp3 songs