Processes and Shells

Similar documents
Assignment clarifications

Review of Fundamentals

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

5/20/2007. Touring Essential Programs

Linux shell programming for Raspberry Pi Users - 2

COMP 4/6262: Programming UNIX

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

Introduction to Shell Scripting

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

CST Algonquin College 2

UNIX System Programming Lecture 3: BASH Programming

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

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

Environment Variables

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

Shells and Shell Programming

Introduction to UNIX Part II

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

UNIX Shell Programming

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

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

Chapter 9. Shell and Kernel

Introduction to the Shell

Vi & Shell Scripting

Mills HPC Tutorial Series. Linux Basics II

System Programming. Introduction to Unix

Last Time. on the website

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

Shells and Shell Programming

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

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

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

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

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?

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

85321, Systems Administration Chapter 6: The shell

Introduction to UNIX Shell Exercises

I/O and Shell Scripting

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

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

INTRODUCTION TO SHELL SCRIPTING ITPART 2

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

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

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

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

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

CSE 15L Winter Midterm :) Review

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

Unix Processes. What is a Process?

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

bash, part 3 Chris GauthierDickey

C Shell Tutorial. Section 1

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

More Scripting Techniques Scripting Process Example Script

Linux shell scripting Getting started *

9.2 Linux Essentials Exam Objectives

What is the Shell. Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within the shell.

SOFTWARE ARCHITECTURE 3. SHELL

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

Lab 2: Implementing a Shell COMPSCI 310: Introduction to Operating Systems

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

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

Bourne Shell Programming Topics Covered

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

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

Linux 系统介绍 (III) 袁华

Shell Programming (bash)

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

ScazLab. Linux Scripting. Core Skills That Every Roboticist Must Have. Alex Litoiu Thursday, November 14, 13

Bash Shell Programming Helps

12.1 UNDERSTANDING UNIX SHELL PROGRAMMING LANGUAGE: AN INTRODUCTION Writing a Simple Script Executing a Script

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.

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

Shell Start-up and Configuration Files

A shell can be used in one of two ways:

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Bourne Shell Reference

CHAPTER 2 THE UNIX SHELLS

Title:[ Variables Comparison Operators If Else Statements ]

CHAPTER 3 SHELL PROGRAMS: SCRIPTS

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

Lab 2: Linux/Unix shell

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

Basic Linux (Bash) Commands

Shell Programming Overview

CS 25200: Systems Programming. Lecture 11: *nix Commands and Shell Internals

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

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

Introduction: What is Unix?

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

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

Shell scripting Scripting and Computer Environment - Lecture 5

Computer Systems and Architecture

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

Linux Bash Shell Scripting

Sperimentazioni I LINUX commands tutorial - Part II

Shell Programming Systems Skills in C and Unix

Implementation of a simple shell, xssh

Transcription:

Shell ls pico httpd CPU Kernel Disk NIC

Processes Processes are tasks run by you or the OS. Processes can be: shells commands programs daemons scripts

Shells Processes operate in the context of a shell. The shell is a command interpreter which: Interprets built in characters, variables and commands Passes the results on to the kernel The kernel is the lowest level of software running. It controls access to all hardware in the computer.

Shells Types of shells: /bin/sh Bourne shell /bin/csh C shell /bin/tcsh Enhanced C shell /bin/bash Bourne again shell /bin/zsh Z shell /bin/ksh Korn shell

Shell Scripts Shell scripts are files which contain commands to be interpreted and executed by a shell. A shell is its own programming environment. Shells contain: Variables Loops Conditional statements Input and Output Built in commands Ability to write functions

Shell Scripts Specifying the shell to be used: On the first line of the file: Implicitly blank line Bourne shell # in column 1 C shell Explicitly #!/bin/sh Bourne shell #!/bin/csh C shell

Exercise Which shell are you using? # echo $SHELL (Don't worry about what these mean, we'll come back to them later)

An Interlude How to be Cool All UNIX people pronounce EVERYTHING. If you don't you aren't cool. Examples:! bang # pound awk awk as in awkward grep grrrrrrr ep chmod chaaa mod chown chaa own www wu wu wu

echo Using echo is very useful for debugging scripts. The echo command prints the value of an expression (to the screen by default) <lister> echo Hello World! Hello World! The n option suppresses newlines

Exercise Run the following script: # cd /opt/exercises/shells #./progress.sh #!/bin/sh i=1 while [ $i < 12 ] do echo n '.' sleep 1 i=`expr $i + 1` done

echo Echoing in a script prints each shell script line to the screen before it is executed. Use the command set x to turn this on

Run this script: #./echotoggle.sh Exercise #!/bin/sh set x echo echo Here is a listing of the files echo ls l

Shell Variables Variables hold strings that can be used later Two Types of Variables: Local (local scope) Environment (global scope)

Shell Variable Assignment Local Variables variable=<value> Environment variable=<value> export variable

Accessing Variables All variables are dereferenced by placing a $ in front of the variable name <lister> echo $PATH

Exercise Run this script: #./variables1.sh #!/bin/sh GREETING=Hello export GREETING there=there friends='kevin Lisa Joanne' echo $GREETING $there $friends

Blanks and Quotes Blanks and other white space are ignored by the shell. If you want them included, you must use quotes. Two types of quotes: ' ' Each has a different behaviour when using variables.

Quotes and Substitution When a shell interprets each line, it performs variable substitution before executing commands. If a variable is within double quotes,, it will be substituted. If a variable is within single quotes, it will not be substituted. It will take on its literal value

Exercise Run this script: #./variables2.sh #!/bin/sh GREETING=Hello there=there friend1=kevin friend2=lisa friend3=joanne friends="$friend1 $friend2 $friend3" echo $GREETING $there $friends echo $GREETING '$friends'

Listing Defined Variables For Your Current Shell To list all variables, use the set command with no argument For environement variables, use export with no variables Exercise Get listings of the current shell variables # set # export

Some Common Shell Variables PATH directory paths to search for commands HOSTNAME the name of the computer USER the user id of the user running this shell SHELL the shell currently being used TERM the type of terminal being used PS1 the prompt to print when then shell is ready for another command

Deassigning Variables For all variables, use the unset command unset variable

Command Line Arguments Powerful feature passing values to your shell script. $1..$9 first nine arguments $0 name of the file/command $* everything on the command line $# returns the number of arguments on the command line

Exercise Run the following script: #./clargs.sh Hello World #./clargs.sh Hello #!/bin/sh echo $# echo $0 echo $1

The $$ Variable The $? variable returns the exit value of the most recently called command. This is useful to detect successful completion of a program before continuing to a program which relies on the output of that command. 0 usually a sign of success non zero error of some sort

Special Characters Filename Wildcards (Globbing) Wildcard characters allow you to match multiple file names Two wildcard characters:? matches a single character * matches one or more characters Historical note: The jargon usage derives from glob, the name of a subprogram that expanded wildcards in archaic pre Bourne versions of the Unix shell.

Special Characters Filename Wildcards (Globbing) Example: Four files named biffo, boffo, baffa and baffo b?ffo matches biffo, boffo and baffo but not baffa *ff* matches all four

Special Characters The \ and # Characters \ performs two roles: It escapes characters from substitution It signals the continuation of a shell script line to the next line # before any characters imply that all following characters on the line make up a comment

I/O Streams and Redirection Very powerful feature of the shell. Not found in other operating systems. Think of input and output as streams of data. Three standard streams for a program: Stdin input stream Stdout output stream Stderr stream for error output (on a terminal same as stdout)

I/O Streams and Redirection You control the course of the data streams: < file direct stdin from file > file direct stdout to file >> file append stdout to file Command1 command2 connects stdout of command1 to stdin of command2 via a pipe

I/O Streams File Descriptors Open files are associated with numeric file descriptors 0 stdin 1 stdout 2 stderr You can direct output to multiple file descriptors simultaneously. The most common is 1>&2 file direct stdout AND stderr to file

Exercise Run the following script: #./redir.sh #!/bin/sh cd ls a > /tmp/ls echo < /tmp/ls cat /tmp/ls grep csh

Command Substitution Any command contained within a pair of backticks `` is executed immediately. The output of the command replaces everything in the backticks. This can be used to assign the output of a command to an array to be used later #!/bin/sh files=`ls` echo $files

Exercise Run the following script: #./bt.sh #!/bin/sh files=`ls` echo $files

Expressions Expressions are used in statements to control the flow of the shell Expressions are made up of constants, variables and operators Expressions always evaluate to strings. Numeric calculations can be performed but are translated back to strings Commands can be executed and variable substitutions can take place before an expression is evaluated.

Expressions Most common expressions take on the form: token operator token where token is usually a variable or a constant. Types of operators: Numeric Logical

Numeric Expressions Numeric expressions are evaluated using the expr command: Numeric operators include +,,*,/,% amongst others Example: #!/bin/sh i=1 echo $i i=`expr $i + 2` echo $i $i=`expr $i \* 3` echo $i

Exercise Run this script: #./math.sh #!/bin/sh i=1 echo $i i=`expr $i + 2` echo $i $i=`expr $i \* 3` echo $i

Logical Expressions Logical expressions are almost always used with conditional statements. Logical operators include, & =,!= <=, >=, <, >

Logical Operators Booean OR & Boolean AND = equivalent!= not equivalent <=, >=, <, > lexical or numeric comparisons Examples: expr $i <= 10 expr $1 = dostats

Logical Expressions test Logical expressions are very often built using the test command. Test allows the shell to test for various conditions test d file true if file is a directory test e file true if file exists test $foo eq $bar true if the numbers $foo and $bar are equal

Logical Expressions test In fact, test is so useful, a shortcut has been created for it [ e file ] is the same as test e file

Control Statements Logical expressions can be used with four control statements to direct the flow of execution: if..then..elif..then..else..fi while..do..done for..in..do..done case..in..;;..esac

Control Statements if statement if logical expression then.. elif logical expression then.. else.. fi

Control Statements statement order Note that the pieces of the if statement needed to be on separate lines. They are each treated as commands. You can place them on the same line by separating them with a semicolon. This holds for all flow control if logical expression ; then. elif logical expression ; then. fi

Exercise Run the following script: #./if.sh Enter a CTRL C and then CTRL D Then run it again with just CTRL D #!/bin/sh /bin/sh st=$? if [ $st eq 0 ] then echo "Success!" elif [ $st eq 1 ] then echo "I'm a failure!" fi

Control Statements case statment case string in pattern1).. ;; pattern2).. ;; *).. ;; esac

Run this script: #./case.sh d Exercise #!/bin/sh case $1 in ' d') echo debugging set debug ;; ' c') echo compiling set compile ;; *) file=$2 echo $2 esac

Control Statements foreach statment for variable in wordlist do.. done This statement loops over all of the values in wordlist and assigns them to variable one at a time until all values have been exhausted.

Exercise Run this script: #./for.sh #!/bin/sh files=`ls a` for file in $files do echo $file done

Control Statements while statment while logical expression do.. end This statement loops until the logical expression is false, that is, it continues to loop while the logical expression is true. Make sure that logical expression can evaluate to false at some point or you will have an infinite loop.

Exercise Run this script: #./while.sh #!/bin/sh n=1 echo 'Look. I can count!' while [ $n <= 10 ] do echo n $n sleep 1 n=`expr $n + 1` done echo exit

Executing Shell Scripts There are two ways to execute a shell script: Source the script as if you typed in the commands yourself into the current shell Make the file executable a new shell is spawned and the new processs is a child of the current (parent) shell

Executing Shell Scripts Source. file (Note that is a dot and a space) Each command in the script is interpreted by the current shell. All variables created are incorporated into the current shell. All variables modified affect the current shell Very useful for start up scripts

Executing Shell Scripts Execute chmod 755 file./file A new process is started with a new shell. Variables created by this child will never be available to the parent. Variables from the parent, however, are inherited by the child.

Processes Encore Processes can be run in the background or the foreground of a shell. Background processes are batch processes that must not require terminal input. Foreground processes run interactively and will block any other input to your current shell until they finish

Processes Encore By default, commands or scripts started from the terminal start in the foreground To background a process, place an ampersand (&) after the command when you run it. Exercise Start a clock in the background # xclock &

Processes Encore In the bash shell, the jobs command will show you the list of background processes associated with the current shell To bring a background process to the foreground, use the fg command with the jobid number given by the jobs command: <lister> fg %1

Exercise Bring your clock process back to the foreground and kill it # jobs # fg %1 (or whatever job number it is) Enter a CTRL C

Start up Scripts Start up scripts are useful scripts you can place in all user's home directories to create a common environment. Typically, a start up script will call other scripts to create variables: Excerpt from /etc/profile for i in /etc/profile.d/*.sh; do if [ r $i ]; then. $i fi done unset i

The ps Command The ps command shows processes currently running on your computer. Which processes are shown depends on the options used with the command: No options show only processes associated with the current shell A show all processes l long listing aux the options I use the most