Shells and Shell Programming

Similar documents
Shells and Shell Programming

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

Shell Programming (bash)

Shells & Shell Programming (Part B)

Assignment clarifications

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

CSC209H Lecture 1. Dan Zingaro. January 7, 2015

COMP 4/6262: Programming UNIX

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

Linux shell programming for Raspberry Pi Users - 2

bash, part 3 Chris GauthierDickey

Useful Unix Commands Cheat Sheet

A shell can be used in one of two ways:

Part 1: Basic Commands/U3li3es

Last Time. on the website

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

UNIX Shell Programming

Bourne Shell Reference

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Introduction to the Shell

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

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Basic Linux (Bash) Commands

UNIX shell scripting

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

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

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

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter.

EECS2301. Lab 1 Winter 2016

UNIX System Programming Lecture 3: BASH Programming

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

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

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

Vi & Shell Scripting

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

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

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

9.2 Linux Essentials Exam Objectives

Review of Fundamentals

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

Chapter 9. Shell and Kernel

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?

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

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

Introduction to UNIX Shell Exercises

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

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

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

CS Unix Tools & Scripting

Consider the following program.

Shell programming. Introduction to Operating Systems

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

Bash command shell language interpreter

More Scripting Techniques Scripting Process Example Script

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

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

Linux 系统介绍 (III) 袁华

Shell Programming Overview

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

Linux Command Line Interface. December 27, 2017

Processes and Shells

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019

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

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP

Linux shell scripting Getting started *

CSC209 Review. Yeah! We made it!

UNIX Kernel. UNIX History

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

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

Linux & Shell Programming 2014

Review of Fundamentals. Todd Kelley CST8207 Todd Kelley 1

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

5/20/2007. Touring Essential Programs

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

Shell Programming. Introduction to Linux. Peter Ruprecht Research CU Boulder

Introduction to Shell Scripting

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

Shell Programming Systems Skills in C and Unix

Introduction to UNIX Part II

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

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

A Brief Introduction to the Linux Shell for Data Science

Perl and R Scripting for Biologists

LING 408/508: Computational Techniques for Linguists. Lecture 5

System Programming. Unix Shells

The Online Unix Manual

Chapter 5 The Bourne Shell

Conditional Control Structures. Dr.T.Logeswari

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

Bash scripting basics

CST Algonquin College 2

CSE II-Sem)

Bourne Shell Programming Topics Covered

Basic Unix Command. It is used to see the manual of the various command. It helps in selecting the correct options

Transcription:

Shells and Shell Programming Shells A shell is a command line interpreter that is the interface between the user and the OS. The shell: analyzes each command determines what actions are to be performed performs the actions Example: wc l file1 > file2 1 2 Which shell? sh Bourne shell Most common, other shells are a superset Good for programming csh or tcsh command-line default on CDF C-like syntax Best for interactive use. Not good for programming. bash default on Linux (Bourne again shell) Based on sh, with some csh features. korn written by David Korn Based on sh Some claim best for programming. Commercial product. bash versus sh On the CDF machines, when you run sh, you are actually running bash. bash is a superset of sh. For CSC209, you will be learning only the features of the language that belong to sh. 3 4

Common shell facilities Input-output redirection prog < infile > outfile ls >& outfile ls > outfile 2>&1 # csh stut and stderr # sh stut and stderr Pipelining commands send the output from one command to the input of the next. ls -l wc ps aux grep krueger sort Job Control A job is a program whose execution has been initiated by the user. At any moment, a job can be running or suspended. Foreground job: a program which has control of the terminal Background job: runs concurrently with the parent shell and es not take control of the keyboard. Start a job in the background by appending & Commands: ^Z, jobs, fg, bg, kill 5 6 File Name Expansion ls *.c rm file[1-6].? cd ~/bin ls ~krueger ls *.[^oa] - ^ in csh,! in sh * stands in for 0 or more characters? stands in for exactly one character [1-6] stands in for one of 1, 2, 3, 4, 5, 6 [^oa] stands in for any char except o or a ~/ stands in for your home directory Exceptions ls.* esn t what you would expect Why?.* matches. and.. because. files are hidden files, we n t usually want to include them in our operations. How to get around this feature? ls -d.* - still catches. and.. ls.??* - misses files like.b Challenge: find other ways. ~krueger stands in for krueger s home directory 7 8

Shell Programming (Bourne shell) Commands run from a file in a subshell A great way to automate a repeated sequence of commands. File starts with #!/bin/sh absolute path to the shell program not the same on every machine. Can also write programs interactively by starting a new shell at the command line. Tip: this is a good way to test your shell programs In a file: #! /bin/sh echo "Hello World!" At the command line: Example skywolf% sh sh-2.05b$ echo "Hello World" Hello World sh-2.05b$ exit exit skywolf% 9 10 Commands Variables You can run any program in a shell by calling it as you would on the command line. When you run a program like grep or ls in a shell program, a new process is created. There are also some built-in commands where no new process is created. echo set read exit test shift wait "man sh to see all builtins. 11 local variables spaces matter name=value assignment $name replaced by value of name variables can have a single value or list of values. Single value: bindir="/usr/bin" List of values (separated by spaces): searchdirs="~/tests $HOME/test2." 12

Example: ($ is the default sh prompt) $ bindir="/usr/bin" $ searchdirs="~/tests $HOME/test2." $ echo $searchdirs ~/tests /u/krueger/test2. $ echo $bindir /usr/bin 13 String Replacement Scripting languages are all about replacing text or strings, unlike other languages such as C or Java which are all about data structures. Variables are placeholders where we will substitute the value of the variable. Example: iters="1 2 3 4" for i in $iters; echo $i = for i in 1 2 3 4; echo $i 14 Quoting Double quotes inhibit wildcard replacement only. Single quotes inhibit wildcard replacement, variable substitution and command substitution. Back quotes cause command substitution. Practice and pay attention. Single and uble quotes are on the same key. Back quote is often on the same key as ~. 15 Quoting example $ echo Today is date Today is date $ echo Today is `date` Today is Thu Sep 19 12:28:55 EST 2002 $ echo "Today is `date`" Today is Thu Sep 19 12:28:55 EST 2002 $ echo Today is `date` Today is `date` " uble quotes single quote ` - back quote 16

Another Quoting Example What the following statements produce if the current directory contains the following nonexecutable files? a b c $ echo * $ echo ls * $ echo `ls *` $ echo "ls *" $ echo ls * $ echo `*` " uble quotes single quote ` - back quote 17 More on Quoting Command substitution causes another process to be created. Which is better? What is the difference? src=`ls *.c` or src="*.c" 18 The built-in command test is used to construct conditional statements in Bourne shell -d filename -f filename -r filename -w filename -x filename -z string str1 = str2 -a, -o Test str1!= str2 int1 -eq int2 -ne, -gt, -lt, -le test arguments Exists as a directory Exists as a regular file. Exists as a readable file Exists as a writable file. Exists as an executable file. True if empty string True if str1 equals str2 True if str1 not equal to str2 True if int1 equals int2 And, or. 19 Control statements for loop for color in red green blue pink echo The sky is $color if statements if then elif then else fi if test! d notes then else fi echo not found echo found = if [! d notes ] then else fi echo not found echo found 20

More on if If statements just check the return value of the command. test is just a command that returns a value. E.g., if grep name file then echo found else echo not found fi Command line arguments positional parameters: variables that are assigned according to position in a string Command line arguments are placed in positional parameters: giant #!/bin/sh echo arg1: $1 echo arg2: $2 echo name: $0 echo all: $* $ giant fee fie fo fum arg1: fee arg2: fie name: giant all: fee fie fo fum 21 22 set and shift set assigns positional parameters to its arguments. $ set `date` $ echo "The date today is $2 $3, $6" The date today is May 25, 2006 shift change the meaning of the positional parameters giant2 #!/bin/sh while test "$1" echo $1 shift $ giant2 fee fie fo fum fee fie fo fum 23 Iterating over arguments Don t use this one unless you know that the argument list will always be short sh allows only 9 positional parameters #!/bin/sh while test "$1" echo $1 shift The method below is more portable. Use this one. #!/bin/sh for arg in "$@" echo $arg 24

Even more on quotes Getting the quotes right on a loop or similar commands can be a bit tricky. The following 4 loops different things: for arg in "$*" echo $arg for arg in $* echo $arg Quotes mean arguments are all in one string. One element for each argument. for arg in "$@" echo $arg for arg in $@ echo $arg Quotes in the arg list are preserved Does not preserve quotes in arg list. 25 expr Since shell scripts work by text replacement, we need a special function for arithmetic. x=1 x=`expr $x + 1` y=`expr 3 *\* 5` 5` #esn t #need work to escape * Can also be used for string manipulation, but we will mostly leave text manipulation for Python. 26 String matching using expr expr $string : $substring Returns the length of matching substring at beginning of string. I.e., it returns 0 if the substring is not found at the beginning of string. Useful in some simple cases. If you need anything more complicated use Python, Perl, sed or awk. 27 read read one line from standard input and assigns successive words to the specified variables. Leftover words are assigned to the last variable. name #!/bin/sh echo "Enter your name:" read fname lname echo "First: $fname" echo "Last: $lname" $ name Enter your name: Alexander Graham Bell First: Alexander Last: Graham Bell 28

Reading from a file Subroutines while read line echo $line < $file Reads one line at a time from a file. $file contains the name of the file that will be read from. 29 You can create your own functions or subroutines: myfunc() { arg1=$1 arg2=$2 echo $arg1 $globalvar return 0 } globalvar="i am global" myfunc num1 num2 Notes: Arguments are passed through positional parameters. Variables defined outside the function are visible within. Return value is the value of the last executed command in the function. 30 NAME cut - remove sections from each line of files SYNOPSIS cut [OPTION]... [FILE]... DESCRIPTION Print selected parts of lines from each FILE to standard output. -c, --characters=list -d, --delimiter=delim -f, --fields=list output only these characters use DELIM instead of TAB for field delimiter output only these fields Use one, and only one of -b, -c or -f. Each LIST is made up of one range, or many ranges separated by commas. Each range is one of: N N'th byte, character or field, counted from 1 N- from N'th byte, character or field, to end of line N-M from N'th to M'th (included) byte, character or field The order of bytes, characters or fields in the output will be identical to those in the input. With no FILE, or when FILE is -, read standard input. 31 The power of pipelines How many people with cdf accounts are using the bash shell as their default shell? First we need to know that the default shell is stored in /etc/passwd g4wang:x:10461:1009:wang Guoyu:/h/u3/g4/00/g4wang:/var/shell/bash g4ali:x:10462:1009:ali Muhammad:/h/u3/g4/00/g4ali:/var/shell/tcsh g4lily:x:10463:1009:hu Lily:/h/u3/g4/00/g4lily:/var/shell/tcsh g4daniel:x:10464:1009:chu Daniel C:/h/u3/g4/00/g4daniel:/var/shell/tcsh g4yk:x:10465:1009:kim Youngki:/h/u3/g4/00/g4yk:/var/shell/tcsh g4kimukr:x:10466:1009:kim Uk Rae:/h/u3/g4/00/g4kimukr:/var/shell/bash g4kongja:x:10467:1009:kong Jason:/h/u3/g4/00/g4kongja:/var/shell/tcsh 32

The power of pipelines Solution: Answer: 77 How many CDF accounts are there? wc /etc/passwd Answer: 4650 (almost) grep bash /etc/passwd wc Another problem If I am logged into seawolf, how can I find out how many people are running bash or tcsh right now? Step 1: Display active processes using ps. man ps ps normally shows processes associated with your terminal use the options aux to display all processes. 33 34 More on grep and pipes More on grep and pipes Step 2: Extract the processes running bash. root 1254 0.0 0.0 2480 1052? S 2004 0:00 /bin/bash / g1gros 4151 0.0 0.0 2484 1532 pts/23 S Jan13 0:00 -bash pgries 29010 0.0 0.0 3456 2464 pts/0 S 09:12 0:00 -bash g1gros 865 0.0 0.0 2452 1464 pts/7 S 10:08 0:00 -bash krueger 4228 0.0 0.0 1340 472 pts/6 S 11:57 0:00 grep bash Solution: ps aux grep bash Step 3: Weed out the grep process (man grep) Solution : ps aux grep bash grep -v grep 35 Step 4: Get rid of duplicate names Strip out only the name Use cut to break each line into fields. Two ways to it: cut -d " " -f 1 Set the delimiter to be a space and select the first field. cut -c -8 Select characters from beginning to the 8th one 36

More on grep and pipes Now get rid of duplicates ps aux grep bash grep -v grep cut -d " " -f 1 sort uniq And finally, count them ps aux grep bash grep -v grep cut -d " " -f 1 sort uniq wc -l find [path ] [expression] Expression Options: -maxdepth level Tests: -name pattern Base of file name matches shell pattern pattern -newer file File was modified more recently the file. Actions -print -exec 37 38 find and xargs find. -name "*.java" -print Displays the names of all the Java files in directories in and below the current working directory. xargs Build and execute command lines from standard input. find. -name "*.java" -print xargs grep "import junit" 39