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

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

More Scripting Todd Kelley CST8207 Todd Kelley 1

bash Execution Control COMP2101 Winter 2019

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash, part 3 Chris GauthierDickey

Useful Unix Commands Cheat Sheet

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Linux shell scripting Getting started *

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

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

A shell can be used in one of two ways:

Linux shell programming for Raspberry Pi Users - 2

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

Topic 2: More Shell Skills

Linux 系统介绍 (III) 袁华

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

Welcome to the Bash Workshop!

Topic 2: More Shell Skills

Shell Programming (ch 10)

Last Time. on the website

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

Assignment clarifications

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

Bash Programming. Student Workbook

COMP 4/6262: Programming UNIX

CS Unix Tools & Scripting

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

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

Bourne Shell Reference

Shells and Shell Programming

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shell programming. Introduction to Operating Systems

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

Week 6 Lesson 1: Control-Flow Statements (Continued)

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

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

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

EECS2301. Example. Testing 3/22/2017. Linux/Unix Part 3. for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi done

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

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

Shells and Shell Programming

Scripting. More Shell Scripts Loops. Adapted from Practical Unix and Programming Hunter College

Mills HPC Tutorial Series. Linux Basics II

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

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

Review of Fundamentals

Part 1: Basic Commands/U3li3es

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

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Welcome to the Bash Workshop!

UNIX System Programming Lecture 3: BASH Programming

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

Lab 4: Shell scripting

Advanced Bash Scripting

Implementation of a simple shell, xssh

More Scripting Techniques Scripting Process Example Script

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College

Lab 2: Linux/Unix shell

Advanced Bash Scripting

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy

UNIX Shell Programming

Title:[ Variables Comparison Operators If Else Statements ]

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

9.2 Linux Essentials Exam Objectives

5/20/2007. Touring Essential Programs

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03

Bash Shell Programming Helps

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

Process Management forks, bombs, zombies, and daemons! Lecture 5, Hands-On Unix System Administration DeCal

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

Essential Linux Shell Commands

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala

Processes. Processes (cont d)

Implementation of a simple shell, xssh

Processes and Shells

CSE 15L Winter Midterm :) Review

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

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Environment Variables

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

Bash command shell language interpreter

Shell script/program. Basic shell scripting. Script execution. Resources. Simple example script. Quoting

Unix Processes. What is a Process?

Basic Linux (Bash) Commands

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

Output with printf Input. from a file from a command arguments from the command read

Computer Systems and Architecture

BASH and command line utilities Variables Conditional Commands Loop Commands BASH scripts

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

UNIX shell scripting

Here redirection. Case statement. Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March 5, 2004

RHCE BOOT CAMP. System Administration

Sperimentazioni I LINUX commands tutorial - Part II

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Bash scripting basics

Introduction to UNIX Shell Exercises

Bourne Shell Programming Topics Covered

Transcription:

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

Positional Arguments It is quite common to allow the user of a script to specify what the script is to operate on (e.g. a file, directory, host, interface, etc.) The method shown here requires the script writer to know or figure out how many command line arguments there are before working with them #!/bin/bash # identify files with setuid or setgid permissions in a # directory specified on the command line dirname="$1" if [ -z "$dirname" ]; then echo "You didn't give me a directory name on the command line" >&2 exit 1 fi [ -d "$dirname" ] (echo "$dirname is not a directory" >&2 ; exit 2) if [! -r "$dirname" ]; then echo "You don't have read permission for $dirname" >&2 exit 3 fi if [ -x "$dirname" ]; then echo "Setuid or setgid files:" find "$dirname" -type f -executable -perm -4000 -ls 2>/dev/null sort -k 3 find "$dirname" -type f -executable -perm -2000 -ls 2>/dev/null sort -k 4 exit 0 else echo "You don't have access permission for $dirname" >&2 exit 3 fi

Command Line Processing A loop can be used to cycle through the available command line arguments and interpret what is there We can use shift to renumber the command line variables each time through the loop The case statement can be better than the if statement for this debug=0 while [ $# -gt 0 ]; do if [ "$1" == "-h" ]; then echo "Usage: $0 [-d level] [-h]" exit 0 elif [ "$1" == "-d" ]; then if [[ "$2" =~ ^[1-9]$ ]]; then debug="$2" echo "Debug mode ON, level $debug" shift else echo "Cannot set debug without a level from 1 to 9" >&2 exit 2 fi else echo "Usage: $0 [-d level] [-h]" echo "Argument '$1' not recognized" >&2 exit 2 fi shift done [ $debug -gt 0 ] && echo "Debug set to $debug" echo "This script will now do some useful task"

Command Line Processing The case statement allows us to more clearly show what we are testing for debug=0 while [ $# -gt 0 ]; do case "$1" in -h --help ) echo "Usage: $0 [-d 1-9] [-h]" exit 0 ;; -d --debug ) if [[ "$2" =~ ^[1-9]$ ]]; then debug="$2" shift echo "Debug mode ON, level $debug" else echo "Cannot set debug without a debug level from 1 to 9" >&2 exit 2 fi ;; * ) echo "Usage: $0 [-d level] [-h]" echo "Argument '$1' not recognized" >&2 exit 2 ;; esac shift done echo "Command line processing complete." [ $debug ] && echo "Debug turned on and set to $debug." # rest of script...

Unnamed Arguments Sometimes you need one or more data items for a script and want it on the command line, but don't want the user to have to put option letters or names in front of it (e.g. fixmydir dirname1 dirname2) In your command line processing, assign things on the command line without a dash to a variable used for the list of things to work on declare -a stufftoprocess while [ $# -gt 0 ]; do case "$1" in -h --help ) echo "Usage: $0 [-d level] [-h]" exit 0 ;; * ) stufftoprocess+=("$1") ;; esac shift done [ ${#stufftoprocess[@]} ] && echo "Will do work on ${stufftoprocess[@]} (${#stufftoprocess[@]} items)"

Practice Modify rolldice.sh to accept a count of dice and a number of sides as command line options, only asking the user for those numbers if they didn't give them on the command line Modify your show interfaces script to accept an interface name on the command line and only display information for that interface, as well as adding options to show the routing and external IP information

Error Output Failed commands often generate unwanted or irrelevant error messages That output can be saved as a log, sent to whoever should see it, or discarded Logs are usually kept in /var for programs we care about managing Redirect output using >&, >>&, &, or #>, or #>&# Use the logger command to send messages to the system logging daemon grapsnag >& /tmp/errormessage.txt if [ $?!= 0 ]; then logger -t $(basename "$0") -i -p user.warning -f /tmp/errormessage.txt && rm /tmp/errormessage.txt fi

Substitution and Expansion Revisited Command substitution is used to execute a command list in a separate shell process, invoked using `cmd`, $(cmd) The back-quotes and $() place the output of the sub-shell on the command line using substitution Variable expansion (getting data from variables), command substitution (getting the output of a command), and arithmetic expansion (using the results of arithmetic) are all ways of getting data for use on your command line as inline data bash performs word splitting after doing these expansions and substitutions which can lead to unexpected results if the data contains word delimiters such as tabs or newlines because they get turned into spaces before being put on the command line myvar="line 1 line 2 line 3" echo Default expansion: $myvar echo "Quoted expansion: $myvar" cat <<EOF ----- HERE Document ----- $myvar EOF The bash environment variable IFS controls which characters are turned into spaces if it exists - set it to an empty string to prevent this data conversion, or quote the variable expansion to force bash to leave the data in its original form as a single word, or use a HERE document which works much like double-quoting

Functions A function is a named script block, it creates a command you can use elsewhere in your script Inside a function, the positional parameters hold the arguments given when invoking the function instead of the arguments given when the script itself was run Functions end with the status code of the last command to run in the function, data results can be passed back on stdout, or using an intermediary means such as storing data in a file or variable function myfunction { list }

Practice Create a function to display a message on stderr which was supplied on the function command line Create a function to display command syntax help for the rolldice script Implement the error message function and the command syntax help function in the rolldice script wherever they make sense

Signals Signal are a way of notifying a process you want it to do something Processes can catch and process or ignore most signals, see signal(7) KILL, STOP, CONT cannot be caught or ignored - STOP/CONT are used to pause/resume processes(jobs) Signals can be sent using the kill command $ kill -SIGNAL pid $ somecommand ^Z $ jobs $ fg %# $ bg %# The shell can send some signals (INT(^C), QUIT(^\), STOP(^Z)) based on keyboard input and manage processes using the jobs, fg, and bg commands

Trap In a shell script, catching signals is done with the trap command trap can run a command when a signal is caught, functions are often useful for this function cleanup { rm /tmp/mytemporaryfiles logger -t `basename "$0"` -i -p user.info -s Cleaning up and aborting exit 1 } trap cleanup SIGHUP trap cleanup SIGTERM trap cleanup SIGINT

Dialog boxes For more complex user interactions such as choosing files, selecting items from a list, or presenting graphics on text-only terminals, there is a the dialog command dialog can ask for input/decisions or display information dialog is useful when you are working on a terminal and want to present interactions in a more user-friendly way than just displaying text e.g. userpicked=$(dialog --menu "choose one" 0 0 0 a 1 b 2 c 3 d 4 e 5 --output-fd 1) (for ((i=0;i<=100;i+=10)) do echo $i;sleep 1;done) dialog --gauge "progress" 7 60;clear foo=$(dialog --rangebox "Pick a value" 8 80 1 9 5 --output-fd 1);clear;echo "You chose $foo" dialog's command line can be inscrutable

Practice Create a script that waits for a user-specified number of seconds updating a progress bar each second showing how many seconds are left, and catches the interrupt and the quit signals. If it gets the interrupt signal (like from a ^C), have it reset the timer and the progress bar to the initial number of seconds and print out a message saying it is doing that. It should simply exit with a message if it receives the quit signal (like from a ^\). Name the script countdown.sh.

Script Organization It is helpful to organize nontrivial scripts in a consistent fashion Scripts can be divided into sections Sections may be placed in separate files if those sections are reusable (using the source command) by other scripts, e.g. function definitions Documentation VARIABLE definitions including inline data Aliases and functions Main script commands