Lecture 6: Using BASH Effectively

Similar documents
CS Unix Tools. Lecture 3 Making Bash Work For You Fall Hussam Abu-Libdeh based on slides by David Slater. September 13, 2010

CS Unix Tools & Scripting

CS Unix Tools & Scripting Lecture 3 Making Bash Work

CS Unix Tools & Scripting

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008

Lecture 3: The UNIX Style

Introduction to Unix

Review of Fundamentals

System Programming. Unix Shells

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

15-122: Principles of Imperative Computation

Exploring UNIX: Session 3

Linux Command Line Interface. December 27, 2017

COMP 4/6262: Programming UNIX

24 Writing Your First Script

Environment Variables

Operating System Interaction via bash

Using the Zoo Workstations

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

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

27-Sep CSCI 2132 Software Development Lab 4: Exploring bash and C Compilation. Faculty of Computer Science, Dalhousie University

Operating systems fundamentals - B02

Environment Variables

LAB #5 Intro to Linux and Python on ENGR

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

Shell Programming Overview

No Food or Drink in this room. Logon to Windows machine

Crash Course in Unix. For more info check out the Unix man pages -orhttp:// -or- Unix in a Nutshell (an O Reilly book).

Lecture # 2 Introduction to UNIX (Part 2)

COMS 6100 Class Notes 3

File Commands. Objectives

Lecture 5: Jobs and Processes

ardpower Documentation

CS 470 Operating Systems Spring 2013 Shell Project

Network softwarization Lab session 2: OS Virtualization Networking

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

Welcome to the Bash Workshop!

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

Chapter 4. Unix Tutorial. Unix Shell

CSE 374: Programming Concepts and Tools. Eric Mullen Spring 2017 Lecture 4: More Shell Scripts

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science

5/20/2007. Touring Essential Programs

System Programming. Session 6 Shell Scripting

Bash Programming. Student Workbook

Lab 4: Shell scripting

Operating Systems, Unix Files and Commands SEEM

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

CS Unix Tools & Scripting Lecture 7 Working with Stream

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

Introduction to UNIX Shell Exercises

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Getting to grips with Unix and the Linux family

THE FORMULAS TAB, CELL REFERENCING,THE VIEW TAB & WORKBOOK SECURITY THE FORMULAS TAB, CELL REFERENCING, THE VIEW TAB & WORKBOOK SECURITY OBJECTIVES

Linux File System and Basic Commands

Introduction to Linux. Fundamentals of Computer Science

Access Intermediate

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

A shell can be used in one of two ways:

More Linux Shell Scripts

Shells and Shell Programming

UNIX, GNU/Linux and simple tools for data manipulation

Goals for This Lecture:

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Shells and Shell Programming

Utilities. September 8, 2015

PHP and MySQL for Dynamic Web Sites. Intro Ed Crowley

No More Passwords (with SSH)

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Esercitazione Introduzione al linguaggio di shell

UNIX files searching, and other interrogation techniques

EECS2031 Winter Software Tools. Assignment 1 (15%): Shell Programming

CS155: Computer Security Spring Project #1

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

Introduction to Unix

Introduction to Unix: Fundamental Commands

Variables: Objects in R

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

Shells and Processes. Bryce Boe 2012/08/08 CS32, Summer 2012 B

Using Ruby and irb in CSE 341 Winter 2015

CS CS Tutorial 2 2 Winter 2018

2 Initialize a git repository on your machine, add a README file, commit and push

Today s Lecture. The Unix Shell. Unix Architecture (simplified) Lecture 3: Unix Shell, Pattern Matching, Regular Expressions

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

Part I. UNIX Workshop Series: Quick-Start

CS370 Operating Systems

An Introduction to Cluster Computing Using Newton

A Brief Introduction to Unix

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

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

Lecture 8. Introduction to Shell Programming. COP 3353 Introduction to UNIX

CSE II-Sem)

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

Introduction to Regular Expressions Version 1.3. Tom Sgouros

Shells & Shell Programming (Part B)

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSE 401/M501 18au Midterm Exam 11/2/18. Name ID #

Simplest version of DayOfYear

Chapter-3. Introduction to Unix: Fundamental Commands

CounterACT Macintosh/Linux Property Scanner Plugin

Transcription:

Lecture 6: Using BASH Effectively CS2042 - UNIX Tools October 10, 2008

Lecture Outline 1 2

What Else Is There? There have been many shells created over the years for UNIX environments: bash - default shell for OSX and most Linux machines csh - default shell for BSD-based systems zsh - possibly the most fully-featured shell A frighteningly thorough comparison of the features of many shells can be found here. Since bash is the gold standard of shells and has more than enough features for this course, we ll stick with it.

How do we use BASH? The servers we use for this class will automatically put us into csh, not bash. If you are already logged in to the server, just type bash. If you want the server to automatically put you into bash, you may want to add the following to /.login. Convert to bash from csh on start up if ( -f /bin/bash ) exec /bin/bash --login Note that /.login gets executed each time you log in to the server and csh starts up. Conversely, /.cshrc gets executed every time you enter the C-shell even if you were already logged in.

BASH and BASH is a full-fledged programming language in addition to a handy shell. If you wanted to, you could write a web server using BASH scripting. To get anything done in a programming language, you need support for variables. in BASH are preceded by a dollar sign ($). The contents of any variable can be listed using the echo command. Example: echo $SHELL /bin/bash

Environment Environment variables are generally used by the system to define aspects of operation. Most of these should not (or cannot) be changed by the user. $SHELL - which shell will be used by default $PATH - a list of directories to search for binaries $HOSTNAME - the hostname of the machine $HOME - current user s home directory...and many others which don t concern us

Local While we don t get much mileage out of many of our system s environment variables, BASH also allows us to define our own. Example: x=3 echo $x 3 We can also use export to define variables. Example: export seven=7 echo $seven 7

Lecture Outline 1 2

Tab Completion Did You Know? You can use the Tab key to auto-complete commands, parameters, and file and directory names. If there are multiple choices based on what you ve typed so far, BASH will list them. Try this at home!!

Modifying Your Prompt The environment variable $PS1 stores your default prompt. You can modify this variable to spruce up your prompt if you like. Example: First, echo $PS1 to see what its value is for now. \s-\v\$ (default) It consists mostly of backslash-escaped special characters, like \u. There are a whole bunch of options, all of which can be found online here.

Modifying Your Prompt, cont. Once you have a prompt you like, set your $PS1 variable. Define your prompt export PS1= <new prompt string> Type this line at the command prompt to temporarily change your prompt (good for testing) Add this line to /.bashrc or /.bash profiles to make the change permanent! Note: Parentheses must be used to invoke the \ characters. Some example BASH prompts PS1= \u-\h \w\$ mjm458-csug06 $ PS1= money\j\t money014:23:57

Lecture Outline 1 2

More Wildcards! Earlier we mentioned how useful wild card characters can be when looking for a particular file or trying to perform operations on a group of files. Let s take a closer look at wildcards which can: Match any string Match a single character Match a single restricted character Match a restricted range of characters

The String The * Wildcard * - Matches any string, including the null string (an empty string, nothing) Examples: Input Matched Not Matched lec* lecture1.pdf, lecture2.doc, lectures/ election data/ *.mp* foo.mp3, bar.mpeg,.mplayer/ mp3s/, tmp/ mi*r mirror, mir, minor, mine.rar mi, mine

The Character The? Wildcard? - Matches any single character (number, letter, punctuation!) Examples: Input Matched Not Matched lecture?.pdf lecture1.pdf, lecture2.pdf lecture12.pdf foo.mp? foo.mp3, foo.mp4, foo.mpg foo.mpeg, min? mine, mind, ming, mint, mink minute, min

The Character Range The [...] Wildcard [ ] - Matches any one of a list of comma-separated characters. A dash between two characters indicates a range to be matched. Examples: Input Matched Not Matched lecture[1,2].pdf lecture1.pdf, lecture2.pdf lecture5.pdf vacation[4-9].jpg vacation7.jpg, vacation9.jpg vacation3.jpg [a-z,a-z][0-9].gif a8.gif, M4.gif, Z0.gif ay3.gif, 8a.gif

Putting Them Together These wildcards are handy individually, but by using them in combination with each other, they become very powerful. Examples: Input Matched Not Matched *i[a-z]e* gift ideas, profile.doc, notice driver.exe [b,f][a,o][r,o].mp? foo.mp3, bar.mp4, for.mpg foo.mpeg *min[a-z]y minty, pepperminty, mindy minutely, hominy

Lecture Outline 1 2

What is the Problem? There are a few problems with your basic BASH session. Some of these you may even have encountered already: Your session isn t preserved if you close your ssh connection It s a pain to switch back and forth between files/the prompt Sometimes using two or three shells at once would be really convenient! All of these complaints can be resolved by using screen.

Intro to The screen Command screen - a screen manager with terminal emulation (Lets you do all that cool stuff from the last slide!) Generally screen can be used just as you would normally use a terminal window. However, special commands can be used to allow you to save your session, create extra shells, or split the window into multiple independent panes. Passing Commands to screen Each screen command consists of CTRL-a (hereafter referred to as C-a) followed by another character (case-sensitive!).

in Action A screenshot of a screen terminal:

Detaching/Reattaching Detach a screen C-a d Detaches the current screen session, allowing you to resume it later from a different location without losing your work! Resume a screen screen -r [pid.tty.host] Resumes a detached screen session screen -x [pid.tty.host] Attach to a non-detached screen session If you have only one screen, the [pid.tty.host] string is unnecessary.

Identifying Sessions Listing screen -ls or screen -list Lists your screen sessions and their statuses These screen sessions are the [pid.tty.host] strings required for resuming! Resuming a If screen -ls returns 9951.pts-2.fuzz (Detached)... screen -r 9951.pts-2.fuzz will resume our screen

Lecture Outline 1 2

Creating More Shells Create a New Shell Window C-a c Creates a new shell in a new window and switches to it Useful for opening multiple shells in a single terminal Concept is similar to tabbed browsing/tabbed IMs But how do we switch between windows? (hint: every window is numbered by order of creation) Window Selection C-a 0 - Switch to window 0 C-a 9 - Switch to window 9

Splitting Split Computing C-a S - splits your terminal area into multiple panes C-a tab - changes the input focus to the next pane The S is case-sensitive! Each split results in a blank pane Use C-a c to create a new shell in a pane Use C-a <num> to move an existing window to a pane Note: When you reattach a split screen, the split view will be gone. Just re-split the view, then switch between panes and reopen the other windows in each with C-a <num>