Introduction to UNIX Part II

Similar documents
Introduction to Unix. Rob Funk

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

Review of Fundamentals

Vi & Shell Scripting

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

22-Sep CSCI 2132 Software Development Lecture 8: Shells, Processes, and Job Control. Faculty of Computer Science, Dalhousie University

Shells. A shell is a command line interpreter that is the interface between the user and the OS. The shell:

CST Algonquin College 2

5/20/2007. Touring Essential Programs

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Essential Unix (and Linux) for the Oracle DBA. Revision no.: PPT/2K403/02

Processes and Shells

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

Basics. I think that the later is better.

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

Basic Linux (Bash) Commands

Shells and Shell Programming

Assignment clarifications

Editors in Unix come in two general flavours:

Chapter 9. Shell and Kernel

Shell Scripting. With Applications to HPC. Edmund Sumbar Copyright 2007 University of Alberta. All rights reserved

CSCI 2132 Software Development. Lecture 3: Unix Shells and Other Basic Concepts

5/8/2012. Specifying Instructions to the Shell Chapter 8

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell.

Shells and Shell Programming

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

System Programming. Unix Shells

5/8/2012. Exploring Utilities Chapter 5

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

EECS2301. Lab 1 Winter 2016

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

Introduction to the Shell

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018

UNIX Shell Programming

Consider the following program.

Unix Shell Environments. February 23rd, 2004 Class Meeting 6

Linux Command Line Interface. December 27, 2017

Shell Programming Systems Skills in C and Unix

CHE3935. Lecture 1. Introduction to Linux

Module 8 Pipes, Redirection and REGEX

Sub-Topic 1: Quoting. Topic 2: More Shell Skills. Sub-Topic 2: Shell Variables. Referring to Shell Variables: More

Software I: Utilities and Internals. What is vi?

Bourne Shell Reference

Topic 2: More Shell Skills

Standard. Shells. tcsh. A shell script is a file that contains shell commands that perform a useful function. It is also known as shell program.

Shell scripting and system variables. HORT Lecture 5 Instructor: Kranthi Varala

bash, part 3 Chris GauthierDickey

SIDDHARTH GROUP OF INSTITUTIONS :: PUTTUR Siddharth Nagar, Narayanavanam Road QUESTION BANK (DESCRIPTIVE) UNIT I

Introduction to the shell Part II

COMP 4/6262: Programming UNIX

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Introduction: What is Unix?

CSE Linux VM. For Microsoft Windows. Based on opensuse Leap 42.2

9.2 Linux Essentials Exam Objectives

Unix Processes. What is a Process?

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Useful Unix Commands Cheat Sheet

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Shell Start-up and Configuration Files

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Linux shell programming for Raspberry Pi Users - 2

A shell can be used in one of two ways:

Creating a Shell or Command Interperter Program CSCI411 Lab

Linux shell scripting Getting started *

Perl and R Scripting for Biologists

UNIX. The Very 10 Short Howto for beginners. Soon-Hyung Yook. March 27, Soon-Hyung Yook UNIX March 27, / 29

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Std: XI CHAPTER-3 LINUX

Introduction to Linux

Today. Review. Unix as an OS case study Intro to Shell Scripting. What is an Operating System? What are its goals? How do we evaluate it?

Shell Programming for System Administrators Student Guide

Topic 2: More Shell Skills. Sub-Topic 1: Quoting. Sub-Topic 2: Shell Variables. Difference Between Single & Double Quotes

UNIX shell scripting

Command-line interpreters

CS395T: Introduction to Scientific and Technical Computing

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

The Command Line. Matthew Bender. Friday 18 th September, CMSC Command Line Workshop

Topic 2: More Shell Skills

/smlcodes /smlcodes /smlcodes. Shell Scripting TUTORIAL. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation

Unix Shells and Other Basic Concepts

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

More on Unix Shell SEEM

Common File System Commands

Operating Systems. Copyleft 2005, Binnur Kurt

Operating Systems 3. Operating Systems. Content. What is an Operating System? What is an Operating System? Resource Abstraction and Sharing

UNIX System Programming Lecture 3: BASH Programming

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

Last Time. on the website

Why Bourne Shell? A Bourne Shell Script. The UNIX Shell. Ken Wong Washington University. The Bourne Shell (CSE 422S)

UNIX and Linux Essentials Student Guide

Today. Operating System Evolution. CSCI 4061 Introduction to Operating Systems. Gen 1: Mono-programming ( ) OS Evolution Unix Overview

Short Read Sequencing Analysis Workshop

UNIX COMMANDS AND SHELLS. UNIX Programming 2015 Fall by Euiseong Seo

Input/Output (I/O) Re-direction

Linux Fundamentals (L-120)

UNIX Quick Reference

Transcription:

T H E U N I V E R S I T Y of T E X A S H E A L T H S C I E N C E C E N T E R A T H O U S T O N S C H O O L of H E A L T H I N F O R M A T I O N S C I E N C E S Introduction to UNIX Part II For students of HI 6327 Biomolecular Modeling Willy Wriggers, Ph.D. School of Health Information Sciences http://biomachina.org/courses/modeling/03.html

Class Objectives introduction to student accounts basic background in UNIX structure and features getting started directory navigation and control file maintenance and display commands shells text processing resources for future projects

Editing Files with vi Line Editor Three modes: Command mode ( beep mode ) Insert mode ( no beep mode ) Command line mode ( colon mode ) Commands are generally case sensitive

vi Cursor Movements arrow keys (depending on terminal) h, j, k, l alternates for arrows ˆF ˆB ˆD ˆU forward one screen back one screen down half screen up half screen and many more (man vi)

vi Deleting Text dd delete current line [n] dd delete [n] line(s) [n] x delete [n] characters [n] yy yank [n] line(s) to buffer [n] yw yank [n] word(s) to buffer

vi Inserting Text p P i a I A o O puts yanked or deleted text after cursor puts yanked or deleted text before cursor insert text before the cursor append text after the cursor insert text at beginning of line append text at end of line open new line after current line open new line before current line

vi Saving and Exiting Esc to reach colon mode :w write changes to file :wq write changes and quit :w! force overwrite of file :q quit if no changes made :q! quit without saving changes

Editing Files with emacs Visual editor Move around freely in buffer Self-explanatory menu system Customizable with.emacs configuration file Online help system ^H (control-h) Undo changes with ^_ (control-underscore) Windows version exists (see online course notes)

emacs Cursor Movements arrow keys (most terminals) Alternates: ˆF ˆB ˆN ˆP next character previous character next line previous line

emacs Cutting and Pasting Text Delete characters with Delete key Highlight regions of text with mouse (click and drag), or with ^space on either side Cut / delete a larger piece of text with ^W (wipe) Paste with ^Y (yank)

emacs Saving and Exiting ^X^S save buffer to file ^X^C exit emacs

Text Processing Commands grep / egrep / fgrep - search the argument for all occurences of the search string; list them awk / gawk - scan for patterns in a file and process the results sed - stream editor for editing files from script or command line The search strings can be generalized to regular expressions

Regular Expressions allow pattern matching on text combine normal and special characters (metacharacters) should not be confused with wildcards for matching files

Regular Expression Syntax Regular expressions come in three different forms: Anchors tie the pattern to a location on the line Character sets match a single character at a single position Modifiers specify how many times to repeat the previous expression Regular Expressions can be combined to form longer regular expressions.

Regular Expression Syntax. match any single character except newline * match zero or more instances of single expression preceding it [abc] [a-d] match any of the characters enclosed match any character in enclosed range

Regular Expression Syntax [ˆabc] match any character NOT in the enclosed set ˆexp regular expression must start at the beginning of the line exp$ regular expression must end at the end of the line \ treat the next character literally

grep [options] regexp [files...] grep The grep utility is used to search for regular expressions in UNIX files. fgrep searches for exact strings. egrep uses extended regular expressions. Some options for grep are: -i ignore case -v display only lines that dont match -n display line number with the line where match was found

gawk gawk is the open source implementation of the awk pattern scanning and text processing language. Awk is a full-featured programming language, beyond the scope of class (see O Reilly book sed & awk ). Often, gawk is used to extract fields from data files: cat file1 gawk {print $1, $3 } > file2 pipeline print fields #1 and #3 comma delimited fields (if not white space) redirect

sed [options] {script} [file] sed The sed utility is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). Most often, sed is used to replace strings: cat file1 sed s/regexp/replacement/g > file2 pipeline string operation replace all occurrenes in line redirect

Questions: Text and String Editing

The Shell The shell sits between you and the OS acts as a command interpreter reads input translates commands into actions display prompt read command execute command interpret command

Bourne Shell (sh) good features for I/O control - often used for scripts awkward Old School syntax not well suited for interactive users default prompt is $

C Shell (csh) uses C-like syntax for scripting I/O more awkward than Bourne shell somewhat nicer for interactive use job control history default prompt is % uses ~ symbol to indicate a home directory (user s or others )

T-C Shell (tcsh) backward compatible with C-shell commands and scripts enhanced for interactive use file name completion command line editing (with left/right arrows) command history (with up/down arrows)

Bourne Again Shell (bash) backward compatible with Bourne shell commands and scripts has some features from C-shell as well (~, history) enhanced for interactive use file name completion command line editing (with left/right arrows) command history (with up/down arrows)

Which Shell to Use? bash or tcsh: easy to fix typos or redo previous commands. most built-in commands are nowadays available in both personal programming preference and history easy to switch between shells: e.g. Cygwin and Linux have bash by default, but by calling tcsh in.profile (bash startup file) we can start with tcsh terminal

Environment Variables DISPLAY EDITOR PATH TERM (list with env) csh/tcsh sh/bash setenv NAME value NAME=value; export NAME

Shell Variables PS1 (sh/bash) prompt (csh/tcsh) others as needed (list with set) csh/tcsh set name=value sh/bash name=value These are used by the shell and shell scripts; not seen or used by external programs

Shell Startup The files.profile (sh) or.bash_profile (bash) or.login (csh/tcsh) are used at login to: set path (search path for executables) define functions set terminal parameters (stty) set terminal type set default file permissions (umask)

Sample.profile or.bash_profile PATH=/usr/bin:/usr/ucb:/usr/local/bin:. export PATH PS1="{ hostname whoami } " ls() { /bin/ls -sbf "$@"; } ll() { ls -al "$@"; } stty erase ^H eval tset -Q -s -m :?xterm umask 077

Startup Files sh/csh Family Shells File.login.cshrc.tcshrc.logout.profile.bash_profile.bash_logout.bashrc Shell csh, tcsh csh, tcsh tcsh csh, tcsh sh, bash bash bash bash Comments Read only for login shells Read for each new shell Tcsh ignores.cshrc if.tcshrc exists Executed at logout Bash ignores.profile if.bash_profile exists. Executed at logout. Used if shell reads /etc/bashrc if it exists

Customizing Start-Up Files Files are placed in your home directory Define your favorite prompt, shell parameters and more Much information is available online: Search for customize bash at google

Shell History tcsh and bash retain information about former commands executed within the shell Customize variables to set number of commands retained: E.g. in.profile: HISTSIZE=1000 History saved in ~/.history or ~/.bash_history between logins.

History Shortcuts $ history nn prints last nn commands $!! repeats the last command $!nn repeats the command numbered nn $!string repeats latest command starting with string

Input and Output I/O redirection and piping in UNIX: output redirection to a file input redirection from a file piping (pipelining): output of one command becomes the input of a subsequent command

Standard File Descriptors stdin Standard input to the program stdout Standard output from the program stderr Standard error output These are not called by name at shell prompt, but are often referenced by these names.

File Descriptors stdin normally from the keyboard, but can redirect from a file or command stdout & stderr normally to the terminal screen, but can redirect either or both to a file or command

I/O Redirection > redirect standard output to file command > outfile >> append standard output to file command >> outfile < input redirection from file command < infile pipe output to another command command1 command2

csh/tcsh Advanced Redirection >& file redirect stdout and stderr to file >>& file append stdout and stderr to file & command pipe stdout and stderr to command To redirect stdout and stderr to separate files: % (command > outfile) >& errfile

sh/bash Advanced Redirection 2>file >file 2>&1 >>file 2>&1 2>&1 command direct stderr to file direct both stdout and stderr to file append both stdout and stderr to file pipe stdout and stderr to command To redirect stdout and stderr to two separate files: $ command > outfile 2 > errfile To discard stderr: $ command 2 > /dev/null (/dev/null: UNIX black hole )

Special Command Symbols ; command separator & && run the command in the background run the following command only if previous command completes successfully run the following command only if previous command did not complete successfully ( ) grouping commands within parentheses are executed in a subshell

Quoting \ escape the following character (take it literally) don t allow any special meaning to characters within single quotes (except! in csh) allow variable and command substitution inside double quotes (does not disable $ and \ within the string) take the output of command in backquotes and substitute it into the command line (works inside double-quotes)

Shell Scripts Similar to DOS batch files Quick and simple programming Text file, interpreted by shell, effectively new command List of shell commands to be run sequentially Set execute permission, no special extension necessary Best way to learn: Look at examples

First Line Include full path to interpreter (shell) #!/bin/sh #!/bin/csh -f csh / tcsh follows C syntax, see man pages, books, and WWW In following only brief intro to sh/bash

Special sh/bash Variables $# Number of arguments on command line $0 Name that script was called as $1 $9 Command line arguments $@ All arguments (separately quoted) $* All arguments $? Numeric result code of previous command $$ Process ID of this running script

I/O (sh/bash) echo output text Talk to user (or ask questions) read variable Get input from user, put it in variable

test and [] if [... ]; then fi... Control Flow (sh/bash) case $variable in... esac for variable in... do... done Check sh/bash man page for details, also look at examples.

Questions: Shells and Shell Programming

Resources UNIX man pages WWW: http://www.utexas.edu/cc/docs/ccug1 http://www.ee.surrey.ac.uk www.ee.surrey.ac.uk/teaching/unix http://www.ee.surrey.ac.uk/docs/unixhelp O Reilly UNIX and Linux Books: http://unix.oreilly.com

Figure and Text Credits Text and figures for this lecture were adapted in part from the following source, in agreement with the listed copyright statements: http://wks.uts.ohio-state.edu/unix_course 1996 University Technology Services, The Ohio State University, Baker Systems Engineering Building, 1971 Neil Avenue, Columbus, OH 43210. All rights reserved. Redistribution and use, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions must retain the above copyright notice, this list of conditions, and the following disclaimer. 2. Neither the name of the University nor the names of its contributors may be used to endorse or promote products or services derived from this document without specific prior written permission. THIS PUBLICATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. THIS PUBLICATION MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS.