Topic 2: More Shell Skills

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

Topic 2: More Shell Skills

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

Review of Fundamentals

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Introduction: What is Unix?

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2017

CISC 220 fall 2011, set 1: Linux basics

bash, part 3 Chris GauthierDickey

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?

5/20/2007. Touring Essential Programs

CSE 303 Lecture 4. users/groups; permissions; intro to shell scripting. read Linux Pocket Guide pp , 25-27, 61-65, , 176

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

UNIX and Linux Essentials Student Guide

Introduction to Linux Workshop 1

CST Algonquin College 2

Introduction to Unix The Windows User perspective. Wes Frisby Kyle Horne Todd Johansen

Lab 3a Using the vi editor

Chapter 9. Shell and Kernel

Introduction to Bash Programming. Dr. Xiaolan Zhang Spring 2013 Dept. of Computer & Information Sciences Fordham University

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Part 1: Basic Commands/U3li3es

More Scripting Techniques Scripting Process Example Script

bash startup files Linux/Unix files stty Todd Kelley CST8207 Todd Kelley 1

Last Time. on the website

Common File System Commands

sottotitolo A.A. 2016/17 Federico Reghenzani, Alessandro Barenghi

Shell Start-up and Configuration Files

The Unix Shell & Shell Scripts

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

Bash Shell Programming Helps

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

Linux shell scripting Getting started *

Introduction to UNIX Part II

I/O and Shell Scripting

Prof. Navrati Saxena TA: R. Sachan

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

Principles of Bioinformatics. BIO540/STA569/CSI660 Fall 2010

EECS2301. Lab 1 Winter 2016

Unix Introduction to UNIX

UNIX Shell Programming

Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions?

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

PROGRAMMAZIONE I A.A. 2015/2016

Introduction to the Shell

Linux shell programming for Raspberry Pi Users - 2

Introduction to UNIX Shell Exercises

Environment Variables

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

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

Environment Variables

Linux & Shell Programming 2014

GNU/Linux 101. Casey McLaughlin. Research Computing Center Spring Workshop Series 2018

Unix Internal Assessment-2 solution. Ans:There are two ways of starting a job in the background with the shell s & operator and the nohup command.

Bourne Shell Reference

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

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

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

BASH SHELL SCRIPT 1- Introduction to Shell

bash Args, Signals, Functions Administrative Shell Scripting COMP2101 Fall 2018

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

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

Essential Linux Shell Commands

BIOINFORMATICS POST-DIPLOMA PROGRAM SUBJECT OUTLINE Subject Title: OPERATING SYSTEMS AND PROJECT MANAGEMENT Subject Code: BIF713 Subject Description:

Introduction to Linux Part 2b: basic scripting. Brett Milash and Wim Cardoen CHPC User Services 18 January, 2018

Basic UNIX commands. HORT Lab 2 Instructor: Kranthi Varala

Shell. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

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

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

Mills HPC Tutorial Series. Linux Basics II

CS 307: UNIX PROGRAMMING ENVIRONMENT KATAS FOR EXAM 2

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

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

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?

Getting your department account

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

UNIX System Programming Lecture 3: BASH Programming

Linux Training. for New Users of Cluster. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

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

Lab 2: Linux/Unix shell

(a) About Unix. History

Perl and R Scripting for Biologists

CMPS 12A Introduction to Programming Lab Assignment 7

Bash Check If Command Line Parameter Exists

Shells and Shell Programming

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

3/8/2017. Unix/Linux Introduction. In this part, we introduce. What does an OS do? Examples

Assignment clarifications

Shells and Shell Programming

CS CS Tutorial 2 2 Winter 2018

Exploring UNIX: Session 3

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.

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

Chap2: Operating-System Structures

System Programming. Unix Shells

Bash command shell language interpreter

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Transcription:

Topic 2: More Shell Skills Sub-topics: simple shell scripts (no ifs or loops yet) sub-shells quoting shell variables aliases bash initialization files I/O redirection & pipes text file formats 1

Reading For This Topic Reading: Custom Course Notes sections 4.2 through 4.6 2

Sub-Topic 1: Quoting Useful command for demonstrations: echo Prints all its arguments Normal rules: spaces separate words in commands some characters have special meanings to bash Sometimes useful to quote strings or characters. Three quoting mechanisms: 'string': every character in the string taken literally "string": most characters taken literally \c: character taken literally 3

Difference Between Single & Double Quotes A few special characters are interpreted by double quotes but not single quotes 1.!: history expansion 2. $: shell variables (next sub-topic) 3. back quotes (`) next week 4

Sub-Topic 2: Shell Variables shell variables: a way to store information in bash To give a value to a variable: today=monday If no "today" variables exists, creates it. Sets value to the string "Monday" To refer to value of variable: use $. Example: echo $today 5

Uses of Shell Variables 1. Use as variables in shell scripts 2. Store settings for shell and other programs. To see all variables with values: set 6

Referring to Shell Variables: More Awkward situation: ------$ object=computer ------$ echo I have two $objects I have two ------$ Why? Fix: ------$ echo I have two ${object}s I have two computers 7

Shell Variables and Quotes The $ character retains its special meaning inside double quotes. echo "Today is $today" Not inside single quotes. 8

Shell Variables Are Temporary Changes & new variables only last for life of current shell. To make a permanent change: use an initialization file (soon!) 9

PATH $PATH Contains list of directories separated by ":"s. When you type ls Bash looks for a command named "ls". Searching order: 1. Is it a built-in shell command? 2. In each directory in $PATH: is there a program called "ls"? 10

PS1 $PS1 is your bash prompt. Can contain literal characters plus some special ones: \d: the current date \h: the name of the host machine \j: number of jobs you have running \s: the name of the shell \u: name of user \w: current working directory \!: history number of this command 11

Sub-Topic 3: Sub-Shells A shell is a program. Linux automatically runs the bash program when you open a terminal. You can run it too. One reason: experiment with settings. Command to start a sub-shell: bash Exit back to starting shell: exit Use Shell variable $SHLVL to see if you're in a sub-shell A sub-shell "inherits" many of your settings, including current folder, umask Does not automatically inherit variables or aliases export command: says variable should be inherited by sub-shells export myvar 12

Sub-Topic 4: Simple Shell Scripts shell script = file containing bash commands Comments in scripts: # means rest of line is a comment Special variable names: $0: name of command $#: number of arguments $1,$2,...: the individual arguments $*: all the arguments in one string, separated by spaces 13

Executing a Shell Script First step: must be executable (chmod) Type command name alone: bash looks in your PATH directories Type command name with directory: no lookup needed 14

Digression #1: changing file permissions Shell command for changing permissions of a file or files: chmod chmod <users>+<permissions> <filenames> chmod <users>-<permissions> <filenames> chmod <users>=<permissions> <filenames> Users can be: u (user the person running chmod) g (the current group of the user) o (other people) or a combination Permissions can be: r (read) w (write) x (execute) or a combination Examples: chmod u+x myprogram chmod g-w somefile chmod o=rx otherfile chmod go= hiddenfile chmod ug+wr draft 15

Digression #2: default file permissions "umask" = "user mask" default file permissions Specifies permissions for new files you create A property of your bash session reset when you log off & back in. umask command: lets you view or change your umask umask with no args: displays in octal format umask -S: displays in symbolic format Format is like chmod: umask u+x file1 umask g=rx file2 umask o-w file3 16

Complication: scripts & sub-shells When you type the name of a script as a command: executed in a sub-shell. Consequence: can't use to create aliases or set variables, umask, etc. If you want script to affect current shell: source myscript Executes commands from myscript in the current shell. 17

#!/bin/bash Special Initial Comment At beginning of script, specifies this is a script to be run with bash. Why bother? Syntax of some commands different for every shell. Now your script will work for people using other shells. Not required for 220 assignments, but generally a good idea. 18

Alias = easy way to create synonym for a command provides shorthand for something you do often alias ll="ls l" Also a way to turn on certain options always. Example from standard.bashrc file: alias rm="rm -i" To execute original command (without alias): quote the name "rm" file1 file2 To cancel an alias: unalias rm To see all your aliases: alias (no arguments) Sub-Topic 5: Aliases

Sub-Topic 6: Bash Initialization Files In your home directory (probably!):.bash_profile: executed when you start a login shell.bashrc: executed when you start up any other shell Use these files for your own settings (variables, etc.) Advice: before you change, make a copy! don't remove standard files from path /cas/course/cisc220 contains sample files (do ls -a to make sure you see them!) 20

Sub-Topic 7: I/O Redirection & Pipes Three standard "streams" associated with any command or program: 0: standard input (default is keyboard) 1: standard output (default is screen) 2: error output (for error messages, default is screen) I/O redirection can change settings for these three screens 21

Command For Examples Command to use as example: sort. Reads standard input and sorts to standard output. demo... (use ^d to end input) 22

Redirecting Standard Input command 0< file Reads input from file, instead of keyboard. Shorter form: command < file Example: sort < numbers.txt 23

command 1> file Sends standard output of command to file, instead of screen. Shorter form: command > file Example: sort > file Redirecting Standard Output Example of redirecting standard input and output: sort < input > output 24

Output Redirection: Existing Files Bash may object if you redirect to an existing file Depends on noclobber option. To view current options: set -o To set option: set -o noclobber To turn off option: set +o noclobber Safer: don't count on noclobber. Use one of these instead: command > file: if file already exists, overwrite command >> file: if file already exists, append 25

Redirecting Standard Error command 2>filename example: ls *.c 2>errs 26

Getting Rid of Output You Don't Want send to /dev/null Example: error messages you want to throw away: ls *.c 2>/dev/null 27

Redirecting Both Output Files To 2 separate files: cmd 1>goodOutput 2>errOutput or: cmd >goodoutput 2>errOutput To the same file: cmd 1>outFile 2>&1 or: cmd 2>outFile 1>&2 28

Pipes cmd1 cmd2 Sends standard output of cmd1 to standard input of cmd2 Examples: command less command sort command sort > output commanda 2>&1 commandb cmd1 cmd2 cmd3 29

Sub-Topic 8: Text File Formats Basic text file format: ASCII or Unicode characters Problem: Different OSs, different character(s) for end of line! Unix/Linux: '\n' (newline) Windows/Dos: '\r' + '\n' (carriage return + newline) Mac: varies with version & program Most programs can deal with any of these formats. Others are confused by some formats. 30

Historical Note 31

Safety For Marked Assignments Best practice: Create & test your program on CASLab Linux. Do all your editing on Linux. Submit your program to OnQ from a Linux computer (not possible with putty; use the lab or other Linux GUI) Second best: Create & test your program on Linux. ftp to your own computer (Windows or Mac) submit to Moodle immediately Dangerous: Create & test your program on Linux. ftp to your own computer make a couple of trivial edits submit to Moodle 32