Bourne Shell Reference

Similar documents
COMP 4/6262: Programming UNIX

Shell programming. Introduction to Operating Systems

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

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

A shell can be used in one of two ways:

bash Execution Control COMP2101 Winter 2019

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Linux shell programming for Raspberry Pi Users - 2

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

Computer Systems and Architecture

Last Time. on the website

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

UNIX shell scripting

Assignment clarifications

There are some string operators that can be used in the test statement to perform string comparison.

Basic Linux (Bash) Commands

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

Shell Script Example. Here is a hello world shell script: $ ls -l -rwxr-xr-x 1 horner 48 Feb 19 11:50 hello* $ cat hello #!/bin/sh

Computer Systems and Architecture

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019

Conditional Control Structures. Dr.T.Logeswari

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

Shell Programming (bash)

CS Unix Tools & Scripting

Shells and Shell Programming

Shells and Shell Programming

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University

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

Bash scripting basics

Chapter 5 The Bourne Shell

Title:[ Variables Comparison Operators If Else Statements ]

CSC 2500: Unix Lab Fall 2016

Bourne Shell Programming Topics Covered

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

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

What is Bash Shell Scripting?

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

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

UNIX Shell Programming

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

Lab 4: Shell scripting

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

9.2 Linux Essentials Exam Objectives

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

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement)

Vi & Shell Scripting

Shell Programming (Part 2)

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

Useful Unix Commands Cheat Sheet

More Scripting Techniques Scripting Process Example Script

Essentials for Scientific Computing: Bash Shell Scripting Day 3

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Control Structures. CIS 118 Intro to LINUX

Програмиранев UNIX среда

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

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

CMPS 12A Introduction to Programming Lab Assignment 7

Introduction to the Shell

bash, part 3 Chris GauthierDickey

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

I/O and Shell Scripting

Grep and Shell Programming

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

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

Review of Fundamentals

Bash command shell language interpreter

Common File System Commands

Shell Start-up and Configuration Files

Shells & Shell Programming (Part B)

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

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

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

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

Linux 系统介绍 (III) 袁华

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

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

Lab 5a Shell Script Lab 4 Using Arithmetic Operators in shell script

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

Advanced bash scripting

Shell Control Structures

Bash Reference Manual

Table of Contents. 1. Introduction. 2. Environment. 3. Shell Scripting Shell Scripting Introduction

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

CS214 Advanced UNIX Lecture 4

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

/smlcodes /smlcodes /smlcodes. Shell Scripting TUTORIAL. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation

You may already be familiar with some redirection, such as. which saves the output of the ls command into a file called lsoutput.txt.

Lecture 02 The Shell and Shell Scripting

BASH SHELL SCRIPT 1- Introduction to Shell

Bourne Shell (ch 8) Overview. Bourne Shell. Bourne Shell. Bourne Shell. Bourne Shell. Redirect standard error. Redirect standard error

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Learn Bash The Hard Way

Introduction to UNIX Part II

Linux Bash Shell Scripting

Basics. I think that the later is better.

Transcription:

> Linux Reviews > Beginners: Learn Linux > Bourne Shell Reference Bourne Shell Reference found at Br. David Carlson, O.S.B. pages, cis.stvincent.edu/carlsond/cs330/unix/bshellref - Converted to txt2tags format by xiando This le contains short tables of commonly used items in this shell. In most cases the information applies to both the Bourne shell (sh) and the newer bash shell. 1. Tests 1.1. Checking les 1.2. Checking strings 1.3. Checking numbers 1.4. Boolean operators 2. I/O Redirection 3. Shell Built-in Variables 4. Pattern Matching 5. Quoting 6. Grouping 7. Case statement 8. Shell Arithmetic 9. Order of Interpretation 10. Other Shell Features 1. Tests Tests (for ifs and loops) are done with [ ] or with the test command. 1.1. Checking les -r le Check if le is readable. -w le Check if le is writable. -x le Check if we have execute access to le. -f le Check if le is an ordinary le (as opposed to a directory, a device special le, etc.) -s le Check if le has size greater than 0. -d le Check if le is a directory. -e le Check if le exists. Is true even if le is a directory. if [ -s le ] 1. Tests 1

such and such 1.2. Checking strings s1 = s2 Check if s1 equals s2. s1!= s2 Check if s1 is not equal to s2. -z s1 Check if s1 has size 0. -n s1 Check if s2 has nonzero size. s1 Check if s1 is not the empty string. if [ $myvar = "hello" ] echo "We have a match" 1.3. Checking numbers Note that a shell variable could contain a string that represents a number. If you want to check the numerical value use one of the following: n1 -eq n2 Check to see if n1 equals n2. n1 -ne n2 Check to see if n1 is not equal to n2. n1 -lt n2 Check to see if n1 < n2. n1 -le n2 Check to see if n1 <= n2. n1 -gt n2 Check to see if n1 > n2. n1 -ge n2 Check to see if n1 >= n2. if [ $# -gt 1 ] echo "ERROR: should have 0 or 1 command-line parameters" 1.4. Boolean operators! not -a and -o or if [ $num -lt 10 -o $num -gt 100 ] echo "Number $num is out of range" elif [! -w $lename ] echo "Cannot write to $lename" 1.1. Checking les 2

Note that ifs can be nested. For example: if [ $myvar = "y" ] echo "Enter count of number of items" read num if [ $num -le 0 ] echo "Invalid count of $num was given" else... do whatever... The above example also illustrates the use of read to read a string from the keyboard and place it into a shell variable. Also note that most UNIX commands return a true (nonzero) or false (0) in the shell variable status to indicate whether they succeeded or not. This return value can be checked. At the command line echo $status. In a shell script use something like this: if grep -q shell bshellref echo "true" else echo "false" Note that -q is the quiet version of grep. It just checks whether it is true that the string shell occurs in the le bshellref. It does not print the matching lines like grep would otherwise do. 2. I/O Redirection pgm > le Output of pgm is redirected to le. pgm < le Program pgm reads its input from le. pgm >> le Output of pgm is appended to le. pgm1 pgm2 Output of pgm1 is piped into pgm2 as the input to pgm2. n > le Output from stream with descriptor n redirected to le. n >> le Output from stream with descriptor n appended to le. n >& m Merge output from stream n with stream m. n <& m Merge input from stream n with stream m. << tag Standard input comes from here through next tag at start of line. Note that le descriptor 0 is normally standard input, 1 is standard output, and 2 is standard error output. 3. Shell Built-in Variables $0 Name of this shell script itself. $1 Value of rst command line parameter (similarly $2, $3, etc) $# In a shell script, the number of command line parameters. $* All of the command line parameters. $- Options given to the shell. $? Return the exit status of the last command. $$ Process id of script (really id of the shell running the script) 3. Shell Built-in Variables 3

4. Pattern Matching * Matches 0 or more characters.? Matches 1 character. [AaBbCc] matches any 1 char from the list. [^RGB] matches any 1 char not in the list. [a-g] matches any 1 char from this range. 5. Quoting \c Take character c literally. `cmd` Run cmd and replace it in the line of code with its output. "whatever" Take whatever literally, after rst interpreting $, `...`, \ 'whatever' Take whatever absolutely literally. match=`ls *.bak` #Puts names of.bak les into shell variable match. echo \* #Echos * to screen, not all lename as in: echo * echo '$1$2hello' #Writes literally $1$2hello on screen. echo "$1$2hello" #Writes value of parameters 1 and 2 and string hello. 6. Grouping Parentheses may be used for grouping, but must be preceded by backslashes since parentheses normally have a different meaning to the shell (namely to run a command or commands in a subshell). For example, you might use: if test \( -r $le1 -a -r $le2 \) -o \( -r $1 -a -r $2 \) do whatever 7. Case statement Here is an example that looks for a match with one of the characters a, b, c. If $1 fails to match these, it always matches the * case. A case statement can also use more advanced pattern matching. case "$1" in a) cmd1 ;; b) cmd2 ;; c) cmd3 ;; *) cmd4 ;; esac 8. Shell Arithmetic 8. Shell Arithmetic 4

In the original Bourne shell arithmetic is done using the expr command as in: result=`expr $1 + 2` result2=`expr $2 + $1 / 2` result=`expr $2 \* 5` (note the \ on the * symbol) With bash, an expression is normally enclosed using [ ] and can use the following operators, in order of precedence: * / % (times, divide, remainder) 1. - (add, subtract) < > <= >= (the obvious comparison operators) ==!= (equal to, not equal to) && (logical and) (logical or) = (assignment) Arithmetic is done using long integers. result=$[$1 + 3] In this example we take the value of the rst parameter, add 3, and place the sum into result. 9. Order of Interpretation The bash shell carries out its various types of interpretation for each line in the following order: brace expansion (see a reference book) ~ expansion (for login ids) parameters (such as $1) variables (such as $var) command substitution ( match=`grep DNS *` ) arithmetic (from left to right) word splitting pathname expansion (using *,?, and [abc] ) 10. Other Shell Features $var Value of shell variable var. ${var}abc value of shell variable var with string abc appended. # At start of line, indicates a comment. var=value Assign the string value to shell variable var. cmd1 && cmd2 Run cmd1, if cmd1 successful run cmd2, otherwise skip. cmd1 cmd2 Run cmd1, if cmd1 not successful run cmd2, otherwise skip. cmd1; cmd2 Do cmd1 and cmd2. cmd1 & cmd2 Do cmd1, start cmd2 without waiting for cmd1 to nish. (cmds) Run cmds (commands) in a subshell. See a good reference book for information on traps, signals, exporting of variables, functions, eval, source, etc. http://cis.stvincent.edu/carlsond/cs330/unix/bshellref > Linux Reviews > Beginners: Learn Linux > Bourne Shell Reference 10. Other Shell Features 5