bash, part 3 Chris GauthierDickey

Similar documents
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

Shells and Shell Programming

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

Shells and Shell Programming

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

CSE 390a Lecture 2. Exploring Shell Commands, Streams, Redirection, and Processes

Unix Processes. What is a Process?

Introduction to UNIX Shell Exercises

Topic 2: More Shell Skills

Linux shell scripting Getting started *

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

Useful Unix Commands Cheat Sheet

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

Assignment clarifications

Lab 2: Linux/Unix shell

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Last Time. on the website

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

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

CST Algonquin College 2

CSE 390a Lecture 2. Exploring Shell Commands, Streams, and Redirection

Command-line interpreters

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

Part 1: Basic Commands/U3li3es

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

Review of Fundamentals

Bourne Shell Reference

Programs. Program: Set of commands stored in a file Stored on disk Starting a program creates a process static Process: Program loaded in RAM dynamic

COMS 6100 Class Notes 3

Introduction to UNIX. Introduction. Processes. ps command. The File System. Directory Structure. UNIX is an operating system (OS).

Introduction to UNIX. CSE 2031 Fall November 5, 2012

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

Shells & Shell Programming (Part B)

Reading and manipulating files

elinks, mail processes nice ps, pstree, top job control, jobs, fg, bg signals, kill, killall crontab, anacron, at

System Administration

The Unix Shell & Shell Scripts

5/8/2012. Exploring Utilities Chapter 5

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Basic Linux (Bash) Commands

M2PGER FORTRAN programming. General introduction. Virginie DURAND and Jean VIRIEUX 10/13/2013 M2PGER - ALGORITHME SCIENTIFIQUE

Common File System Commands

UNIX Shell Programming

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Topic 2: More Shell Skills

Sperimentazioni I LINUX commands tutorial - Part II

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

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

I/O and Shell Scripting

Unix Introduction to UNIX

C Shell Tutorial. Section 1

SOFTWARE ARCHITECTURE 3. SHELL

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?

A Brief Introduction to the Linux Shell for Data Science

Shell Programming (ch 10)

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.

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

HW 1: Shell. Contents CS 162. Due: September 18, Getting started 2. 2 Add support for cd and pwd 2. 3 Program execution 2. 4 Path resolution 3

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Introduction to the Shell

5/20/2007. Touring Essential Programs

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

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.

BASH SHELL SCRIPT 1- Introduction to Shell

Systems Programming/ C and UNIX

Computer Systems and Architecture

Bourne Shell Programming Topics Covered

Welcome to Linux. Lecture 1.1

Chapter-3. Introduction to Unix: Fundamental Commands

LOG ON TO LINUX AND LOG OFF

Introduction Variables Helper commands Control Flow Constructs Basic Plumbing. Bash Scripting. Alessandro Barenghi

Module 8 Pipes, Redirection and REGEX

Course 144 Supplementary Materials. UNIX Fundamentals

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

Provides ability to perform UNIX commands at some time in the future. At time time on day day, the commands in filefile will be executed.

Bamuengine.com. Chapter 7. The Process

More Scripting Techniques Scripting Process Example Script

Processes and Shells

Introduction to UNIX Part II

UNIX System Programming Lecture 3: BASH Programming

2) clear :- It clears the terminal screen. Syntax :- clear

Linux Command Line Interface. December 27, 2017

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

More Scripting Todd Kelley CST8207 Todd Kelley 1

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.

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013

Linux shell programming for Raspberry Pi Users - 2

Appendix A GLOSSARY. SYS-ED/ Computer Education Techniques, Inc.

Computer Systems and Architecture

Implementation of a simple shell, xssh

CS370 Operating Systems

85321, Systems Administration Chapter 6: The shell

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

Linux & Shell Programming 2014

IMPORTANT: Logging Off LOGGING IN

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

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

Introduction to Linux (Part I) BUPT/QMUL 2018/03/14

Transcription:

bash, part 3 Chris GauthierDickey

More redirection As you know, by default we have 3 standard streams: input, output, error How do we redirect more than one stream? This requires an introduction to file descriptors 2

File Descriptors Recall that Unix uses files to represent many types of things, from devices to network streams Each process has its own set of streams which are numbered (file descriptor) standard input: file descriptor 0 standard output: file descriptor 1 standard error: file descriptor 2 3

Redirecting Streams We can redirect any file descriptor using: n> file, where n is a number from 0 to the maximum number of file descriptors n< file, redirects the file contents to descriptor n By default, > file and < file are the same as 1> file and 0< file To redirect standard output and standard error: wget http://www.google.com > outfile 2> errfile 4

Appending We can append instead of overwriting: >> redirects standard out, but appends n>>file, redirects the fd to file, but appends Why would we want to do this? 5

printf instead of echo Echo is useful, but printf gives us more control over writing printf works by reading a string and passing arguments to it for substitution # the following prints # hello world\n printf hello %s\n world 6

Formatting strings with printf %c: ASCII character %d, %i: decimal digit %e: floating point ([-]d.precision)[+-]dd) %E: floating point ([-]d.precisione[+-]dd) %f: floating point ([-]ddd.precision) %s: string %o: octal value (unsigned) %u: unsigned decimal %x: unsigned hex %%: a literal % 7

Reading user input We can get user input by using the read command read a b c will take a line of input and assign the first word to a, the next to b, and finally to c If you input more words, the final variable will get the rest of the line 8

Example reading Try this next example using 2, 3, and 4 arguments If you don t give it enough arguments, all the vars aren t filled # read in user input to # a, b, and c read a b c echo a: $a, b: $b, c: $c If you give it too many, the last one takes the rest of the line 9

Reading Options If you want to read into an array, use the -a option read -a args; echo ${args[0]} ${args[1]} If you want to separate lines by something other than newline, use -d read -d, args; echo $args Entering hello,world will echo hello 10

More reading options -s will prevent what the user types from being echoed (think password) -n tells read how many characters to read in -e tells read to use the readline facilities, which gives advanced editing features on the line 11

Redirecting to a loop Reading is great, but we can redirect from a file to act as input We can redirect to functions, loops, ifstatements Read will then take its input from the redirected item 12

Example: redirecting to a loop Here we redefine IFS to be : so we can read from /etc/passwd Notice how we redirect the file to standard input # redirecting from a file # to a loop IFS=: while read v1 v2; do echo v1: $v1, v2: $v2 done < /etc/passwd 13

Command blocks We can enclose any set of commands by { }, which turns that set of commands into a block. Once it s a block, we can redirect input or output: { read v; echo $v } < /etc/passwd 14

Fun Places for Redirection /dev/null: This is the proverbial bit-bucket-- anything sent to here just goes away /dev/random: This is a string of random data that you can read from 15

Process Handling Recall: CTRL-Z suspends a running job fg moves the last background job to the foreground bg moves the last suspended job into the background jobs lists all the jobs 16

Jobs Each job has a job ID, the jobs commands lists all your processes with their job ID %n will refer to job ID n %foo will refer to the job with the command name that begins with foo %?foo will refer to the job with the command name that contains foo %- is the most recent bg job, %+ is the 2nd most recent 17

Signals CTRL-Z is actually a signal: the suspend signal To list all the signals, type kill -l The only signals mapped to control keys are: CTRL-C as SIGINT CTRL-Z as SIGTSTP CTRL-\ as SIGQUIT (stronger than INT) stty can map signals to keys 18

The kill command kill sends signals to processes By default, kill sends SIGTERM You can specify a signal by number or by name if preceeded by a dash kill -HUP 2125 You can refer to a job by its process ID (just a number) or its job ID (%number) 19

The ps command ps is like ls, but for processes By default, it lists a PID, TTY, time, and command The time is processor time so far used by the process We can pass args to ps to get more info: Just man ps for details! 20

Some standard ps args On the Linux systems, ps -e lists all the processes by the user ps ax does a similar thing, but includes all processes ps aux adds user IDs 21

Trapping Signals Trapping signals can help your program deal with abnormal situations To trap signals, we use: trap cmd sig1 sig2... Here, cmd is the name of the command or function to call if one of the listed signals is reached Execution returns to the command following the one where the signal was raised 22

Example Trap Here, the trap command defines a handler for INT The function inthandler is called whenever the process receives SIGINT Run it and try to kill it with CTRL-C # trap SIGINT trap inthandler INT function inthandler { echo You hit CTRL-C! } while true; do sleep 60 done 23

Ignoring a Signal The nohup command will cause the HUP signal to be ignored (called when you exit your shell) We can untrap a signal using - # Ignore any HUPs, similar # to the nohup command function ignorehup { trap HUP eval $@ } trap - HUP 24

Coroutines Let s say you have multiple cores and want to run commands simultaneously We start each command in a script with & However, as soon as the script continues, any remaining processes not complete will enter an orphaned state foo &, bar, exit If bar completes before foo, foo will become an orphan 25

Coroutines To fix this, we add a wait command at the end foo &; bar; wait This forces the script to wait until all background scripts complete wait can also take PID of the job How do we get a PID of a process? 26

The PID variable $$ is always the process ID (PID) of the process that is running It s useful for making temporary files cat junk > /tmp/myfile$$ 27

Subshells Instead of spawning multiple processes, we can also create subshells The syntax of a subshell looks like a code block, but we use () instead ( exit ); echo testing Here, exit is run in a subshell, which doesn t cause the parent to terminate subshells inherit environment variables, standard streams, signal traps and the current directory 28

More Tools Unix contains a host of programs that belong in your toolbox Over the next few slides, several of the more widely used tools will be presented 29

find find is a command that searches the directory tree, performs operations, and can execute commands on results Don t forget: man find Basic syntax: find <path> <expression> 30

Example Finds find. -name *.txt Finds all the files from the current directory that end with.txt find. -name *.swp -exec rm {} \; Finds all the files that end in.swp and removes them {} is substituted with the filename, \; keeps bash from interpreting the ; on the command line 31

cutting things cut is another simple utility that is useful for columned data cut -d : -f1 /etc/passwd -d is the delimiter, -f is the field, which takes a list that is N, N-, N-M, or -M that s the nth column, nth to the end, nth to the mth, or 1st to the mth column By default, TAB is the delimiter 32

More tools head lists the first lines of a file head -n 20 myfile: lists the first 20 lines tail lists the last lines of a file tail myfile or tail -n 20 myfile lists the last 20 lines sort sorts text files, various options can sort on columns, numerically, etc sort myfile: by default it sorts each line alphanumerically 33

More tools... date: gives you the current date time: gives you the timing statistics for executing a command zdump: gives you time in a given time zone touch: creates a file or sets the modified time to the current time at: runs a job at a given time (usually for running a job just once) 34

More tools... sleep: suspends the process for some number of seconds cal: prints a calculator expr: an all-purpose calculator (just like $(()) ) dc: an arbitrary precision calculator that uses reverse polish notation (RPN) 35

More tools grep <pattern> file: searches for the regular expression in file and prints out the line which it s contained on grep function foo *.sh wc gives word counts, line counts, byte counts, depending on the argument wc -l myfile 36

More tools du will list disk usage--by default, it runs in your current directory try du -h for more readable info And even more---where can you look? /usr/bin, /usr/local/bin, /usr/sbin, /usr/local/sbin 37

getopts for better options To improve your ability to get options for your shell scripts, use getopts You give it letters that can be arguments (think -a -b) A colon after a letter means it needs an argument, which is put in $OPTARG # the initial : here prevents silly # error messages from getopts when # it fails. opt is set to? if # it was an illegal argument while getopts :ab:c opt; do case $opt in a ) echo arg a passed ;; b ) echo arg b with $OPTARG ;; c ) echo arg c passed ;; \? ) echo usage: blah blah blah exit 1 esac done 38

getopts continued getopts sets OPTIND to the argument number to be processed next each time it s called We can use a new command, shift, which left shifts all the arguments by a given number (1 by default) Why do we need shift to do this? What use is it? After using getopts, we may want to process the rest of the arguments, so we do a shift $((OPTIND - 1)) We also can t say 1=$2, for example 39

Debugging bash scripts Here s a few things you can do now that your scripts are getting more sophisticated Use the line set -o verbose or set -o xtrace at the start of your script Verbose prints each line as it executes, xtrace prints the line with any substitutions in place 40

Fake Signals You can also trap fake signals for debugging EXIT, called when exit is called from the script ERR, called when any command returns non-zero saves the error code in $?, which you should save DEBUG, called whenever the shell executes a statement useful for monitoring a variable RETURN, called when a script, function, or source finishes 41

Gotta catch em all Not really, you just trap the ones you want trap echo script has exited EXIT Untrap them like other signals trap - EXIT 42