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

Similar documents
Control Structures. CIS 118 Intro to LINUX

Title:[ Variables Comparison Operators If Else Statements ]

COMP 4/6262: Programming UNIX

Shell programming. Introduction to Operating Systems

Bourne Shell Reference

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

A shell can be used in one of two ways:

CS Unix Tools & Scripting

Shell Start-up and Configuration Files

Conditional Control Structures. Dr.T.Logeswari

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

Lab 4: Shell scripting

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

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

Computer Systems and Architecture

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

Linux 系统介绍 (III) 袁华

INd_rasN SOME SHELL SCRIPTING PROGRAMS. 1. Write a shell script to check whether the name passed as first argument is the name of a file or directory.

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

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

bash Execution Control COMP2101 Winter 2019

Bash scripting basics

Bash shell programming Part II Control statements

Vi & Shell Scripting

Shell Control Structures

Shell Programming (bash)

Computer Systems and Architecture

Lecture 02 The Shell and Shell Scripting

Shell Control Structures

UNIX shell scripting

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

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

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

9.2 Linux Essentials Exam Objectives

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Linux Bash Shell Scripting

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

What is Bash Shell Scripting?

Basic Linux (Bash) Commands

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

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

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

Useful Unix Commands Cheat Sheet

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

CS214 Advanced UNIX Lecture 4

Essentials for Scientific Computing: Bash Shell Scripting Day 3

CSC 2500: Unix Lab Fall 2016

Implementation of a simple shell, xssh

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

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

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

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits

Practical 04: UNIX / LINUX Shell Programming 1

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

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

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

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

Assignment 3, Due October 4

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

Unix Shell scripting. Dr Alun Moon 7th October Introduction. Notation. Spaces

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

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Lab #12: Shell Scripting

Shells & Shell Programming (Part B)

Essential Skills for Bioinformatics: Unix/Linux

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

Grep and Shell Programming

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

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

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan

Practice 5 Batch & Shell Programming

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits

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

CS1101: Lecture 9 The Shell as a Programming Language

Last Time. on the website

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

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

Pathologically Eclectic Rubbish Lister

More Raspian. An editor Configuration files Shell scripts Shell variables System admin

Processes and Shells

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

UNIX Programming Laboratory. Subject Code: 10MCA17

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

Assignment clarifications

Shells and Shell Programming

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

COMP2100/2500 Lecture 17: Shell Programming II

Shell Programming (Part 2)

Assignment 2. Summary. Some Important bash Instructions. CSci132 Practical UNIX and Programming Assignment 2, Fall Prof.

Review. Preview. Functions. Functions. Functions with Local Variables. Functions with Local Variables 9/11/2017

System Programming. Session 6 Shell Scripting

Using bash. Administrative Shell Scripting COMP2101 Fall 2017

LINUX Scripts de Bash Shell. Exercise 1 - Working with Files

RHCE BOOT CAMP. System Administration

CS 251L Review Quiz Key

INTRODUCTION TO SHELL SCRIPTING Dr. Jeffrey Frey University of Delaware, IT

CS Unix Tools & Scripting

Transcription:

Bourne Shell Programming Variables - creating and assigning variables Bourne shell use the set and unset to create and assign values to variables or typing the variable name, an equal sign and the value to which you would like it assigned makes an assignment. If a variable of that name es not already exist in the current shell, this will create that variable in the current shell. If a variable of that name es already exist in the current shell, this will change its value. The method for knowing which variables are local and which are environmental in the Bourne shell uses the env command. In the Bourne shell, this information is managed by the "export list". The export list contains the list of variables that will be given to any shells started as children of the current shell. To put a variable on the export list, type the command export with a variable name as the argument. name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable. echo, read and expr In Bourne shell, if you request the display of a variable which es not exist, there will be no error message. Instead, it will display as if that variable has no value (is blank). Another difference in the echo command is that the -n option in C-shell which prevents the end of line is replaced by a \c at the end of the string. For input the Bourne shell uses the read utility which allows user input to be assigned into the variable given as the parameter. The expr command is used to evaluate a mathematical expression. #!/usr/bin/sh #This script is to show how echo is different from the C-shell version # how the read command can be used for input # and how the expr command can be used to evaluate an expression echo "Enter an integer: \c" read int1 echo "Enter another integer: \c" read int2 int3=`expr $int1 + $int2` echo "Sum = $int3 and Difference = `expr $int1 - $int2`" echo "That is all" All BASH shell scripts should include the magic line as the very rst line in the test le which tells the system which shell to use to evaluate the included statements: #!/bin/bash <option> Ou can also invoke debugging options interactively, within the script or with the set command: set e If a simple command fails the shell shall immediately exit set n The shell shall read but es not execute them set u The shell shall write a message to standard error when it tries to expand a variable that is not set and immediately exit set v The shell shall write its input to standard error as it is read set x The shell shall write to standard error a trace for each command after it expands the command and before it executes it.

command line arguments The items typed on the command line are put into a vector similar to the method used in the C-shell. The name of the command used to run the script is put into the variable by the name $0 and its arguments are put into the next elements which are named $1 $9. The $1 $9 are actually called the "positional parameters". If there were more than 9 parameters given on the command line, the shift command must be used to bring them into these rst 9 positions before they can be accessed. In Bourne shell, there is a set command, it just has a different purpose than the set command of the C-shell. The set command here allows you to assign values into the positional parameters. Giving the command followed by a list of values will put those values into the rst positions of the positional parameters. Note you can also unset variable (undened them). set Jan Mike echo $1 Jan echo $2 Mike This can be especially useful because it is an easy way to create an array of elements when those elements are normally only on a line separated by spaces. For example, the output of the date command has many pieces of information separated from one another by spaces. Assigning this into the positional parameters allows you to access each element individually. set `date` echo $1 Fri echo $6 2002 PATH UNIX / LINUX executables are located in a directory sequence called the PATH (echo $PATH). Unlike Winws which searches rst in the current directory for an executable, UNUX and LINUX only use the PATH vaiable to locate executable.

conditionals The structure of the if conditional is quite different from the structure in the C-shell programming language, but its purpose and parts are the same. In both languages, the if must have a condition to test and a group of shell that will be performed if and only if that expression evaluates to true. In the Bourne shell the condition being evaluated is called a "test condition". This is probably because it is often with the utility named test. But anything that returns the successful/unsuccessful result can be used. The true (or successful) is assumed to be the value 0, and the false (or unsuccessful) is any non-zero value. test The test utility, has many formats for evaluating expressions. For example, when given three arguments, will return the value true if the rst and third argument hold the relation represented by the second argument, but it also has many options to test other properties of les, strings and integers. The test utility can be used by stating the keyword test which is followed by one of its many expression formats, or the test utility can simply be called by putting the expression in square brackets. Some of the possible expressions are: int1 relational-operator compares the integers on either side according to the relational operator int2 (-lt,-gt, -eq, -ne, -le, -ge) string1 = string2 compares the two strings for equality string1!= string2 compares the two strings for inequality -z string true if the string is of length 0 -r lename true if you have read permission on the le named There are also compound expressions allowed in the test expression. These are built by using the -a for "and" and the -o for "or". You can also use parentheses to set the order of precedence and the exclamation point for not. if - - The structure of the if (one without an ) has the if and the expression to be tested on one line, followed with a on a line by itself, followed by the body, and nally followed by a (if written backwards). if test-expression or if [condition] The test expression will include everything between the keyword if and the end of that line. Like the if in the C-shell, the in the body are each complete lines as they would be given on the shell command line (Bourne shell command line in this case). The of the body are in sequence as listed. adding the clause The structure of the if with an has the keyword if and the expression to be tested on one line, followed by the on a line by itself, followed by the body to be if the condition is true, followed by an on a line by itself, followed by the body to be if the condition is false, followed by a on its own line. if test-expression or if [condition] 1 2

adding the elif clause There is also the possibility of the having an if (like there was in C-shell), but in Bourne shell, it is not written as two separate words. The "" and the "if" are combined to make an elif keyword. The elif keyword is used just like the " if" of the C-shell in that it takes its own condition but es not need its own end marker. if test-expression1 or if [condition1] 1 elif test-expression2 or if [condition2] 2 3 #!/usr/bin/sh # To show the use of read and structured if echo -n "rst value: " read val1 echo -n "second value: " read val2 echo -n "third value: " read val3 if test $val1 = $val2 -a $val1 = $val3 echo "complete match" elif test $val1 = $val2 -o $val1 = $val3 -o $val2 = $val3 echo "only two match" echo "no matches"

loops In Bourne shell there are several loop structures. The two for looping structures are similar to the foreach of C-shell programming and the while and until loops are more similar to the while loop of C-shell programming. In all of the loop structures of Bourne shell, the body of the loop is contained between a (on a line by itself) and a (also on a line by itself). The in the body are any valid Bourne shell and will be performed in sequence. for - in The for-in loop structure uses a loop control variable specied after the keyword for. The loop control variable takes on the values given in the list which is specied after the keyword in. This structure is very similar to the foreach loop of C-shell. The difference is the use of the word in rather than the parenthesis to separate the loop control variable from the list of values. for loop-var in list for The for loop is actually a for-in loop but using a default value for the list rather than specifying it explicitly. The default value for the list is the argument vector (as it was called it C-shell) or the command line parameter list (as it is called in Bourne shell). This is the most common use of the for statement is to be able to traverse the list of command line parameters. for loop-var while The while loop uses the same structure for the test-command as was used in the if statement. The statements in the body of the while loop are performed completely and in sequence if the test-command evaluates to true. while test-command until The until loop has the same structure as the while in that it is top-tested. (Unlike the "-while" loop of the C programming language.) The while and the until are only different in the condition specied at the top. The while loop will go into and perform the statements in the body of the loop if and only if that condition evaluates to true. With the until loop, the statements are performed if and only if the expression evaluates to false. until test-command