Linux shell scripting Getting started *

Similar documents
Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

Review of Fundamentals

Unix Processes. What is a Process?

A shell can be used in one of two ways:

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

bash, part 3 Chris GauthierDickey

BASH SHELL SCRIPT 1- Introduction to Shell

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

Chapter 9. Shell and Kernel

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

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

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

Useful Unix Commands Cheat Sheet

Assignment clarifications

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

More Scripting Todd Kelley CST8207 Todd Kelley 1

Shells and Shell Programming

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

Linux shell programming for Raspberry Pi Users - 2

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

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

Introduction to the Shell

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

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

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

Implementation of a simple shell, xssh

UNIX System Programming Lecture 3: BASH Programming

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

Linux Command Line Interface. December 27, 2017

System Programming. Introduction to Unix

COMS 6100 Class Notes 3

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Shells and Shell Programming

85321, Systems Administration Chapter 6: The shell

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

Shell Script Programming 2

Command-line interpreters

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Creating a Shell or Command Interperter Program CSCI411 Lab

More Scripting Techniques Scripting Process Example Script

Lab 4: Shell scripting

B a s h s c r i p t i n g

Last Time. on the website

Bash Programming. Student Workbook

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

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.

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

The Shell, System Calls, Processes, and Basic Inter-Process Communication

COMP 4/6262: Programming UNIX

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

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

UNIX Shell Programming

5/20/2007. Touring Essential Programs

Basic Linux (Bash) Commands

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

hash Remember the full pathname of a name argument head Output the first part of file(s) history Command History hostname Print or set system name

SOFTWARE ARCHITECTURE 3. SHELL

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

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

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

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.

Implementation of a simple shell, xssh

CS370 Operating Systems

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

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

The Unix Shell & Shell Scripts

CST Algonquin College 2

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

Process Management! Goals of this Lecture!

Introduction To. Barry Grant

Exploring UNIX: Session 3

Introduction to UNIX Shell Exercises

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Processes and Shells

9.2 Linux Essentials Exam Objectives

Chapter 1 - Introduction. September 8, 2016

Topic 2: More Shell Skills

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

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

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth

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

$Id: asg4-shell-tree.mm,v :36: $

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

Introduction to UNIX Part II

Process Management 1

Part 1: Basic Commands/U3li3es

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

Bash Reference Manual

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

System Administration

Process Management! Goals of this Lecture!

Topic 2: More Shell Skills

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

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

The Classical OS Model in Unix

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

CSC209F Midterm (L5101) Fall 1998 University of Toronto Department of Computer Science

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

Transcription:

Linux shell scripting Getting started * David Morgan *based on chapter by the same name in Classic Shell Scripting by Robbins and Beebe What s s a script? text file containing commands executed as a unit command means a body of code from somewhere from where? the alias list, in the shell s memory the keywords, embedded in the shell the functions that are in shell memory the builtins, embedded in the shell code itself a binary file, outboard to the shell 1

Precedence of selection for execution aliases keywords (a.k.a. reserved words) functions builtins files (binary executable and script) hash table PATH Keywords (a.k.a. reserved words) RESERVED WORDS Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command or the third word of a case or for command:! case do done elif else esac fi for function if in select then until while { } time [[ ]] bash man page 2

bash builtin executables source continue fc popd test alias declare fg printf times bg typeset getopts pushd trap bind dirs hash pwd type break disown help read ulimit builtin echo history readonly umask cd enable jobs return unalias caller eval kill set unset command exec let shift wait compgen exit local shopt complete export logout suspend * code for a bash builtin resides in file /bin/bash, along with the rest of the builtins plus the shell program as a whole. Code for a utility like ls resides in its own dedicated file /bin/ls, which holds nothing else. Being "builtin" is a matter of placement. Other shells too may contain builtins. Their list may overlap with this one. Identically named builtins may not behave identically. bash builtins memory echo echo cd kill exit exec user space bash shells builtins and kernel s system functions cal kill A builtin is a command contained within the Bash tool set, literally built in. This is either for performance reasons -- builtins execute faster than external commands, which usually require forking off a separate process -- or because a particular builtin needs direct access to the shell internals. Advanced Bash-Scripting Guide Shell and kernel each (separately) carry something built in to them shell builtins and kernel system functions open kernel space read fork exec disk What reasons for builtins? - some directly manipulate shell itself, can t achieve function outside shell e.g., cd, exit, exec - efficiency e.g., echo, printf - emergency role e.g. kill, if you ve run out of free PIDs echo, if you ve accidentally deleted /bin/echo echo *, if you ve accidentally deleted /bin/ls kill echo bash cal 3

Locating file executables If the name is neither a shell function nor a builtin, and contains no slashes, bash searches each element of the PATH for a directory containing an executable file by that name. Bash uses a hash table to remember the full pathnames of executable files (see hash under SHELL BUILTIN COMMANDS below). A full search of the directories in PATH is performed only if the command is not found in the hash table. PATH The search path for commands. It is a colon-separated list of directories in which the shell looks for commands... The default path is system-dependent, and is set by the administrator who installs bash. A common value is "/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin". bash man page Example lots of pwd s two code entities called pwd create 3 more alias first choice, trumps function * * * function second choice, trumps builtin builtin third choice, trumps file file/script last choice * *destroy or disable: aliases with unalias functions with unset builtins with enable n files with rm 4

Example lots of time s alias first choice, trumps keyword keyword second choice keyword no way to disable, so can t invoke function tip in scripts control which identically named code you use eg time (keyword or file?) if you want the file provide its fully-qualified path name, or precede it with the built-in command command if you want the keyword, just time eg echo hello (builtin or file?) if you want the file provide its fully-quailified name or precede it with the built-in command command or disable (then later re-enable?) the builtin if you want the builtin just echo or builtin echo 5

How can non-binary scripts run? shell gets kernel to run users commands kernel can run machine-code executables (aside: the shell is a machine-code executable) kernel cannot run scripts a script is not a machine-code executable shell can run scripts, kernel can run shell so scripts are handled by running a copy of the shell with the script as argument, after kernel tries but fails to run the script itself How shell handles your command* /* try to run the program */ execl(program, basename(program), (char *)0); /* the exec failed -- maybe it is a shell script? */ if (errno == ENOEXEC) execl ("/bin/sh", "sh", "-c", program, (char *)0); First resort: shell has kernel try to run program, kernel fails if program is a script /* oh no mr bill!! */ perror(program); return -1; Second resort: shell has kernel try to run a copy of itself, feeding the program to that as argument kernel can run shell, does not fail this time see http://www.faqs.org/faqs/unix-faq/faq/part3/section-16.html *code found in the shell s child branch 6

#! on line 1: Interpreter scripts Interpreter script examples 7

Interpreter script mechanism an inescapable example./rename-me-to something-else becomes /bin/mv rename-me-to something-else Interpreter script implementation* First resort try to exec the file (i.e., the command given) Second resort if the exec of the file failed get the file's first line if it doesn t start with "#!" execute a copy of the shell w/file as argument) else if it does execute the file named in the first line (e.g., /bin/perl), w/file as argument endif Third resort *pseudocode from the shell s child branch 8

Simple output with echo common and convenient for output portability headache, different historical versions example: same syntax, output differs per shell Simple output with printf modeled after C printf( ) function portable across platforms preferred for portability same syntax, same output, both shells 9

printf format-string [arguments] format string contains literals escape sequences format specifiers specifiers apply to arguments Standard I/O Standard I/O is perhaps the most fundamental concept in the Software Tools philosophy. The idea is that programs should have a data source, a data sink (where data goes, and a place to report problems. These are referred to by the names standard input, standard output, and standard error, respectively. A program should neither know, nor care, what kind of device lies behind its input and outputs: disk files, terminals, tape drives, network connections, or even another running program! A program can expect these standard places to be already open and ready to use when it starts up. Classic Shell Scripting, Robbins and Beebe, O Reilly, p. 18 10

Basic I/O redirection pre-arranged place names for data stdin names a data source (a.k.a. 0) stdout names a data destination (a.k.a. 1) stderr another data destination (a.k.a. 2) names connected to physical places, by default stdin gets connected to keyboard stdout to monitor stderr to monitor reconnecting them with other places is redirection main operators > < I/O descriptors in a unix process process ID #1234 /home/david current dir code 0 = ls 1 = -l 2 = foo 3 = bar arguments UID=500 LINES=25 PATH= 1 default 2 handler pointer 3 ignore environment (variables) signal table data 0 1 2 3 stdin stdout stderr /somefile file descriptors user david 11

Example Changes applied to: tr s stdin descriptor 0 tr s stdout descriptor 1 and sort s stdin descriptor 0 sort s stdout descriptor 1 Where did this program write to? hello world program traced; Hello world on screen, syscall trace in file strace1 program redirected, traced; Hello world in file hello.out,` syscall trace in file strace2 but program wrote identically both times (not to different places) program did not write to screen the 1st time program did not write to hello.out the 2 nd time both times it wrote to 1 12

slide series by Professor Jennifer Rexford, Princeton University http://www.cs.princeton.edu/courses/archive/spring11/cos217/lectures/21ioprocessmgmt.pdf Background: dup( ) and creat( ( ) 13

a disconnect happens here this part (not shown) conditional on > somefile a reconnect happens here to file descriptor 1 14

15

16

17

18

Be careful - clobbering receiving file is created first 1 application code is read in (exec) thereafter 2 if application uses the receiving file, it s empty by the time the app comes along to do so prevent: set o noclobber doggone! 1 2 i/o descriptor manipulation preceding examples demonstrate C shell also provides syntax functionally similar uses exec with no command argument to do the job exec [-cl] [-a name] [command [arguments]] If command is specified, it replaces the shell. No new process is created.... [ BUT!!! Check it out!! ] If command is not specified, any redirections take effect in the current shell --bash man page redirect stdout (i.e., descriptor 1) to file: redirect descriptor n to file: redirect descriptor n to m: make n point, also, to wherever m does at the moment close descriptor n exec > filename exec n> filename exec n>&m n>&- 19

Redirect output to file, and back Redirect output to file, and whoops! 20

Plan return route Special files: /dev/null bit bucket program that writes to it experiences success nothing done in practice with what s written allows getting exit status without output 21

Special files: /dev/null grep s exit status needed for if but grep s ouput appears extraneous, unwanted redirect it away grep s ouput isn t here anymore, and also isn t anywhere, but grep exit status was obtained 22