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

Size: px
Start display at page:

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

Transcription

1 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 3 The test also returns a nonzero exit value if there is no argument: test String Operators There are some string operators that can be used in the test statement to perform string comparison. For example, by using test, the following will return a zero exit value: set name=sliao test "$name" = sliao The = operator tests if the two operands are identical. In the above case, we tested whether the contents of the shell variable name is identical to the string sliao. If the two operands are identical, the test returns an exit value of zero; otherwise, it would return a nonzero. The!= operator tests two strings for inequality. The exit status from test is zero if the two strings are not equal, and nonzero if the two string are identical. set name = sliao test "$name"!= sliao test "$name"!= xliao

2 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 3 Space characters can be part of a string. Example: set name="sliao " test "$name" = sliao test "$name"!= sliao The double quotes quoting $name are necessary here to contain the space character as part of the string assigned to name. set name="sliao " test $name = sliao In this example, the last character of the string "sliao ", a space character, is lost because $name is not enclosed by double quotes. A string itself can also be used as an operator to test if the string has a null value. The test returns an exit value zero if the string is not null, and returns a nonzero if the string has a null value. Example: set name=sliao test "$name" set name= test "$name"

3 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 32 Another operator is -n, with the general format -n string The -n operator returns an exit value of zero if the length of string is nonzero. The -z operator is the complement of -n. The expression -z string returns an exit status of zero if the length of string is zero. set name=sliao test -n "$name" test -z "$name" set name= test -n "$name" test -z "$name" Operator Returns true if string = string2 string and string2 are identical string!= string2 string and string2 are not identical string string is not the null string -n string The length of string is nonzero -z string The length of string is zero

4 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 33 Integer Operators The test can be used to test numerical values as well. The format of test remains the same: test expression For example, the operator -eq tests if two integers are equal, and the operator -lt tests if the rst integer is less than the second one. Examples: set count= test "$count" -eq set count= test "$count" -eq test "$count" -lt 5 Some of the integer operators are listed here: Operator int eq int2 int ge int2 int gt int2 int le int2 int lt int2 int ne int2 Returns true if int is equal to int2 int is greater than or equal to int2 int is greater than int2 int is less than or equal to int2 int is less than int2 int is not equal to int2 The shell itself makes no distinction about the type of value stored in a variable. It is the test operator that interprets the value as an integer when an integer operator is applied.

5 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 34 Examples: set x="5" set x2=" " test "$x" = 5 test "$x" -eq 5 test "$x2" = test "$x2" -eq It is clear that the string operator = and the integer operator -eq are doing different things. File Operators Most shell programs have to deal with one or more les. Naturally, Unix allows test to test on the existence and properties of les. Some le operators are list in here: Operator Returns true if r le le exists and has read permission w le le exists and has write permission x le le exists and has execute permission f le le exists and is a regular le d le le exists and is a directory s le le exists and has a size greater than All of the above le operators are unary operators and expect a single argument to follow.

6 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 35 Examples: ls ~ 294 bin mail mbox test -f ~/mbox test -d ~/mbox test -d ~/bin test -x ~/mbox test -r ~/mbox The Logical Operators: -a and o The operator -a performs a logical AND operation of two expressions and will return a true (an exit value of zero) only if the two joined expressions are both true. Examples: set num=3 test "$num" -ge -a "$num" -lt set num=5 test "$num" -ge -a "$num" -lt test -f ~/mbox -a -r ~/mbox test -f ~/mbox -a -x ~/mbox

7 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 36 The o performs a logical OR operation and works in similar format as the -a operator to form a logical OR of two expressions. The evaluation of the two ORed expressions will be true if either the rst expression or the second expression is true. Examples: test -r ~/mbox -o -x ~/mbox test -x ~/mbox -o -x ~/obox test -x ~/mbox -o -x ~/year25 The o operator has lower precedence than the a operator. But the a operator has lower precedence than the integer, string, and le operators. So, the expression test "$num" -ge -a "$num" -lt will be evaluated as test ( $num -ge ) -a ( $num -lt ) However, the above test cannot be executed and will get a message from the system: Badly placed (.

8 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 37 because the parentheses ( and )have a special meaning to the shell, so the above will not be interpreted appropriately by the shell. You still can use parentheses in a test expression to change the order of evaluation, but you need to quote the parentheses. Example: set num=5 test \( $num -ge \) -a \( $num -lt \) An Alternate Format for test The test is used so often by shell programmers that an alternate format of the is recognized. This format improves the readability of the, especially when used in if s. The general format of the test test expression can be expressed in the alternate format as [ expression ] The [ initiates execution of the same test, and in this format, test expects to see a closing ] at the end of the expression. It is important to remember that when you use this form, you must surround the brackets, [ and ], with spaces.

9 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 38 Control Statements A shell is a programming language. As other programming languages, it provides the control structures that allow making decisions. The if- Statement The general format of the if- statement is: if test- The system rst executes test- following the if keyword. If test- returns a zero exit value, all s between and are executed. If test- is unsuccessful, all s between and will be skipped. The following is a program named classday. to remind you every Wednesday that you have class at 6: pm. date Wed Feb 25 :3:48 CST 25 cat classday. #!/bin/sh # Program classday, version if date grep "Wed" > /dev/null echo "It's Wednesday, you have class at 6: pm." classday. It's Wednesday, you have class at 6: pm.

10 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 39 If you do not want to display the output from pipeline if date grep " Wed" you can redirect it to the device /dev/null. Since the redirection task will always be successful, the exit status of the pipeline only depends on if there is a string Wed in the output of the date. The if--else Statement The introduction of the else statement allows the if- structure to make a two-way decision. The general format of the if--else control structure is: if test- else If test- that follows if returns an exit status zero, the if--else structure executes all s between and else, and the s between else and are skipped. If the test- returns a nonzero exit value, the s between and else will be skipped and the s between else and are executed. In either case, only one set of s will be executed: the rst set if the exit status is zero, or the second set if it is nonzero. As an example, we want to improve the program classday. to display a message if today is not a class day.

11 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 4 cat classday.2 #!/bin/sh # Program classday, version 2 if date grep "Wed" > /dev/null echo "It's Wednesday, you have class at 6: pm." else echo "You don't have class today." date Wed Feb 25 :4:58 CST 25 classday.2 It's Wednesday, you have class at 6: pm. The if--elif Statement When your programs become more complex and have more decisions to make, you may need to use nested if-else statements. However, a special elif construct provided by Unix can do this task easily. The general format of the if--elif statement is: if test- elif test-2 elif test-n else

12 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 4 test-, test-2,, test-n are executed in turn and their exit status are tested. If one of them returns an exit status of zero, the s between the follows and another elif, else, or are executed. If none of the test-s returns a zero exit value, the s after the else are executed. The else is optional in this structure. If you have classes on Tuesday and Thursday, you may want to improve the program classday.2 further by using the if--elif structure: cat classday.3 #!/bin/sh # Program classday, version 3 if date grep "Tue" > /dev/null echo "It's Tuesday, you have class at 4: pm." elif date grep "Thu" > /dev/null echo "It's Thursday, you have class at 4: pm." else echo "You don't have class today." date Wed Feb 25 :6:54 CST 25 classday.3 You don't have class today. The exit Command The exit causes a shell program, or a shell to exit immediately. The general format of the exit is exit n

13 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 42 where n is the exit value returned to the system. If you omit n, the exit value will be that of the last executed. The value of n can be from to 255, inclusive. When we use a program that requires arguments, we may pass a wrong number of arguments to the program. For example, the program rmname.2 should remove one entry every time. In the newer version rmname.3, we ask the program to inform user if a wrong number of arguments is passed to it. cat rmname.3 #!/bin/sh # Program rmname, version 3 if [ "$#" -ne ] echo "Incorrect number of arguments." echo "Usage: $ name" exit grep -v "$" phonebook > /tmp/phonebook$$ mv /tmp/phonebook$$ phonebook cat phonebook Perry Don Pesce Tony Pester Larry Pestrak Stan Petate James Peter Brian Peter Bruno rmname.3 Perry Don Incorrect number of arguments. Usage: rmname.3 name rmname.3 'Perry Don' cat phonebook Pesce Tony Pester Larry Pestrak Stan Petate James Peter Brian Peter Bruno

14 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 43 First, the program rmname.3 checks for the correct number of arguments. If the number does not t the requirement, the program will display a two-line message and is terminated by the exit. If the right number of arguments is supplied, the program will remove the name and the associated telephone number from the le phonebook. The case Statement Another useful structure for making decisions is the case. The format of the case statement is case test-string in pattern_ ) ;; pattern_2 ) ;; pattern_n ) ;; esac Please note that each block of s is terminated by double semicolons, ;;. The string test-string is compared with the patterns pattern_, pattern_2,, and pattern_n successively until a match is found. Then, the s listed between the matching pattern and the double semicolons are executed. Once the double semicolons are reached, the execution of the case is terminated. The following program named translator takes the English numbers and translates it to its French equivalent:

15 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 44 cat translator. #!/bin/sh # Program translator, version if test "$#" -ne echo "Usage: $ word" exit case "$" in zero) echo "zero";; one) echo "un";; two) echo "deux";; three) echo "trois";; four) echo "quatre";; ve) echo "cinq";; six) echo "six";; seven) echo "sept";; eight) echo "huit";; nine) echo "neuf";; esac translator. Usage: translator. word translator. one un translator. seven sept translator. ten The last example shows what happens when you type in one word which does not match any pattern in the case statement, there is no echo is executed.

16 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 45 Special Pattern Matching Characters The shell allows us to use the same special characters for specifying the patterns in a case statement as you can for lename substitution. The special characters and strings you can use in the case statement: Pattern [ ] Matches * Matches any string of characters? Matches any single character Matches any one of the characters enclosed Since the pattern * matches everything, it is frequently used at the very end of the case as a default. If none of the previous patterns in the case is matched, the * will be matched. Here is another version of the program translator: cat translator.2 #!/bin/sh # Program translator, version 2 if test "$#" -ne echo "Usage: $ word" exit case "$" in zero) echo "zero";; one) echo "un";; two) echo "deux";; three) echo "trois";; four) echo "quatre";; ve) echo "cinq";; six) echo "six";; seven) echo "sept";; eight) echo "huit";; nine) echo "neuf";;

17 ACS-294- Unix (Winter Term, 26-7) Part II: Shell Programming Page 46 *) echo "Sorry, I was not trained to translate this word" esac translator.2 ve cinq translator.2 ten Sorry, I was not trained to translate this word && and The shell has two simple s, && and, to make a decision based on whether the preceding succeeds or fails. && causes the following list to be executed if the preceding pipeline returns a zero exit value; executes the following list if the preceding pipeline returns a nonzero exit value. For example, && 2 will execute rst, and if returns an exit value of zero, 2 will be executed. If returns an exit value of nonzero, 2 will be skipped. We can rewrite the rst version of the program classday. to: cat classday.. #!/bin/sh # Program classday, version. date grep "Wed" > /dev/null && echo "It's Wednesday, you have class at 6: pm." date Wed Feb 25 :2:36 CST 25 classday.. It's Wednesday, you have class at 6: pm.

Then, we modify the program list.1 to list two files.

Then, we modify the program list.1 to list two files. ACS-2941-001 Unix (Winter Term, 2016-17) Part II: Shell Programming Page 17 Working with Parameters A Unix shell allows users to pass parameters to shell scripts. With processing the parameters passed

More information

Conditional Control Structures. Dr.T.Logeswari

Conditional Control Structures. Dr.T.Logeswari Conditional Control Structures Dr.T.Logeswari TEST COMMAND test expression Or [ expression ] Syntax Ex: a=5; b=10 test $a eq $b ; echo $? [ $a eq $b] ; echo $? 2 Unix Shell Programming - Forouzan 2 TEST

More information

A shell can be used in one of two ways:

A shell can be used in one of two ways: Shell Scripting 1 A shell can be used in one of two ways: A command interpreter, used interactively A programming language, to write shell scripts (your own custom commands) 2 If we have a set of commands

More information

Bourne Shell Reference

Bourne Shell Reference > 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

More information

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1 Shell Scripting Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 If we have a set of commands that we want to run on a regular basis, we could write a script A script acts as a Linux command,

More information

COMP 4/6262: Programming UNIX

COMP 4/6262: Programming UNIX COMP 4/6262: Programming UNIX Lecture 12 shells, shell programming: passing arguments, if, debug March 13, 2006 Outline shells shell programming passing arguments (KW Ch.7) exit status if (KW Ch.8) test

More information

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

Scripting. More Shell Scripts. Adapted from Practical Unix and Programming Hunter College Scripting More Shell Scripts Adapted from Practical Unix and Programming Hunter College Copyright 2006 2009 Stewart Weiss Back to shell scripts Now that you've learned a few commands and can edit files,

More information

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

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable. 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

More information

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

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 Shell Programming Shells A shell can be used in one of two ways: A command interpreter, used interactively A programming language, to write shell scripts (your own custom commands) Shell Scripts A shell

More information

Control Structures. CIS 118 Intro to LINUX

Control Structures. CIS 118 Intro to LINUX Control Structures CIS 118 Intro to LINUX Basic Control Structures TEST The test utility, has many formats for evaluating expressions. For example, when given three arguments, will return the value true

More information

Shell Control Structures

Shell Control Structures Shell Control Structures EECS 2031 20 November 2017 1 Control Structures l if else l for l while l case (which) l until 2 1 if Statement and test Command l Syntax: if condition command(s) elif condition_2

More information

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

Command Interpreters. command-line (e.g. Unix shell) On Unix/Linux, bash has become defacto standard shell. Command Interpreters A command interpreter is a program that executes other programs. Aim: allow users to execute the commands provided on a computer system. Command interpreters come in two flavours:

More information

Shell Control Structures

Shell Control Structures Shell Control Structures CSE 2031 Fall 2010 27 November 2010 1 Control Structures if else for while case (which) until 2 1 if Statement and test Command Syntax: if condition command(s) elif condition_2

More information

Shell programming. Introduction to Operating Systems

Shell programming. Introduction to Operating Systems Shell programming Introduction to Operating Systems Environment variables Predened variables $* all parameters $# number of parameters $? result of last command $$ process identier $i parameter number

More information

UNIX shell scripting

UNIX shell scripting UNIX shell scripting EECS 2031 Summer 2014 Przemyslaw Pawluk June 17, 2014 What we will discuss today Introduction Control Structures User Input Homework Table of Contents Introduction Control Structures

More information

9.2 Linux Essentials Exam Objectives

9.2 Linux Essentials Exam Objectives 9.2 Linux Essentials Exam Objectives This chapter will cover the topics for the following Linux Essentials exam objectives: Topic 3: The Power of the Command Line (weight: 10) 3.3: Turning Commands into

More information

CS Unix Tools & Scripting

CS Unix Tools & Scripting Cornell University, Spring 2014 1 February 24, 2014 1 Slides evolved from previous versions by Hussam Abu-Libdeh and David Slater A note on awk for (item in array) The order in which items are returned

More information

Bash shell programming Part II Control statements

Bash shell programming Part II Control statements Bash shell programming Part II Control statements Deniz Savas and Michael Griffiths 2005-2011 Corporate Information and Computing Services The University of Sheffield Email M.Griffiths@sheffield.ac.uk

More information

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Essentials for Scientific Computing: Bash Shell Scripting Day 3 Essentials for Scientific Computing: Bash Shell Scripting Day 3 Ershaad Ahamed TUE-CMS, JNCASR May 2012 1 Introduction In the previous sessions, you have been using basic commands in the shell. The bash

More information

Computer Systems and Architecture

Computer Systems and Architecture Computer Systems and Architecture UNIX Scripting Bart Meyers University of Antwerp August 29, 2012 Outline Basics Conditionals Loops Advanced Exercises Shell scripts Grouping commands into a single file

More information

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

UNIX Shell Scripts. What Is a Shell? The Bourne Shell. Executable Files. Executable Files: Example. Executable Files (cont.) CSE 2031 Fall 2012 What Is a Shell? UNIX Shell Scripts CSE 2031 Fall 2012 A program that interprets your requests to run other programs Most common Unix shells: Bourne shell (sh) C shell (csh - tcsh) Korn shell (ksh) Bourne-again

More information

Shells & Shell Programming (Part B)

Shells & Shell Programming (Part B) Shells & Shell Programming (Part B) Software Tools EECS2031 Winter 2018 Manos Papagelis Thanks to Karen Reid and Alan J Rosenthal for material in these slides CONTROL STATEMENTS 2 Control Statements Conditional

More information

Linux shell programming for Raspberry Pi Users - 2

Linux shell programming for Raspberry Pi Users - 2 Linux shell programming for Raspberry Pi Users - 2 Sarwan Singh Assistant Director(S) NIELIT Chandigarh 1 SarwanSingh.com Education is the kindling of a flame, not the filling of a vessel. - Socrates SHELL

More information

CSC 2500: Unix Lab Fall 2016

CSC 2500: Unix Lab Fall 2016 CSC 2500: Unix Lab Fall 2016 Control Statements in Shell Scripts: Decision Mohammad Ashiqur Rahman Department of Computer Science College of Engineering Tennessee Tech University Agenda User Input Special

More information

bash Execution Control COMP2101 Winter 2019

bash Execution Control COMP2101 Winter 2019 bash Execution Control COMP2101 Winter 2019 Bash Execution Control Scripts commonly can evaluate situations and make simple decisions about actions to take Simple evaluations and actions can be accomplished

More information

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

Preview. Review. The test, or [, command. Conditions. The test, or [, command (String Comparison) The test, or [, command (String Comparison) 2/5/2019 Review Shell Scripts How to make executable How to change mode Shell Syntax Variables Quoting Environment Variables Parameter Variables Preview Conditions The test, or [ Command if statement if--if statement

More information

Bourne Shell Programming Topics Covered

Bourne Shell Programming Topics Covered Bourne Shell Programming Topics Covered Shell variables Using Quotes Arithmetic On Shell Passing Arguments Testing conditions Branching if-else, if-elif, case Looping while, for, until break and continue

More information

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

Програмиранев UNIX среда Програмиранев UNIX среда Използванена команден шел и създаванена скриптове: tcsh, bash, awk, python Shell programming As well as using the shell to run commands you can use its built-in programming language

More information

Basic Linux (Bash) Commands

Basic Linux (Bash) Commands Basic Linux (Bash) Commands Hint: Run commands in the emacs shell (emacs -nw, then M-x shell) instead of the terminal. It eases searching for and revising commands and navigating and copying-and-pasting

More information

Shell Programming (bash)

Shell Programming (bash) Shell Programming Shell Programming (bash) Commands run from a file in a subshell A great way to automate a repeated sequence of commands. File starts with #!/bin/bash absolute path to the shell program

More information

Vi & Shell Scripting

Vi & Shell Scripting Vi & Shell Scripting Comp-206 : Introduction to Week 3 Joseph Vybihal Computer Science McGill University Announcements Sina Meraji's office hours Trottier 3rd floor open area Tuesday 1:30 2:30 PM Thursday

More information

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

Advanced Unix Programming Module 03 Raju Alluri spurthi.com Advanced Unix Programming Module 03 Raju Alluri askraju @ spurthi.com Advanced Unix Programming: Module 3 Shells & Shell Programming Environment Variables Writing Simple Shell Programs (shell scripts)

More information

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

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Shell Scripting. Winter 2019 CSCI 2132: Software Development Shell Scripting Norbert Zeh Faculty of Computer Science Dalhousie University Winter 2019 Reading Glass and Ables, Chapter 8: bash Your Shell vs Your File Manager File manager

More information

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

While Statement Examples. While Statement (35.15) Until Statement (35.15) Until Statement Example While Statement (35.15) General form. The commands in the loop are performed while the condition is true. while condition one-or-more-commands While Statement Examples # process commands until a stop is

More information

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

1. Hello World Bash Shell Script. Last Updated on Wednesday, 13 April :03 1 of 18 21/10/2554 9:39 Bash scripting Tutorial tar -czf myhome_directory.tar.gz /home/linuxcong Last Updated on Wednesday, 13 April 2011 08:03 Article Index 1. Hello World Bash Shell Script 2. Simple

More information

Last Time. on the website

Last Time. on the website Last Time on the website Lecture 6 Shell Scripting What is a shell? The user interface to the operating system Functionality: Execute other programs Manage files Manage processes Full programming language

More information

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017 Linux Shell Scripting Linux System Administration COMP2018 Summer 2017 What is Scripting? Commands can be given to a computer by entering them into a command interpreter program, commonly called a shell

More information

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

OPERATING SYSTEMS LAB LAB # 6. I/O Redirection and Shell Programming. Shell Programming( I/O Redirection and if-else Statement) P a g e 1 OPERATING SYSTEMS LAB LAB 6 I/O Redirection and Shell Programming Lab 6 Shell Programming( I/O Redirection and if-else Statement) P a g e 2 Redirection of Standard output/input i.e. Input - Output

More information

Computer Systems and Architecture

Computer Systems and Architecture Computer Systems and Architecture Stephen Pauwels UNIX Scripting Academic Year 2018-2019 Outline Basics Conditionals Loops Advanced Exercises Shell Scripts Grouping commands into a single file Reusability

More information

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

28-Nov CSCI 2132 Software Development Lecture 33: Shell Scripting. 26 Shell Scripting. Faculty of Computer Science, Dalhousie University Lecture 33 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 33: Shell Scripting 28-Nov-2018 Location: Chemistry 125 Time: 12:35 13:25 Instructor: Vla Keselj

More information

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Lecture 5. Essential skills for bioinformatics: Unix/Linux Lecture 5 Essential skills for bioinformatics: Unix/Linux UNIX DATA TOOLS Text processing with awk We have illustrated two ways awk can come in handy: Filtering data using rules that can combine regular

More information

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

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers A Big Step Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Copyright 2006 2009 Stewart Weiss What a shell really does Here is the scoop on shells. A shell is a program

More information

Bash scripting basics

Bash scripting basics Bash scripting basics prepared by Anatoliy Antonov for ESSReS community September 2012 1 Outline Definitions Foundations Flow control References and exercises 2 Definitions 3 Definitions Script - [small]

More information

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

Unix Scripts and Job Scheduling. Overview. Running a Shell Script Unix Scripts and Job Scheduling Michael B. Spring Department of Information Science and Telecommunications University of Pittsburgh spring@imap.pitt.edu http://www.sis.pitt.edu/~spring Overview Shell Scripts

More information

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

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Scripting Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers Adapted from Practical Unix and Programming Hunter College Copyright 2006 2009 Stewart Weiss What a shell

More information

Assignment clarifications

Assignment clarifications Assignment clarifications How many errors to print? at most 1 per token. Interpretation of white space in { } treat as a valid extension, involving white space characters. Assignment FAQs have been updated.

More information

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo SHELL SCRIPT BASIC UNIX Programming 2014 Fall by Euiseong Seo Shell Script Interactive shell sequentially executes a series of commands Some tasks are repetitive and automatable They are what programs

More information

CS214 Advanced UNIX Lecture 4

CS214 Advanced UNIX Lecture 4 CS214 Advanced UNIX Lecture 4 March 2, 2005 Passing arguments to scripts When you invoke a command like > cat f1 f2 f3 f1, f2, f3 are all arguments you pass to the cat command. Many times in your script

More information

Lecture 02 The Shell and Shell Scripting

Lecture 02 The Shell and Shell Scripting Lecture 02 The Shell and Shell Scripting In this course, we need to be familiar with the "UNIX shell". We use it, whether bash, csh, tcsh, zsh, or other variants, to start and stop processes, control the

More information

Useful Unix Commands Cheat Sheet

Useful Unix Commands Cheat Sheet Useful Unix Commands Cheat Sheet The Chinese University of Hong Kong SIGSC Training (Fall 2016) FILE AND DIRECTORY pwd Return path to current directory. ls List directories and files here. ls dir List

More information

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.

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. 1. Write a shell script to check whether the name passed as rst argument is the name of a le or directory. Ans: #!/bin/bash if [ -f $1 ] echo "$1 is a le" echo "$1 is not a le" 2. Write a shell script

More information

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

Prof. Navrati Saxena TA: R. Sachan Bharat Jyoti Ranjan Prof. Navrati Saxena navrati@ece.skku.ac.kr TA: R. Sachan rochak@skku.edu Bharat Jyoti Ranjan Decision making Loops de-bugging the shell script 2 What is decision.? How to take decision? Knows only 0(zero)

More information

Shell Start-up and Configuration Files

Shell Start-up and Configuration Files ULI101 Week 10 Lesson Overview Shell Start-up and Configuration Files Shell History Alias Statement Shell Variables Introduction to Shell Scripting Positional Parameters echo and read Commands if and test

More information

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo SHELL SCRIPT BASIC UNIX Programming 2015 Fall by Euiseong Seo Shell Script! Interactive shell sequentially executes a series of commands! Some tasks are repetitive and automatable! They are what programs

More information

Assignment 3, Due October 4

Assignment 3, Due October 4 Assignment 3, Due October 4 1 Summary This assignment gives you practice with writing shell scripts. Shell scripting is also known as bash programming. Your shell is bash, and when you write a shell script

More information

Practical 04: UNIX / LINUX Shell Programming 1

Practical 04: UNIX / LINUX Shell Programming 1 Operating Systems: UNIX / LINUX Shell Programming 1 Page 1 Practical 04: UNIX / LINUX Shell Programming 1 Name: Roll no: Date: Section: Practical Objectives 1. To study about the Unix Shell Programming

More information

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

CSCI 211 UNIX Lab. Shell Programming. Dr. Jiang Li. Jiang Li, Ph.D. Department of Computer Science CSCI 211 UNIX Lab Shell Programming Dr. Jiang Li Why Shell Scripting Saves a lot of typing A shell script can run many commands at once A shell script can repeatedly run commands Help avoid mistakes Once

More information

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

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012 Cisco IOS Shell Last Updated: December 14, 2012 The Cisco IOS Shell (IOS.sh) feature provides shell scripting capability to the Cisco IOS command-lineinterface (CLI) environment. Cisco IOS.sh enhances

More information

Essential Skills for Bioinformatics: Unix/Linux

Essential Skills for Bioinformatics: Unix/Linux Essential Skills for Bioinformatics: Unix/Linux SHELL SCRIPTING Overview Bash, the shell we have used interactively in this course, is a full-fledged scripting language. Unlike Python, Bash is not a general-purpose

More information

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 Command Lists A command is a sequence of commands separated by the operators ; & && and ; is used to simply execute commands in

More information

My Favorite bash Tips and Tricks

My Favorite bash Tips and Tricks 1 of 6 6/18/2006 7:44 PM My Favorite bash Tips and Tricks Prentice Bisbal Abstract Save a lot of typing with these handy bash features you won't find in an old-fashioned UNIX shell. bash, or the Bourne

More information

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St. Introduction to Perl c Sanjiv K. Bhatia Department of Mathematics & Computer Science University of Missouri St. Louis St. Louis, MO 63121 Contents 1 Introduction 1 2 Getting started 1 3 Writing Perl scripts

More information

Title:[ Variables Comparison Operators If Else Statements ]

Title:[ Variables Comparison Operators If Else Statements ] [Color Codes] Environmental Variables: PATH What is path? PATH=$PATH:/MyFolder/YourStuff?Scripts ENV HOME PWD SHELL PS1 EDITOR Showing default text editor #!/bin/bash a=375 hello=$a #No space permitted

More information

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

Bash scripting Tutorial. Hello World Bash Shell Script. Super User Programming & Scripting 22 March 2013 Bash scripting Tutorial Super User Programming & Scripting 22 March 2013 Hello World Bash Shell Script First you need to find out where is your bash interpreter located. Enter the following into your command

More information

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

COMP 2718: Shell Scripts: Part 1. By: Dr. Andrew Vardy COMP 2718: Shell Scripts: Part 1 By: Dr. Andrew Vardy Outline Shell Scripts: Part 1 Hello World Shebang! Example Project Introducing Variables Variable Names Variable Facts Arguments Exit Status Branching:

More information

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

Introduction to Linux Basics Part II. Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala Introduction to Linux Basics Part II 1 Georgia Advanced Computing Resource Center University of Georgia Suchitra Pakala pakala@uga.edu 2 Variables in Shell HOW DOES LINUX WORK? Shell Arithmetic I/O and

More information

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

Unix Guide. Meher Krishna Patel. Created on : Octorber, 2017 Last updated : December, More documents are freely available at PythonDSP Unix Guide Meher Krishna Patel Created on : Octorber, 2017 Last updated : December, 2017 More documents are freely available at PythonDSP Table of contents Table of contents i 1 Unix commands 1 1.1 Unix

More information

Linux shell & shell scripting - II

Linux shell & shell scripting - II IBS 574 - Computational Biology & Bioinformatics Spring 2018, Tuesday (02/01), 2:00-4:00PM Linux shell & shell scripting - II Ashok R. Dinasarapu Ph.D Scientist, Bioinformatics Dept. of Human Genetics,

More information

Shells and Shell Programming

Shells and Shell Programming Shells and Shell Programming 1 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

More information

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

Shell Programming. Introduction to Linux. Peter Ruprecht  Research CU Boulder Introduction to Linux Shell Programming Peter Ruprecht peter.ruprecht@colorado.edu www.rc.colorado.edu Downloadable Materials Slides and examples available at https://github.com/researchcomputing/ Final_Tutorials/

More information

Wildcards and Regular Expressions

Wildcards and Regular Expressions CSCI 2132: Software Development Wildcards and Regular Expressions Norbert Zeh Faculty of Computer Science Dalhousie University Winter 2019 Searching Problem: Find all files whose names match a certain

More information

A control expression must evaluate to a value that can be interpreted as true or false.

A control expression must evaluate to a value that can be interpreted as true or false. Control Statements Control Expressions A control expression must evaluate to a value that can be interpreted as true or false. How a control statement behaves depends on the value of its control expression.

More information

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

Scripting. More Shell Scripts Loops. Adapted from Practical Unix and Programming Hunter College Scripting More Shell Scripts Loops Adapted from Practical Unix and Programming Hunter College Copyright 2006 2009 Stewart Weiss Loops: for The for script version 1: 1 #!/bin/bash 2 for i in 1 2 3 3 do

More information

Shells and Shell Programming

Shells and Shell Programming 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

More information

What is Bash Shell Scripting?

What is Bash Shell Scripting? What is Bash Shell Scripting? A shell script is a script written for the shell, or command line interpreter, of an operating system. The shell is often considered a simple domain-specic programming language.

More information

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

CSE 390a Lecture 6. bash scrip'ng con'nued; remote X windows; unix 'dbits CSE 390a Lecture 6 bash scrip'ng con'nued; remote X windows; unix 'dbits slides created by Marty Stepp, modified by Jessica Miller h>p://www.cs.washington.edu/390a/ 1 Lecture summary more shell scrip'ng

More information

Chapter 5 The Bourne Shell

Chapter 5 The Bourne Shell Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, 2003 Notes by Bing Li Creating/Assigning a Variable Name=value Variable created

More information

Grep and Shell Programming

Grep and Shell Programming Grep and Shell Programming Comp-206 : Introduction to Software Systems Lecture 7 Alexandre Denault Computer Science McGill University Fall 2006 Teacher's Assistants Michael Hawker Monday, 14h30 to 16h30

More information

Lab 4: Shell scripting

Lab 4: Shell scripting Lab 4: Shell scripting Comp Sci 1585 Data Structures Lab: Tools Computer Scientists Outline 1 2 3 4 5 6 What is shell scripting good? are the duct tape and bailing wire of computer programming. You can

More information

Chapter 4. Unix Tutorial. Unix Shell

Chapter 4. Unix Tutorial. Unix Shell Chapter 4 Unix Tutorial Users and applications interact with hardware through an operating system (OS). Unix is a very basic operating system in that it has just the essentials. Many operating systems,

More information

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

/smlcodes /smlcodes /smlcodes. Shell Scripting TUTORIAL. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation /smlcodes /smlcodes /smlcodes Shell Scripting TUTORIAL Small Codes Programming Simplified A SmlCodes.Com Small presentation In Association with Idleposts.com For more tutorials & Articles visit SmlCodes.com

More information

Common File System Commands

Common File System Commands Common File System Commands ls! List names of all files in current directory ls filenames! List only the named files ls -t! List in time order, most recent first ls -l! Long listing, more information.

More information

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

Unix as a Platform Exercises + Solutions. Course Code: OS 01 UNXPLAT Unix as a Platform Exercises + Solutions Course Code: OS 01 UNXPLAT Working with Unix Most if not all of these will require some investigation in the man pages. That's the idea, to get them used to looking

More information

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

More Scripting and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1 More Scripting and Regular Expressions Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 Regular Expression Summary Regular Expression Examples Shell Scripting 2 Do not confuse filename globbing

More information

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

CSE 390a Lecture 6. bash scripting continued; remote X windows; unix tidbits CSE 390a Lecture 6 bash scripting continued; remote X windows; unix tidbits slides created by Marty Stepp, modified by Jessica Miller and Ruth Anderson http://www.cs.washington.edu/390a/ 1 Lecture summary

More information

Practice 5 Batch & Shell Programming

Practice 5 Batch & Shell Programming Computer Programming Practice (2008 Fall) Practice 5 Batch & Shell Programming 2008. 10. 8 Contents Batch & Shell Programming Introduction Comment Variables Functions Quoting Flow control Test operations

More information

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

Table of Contents. 1. Introduction. 2. Environment. 3. Shell Scripting Shell Scripting Introduction Unix Shell Scripting Guide For MPPKVVCL Programmers Table of Contents 1. Introduction... 1 2. Environment... 1 3. Shell Scripting... 1 3.1. Shell Scripting Introduction... 1 3.2. Shell Scripting Basics....

More information

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

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 Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who

More information

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017 Command Lists A command is a sequence of commands separated by the operators ; & && and ; is used to simply execute commands in

More information

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

EECS2301. Special variables. $* and 3/14/2017. Linux/Unix part 2 Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who

More information

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

Shell script. Shell Scripts. A shell script contains a sequence of commands in a text file. Shell is an command language interpreter. Shell Scripts A shell script contains a sequence of commands in a text file. Shell is an command language interpreter. Shell executes commands read from a file. Shell is a powerful programming available

More information

GNU Bash. an introduction to advanced usage. James Pannacciulli Systems Engineer.

GNU Bash. an introduction to advanced usage. James Pannacciulli Systems Engineer. Concise! GNU Bash http://talk.jpnc.info/bash_lfnw_2017.pdf an introduction to advanced usage James Pannacciulli Systems Engineer Notes about the presentation: This talk assumes you are familiar with basic

More information

S E C T I O N O V E R V I E W

S E C T I O N O V E R V I E W AN INTRODUCTION TO SHELLS S E C T I O N O V E R V I E W Continuing from last section, we are going to learn about the following concepts: understanding quotes and escapes; considering the importance of

More information

More Scripting Techniques Scripting Process Example Script

More Scripting Techniques Scripting Process Example Script More Scripting Techniques Scripting Process Example Script 1 arguments to scripts positional parameters input using read exit status test program, also known as [ if statements error messages 2 case statement

More information

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

EECS 470 Lab 5. Linux Shell Scripting. Friday, 1 st February, 2018 EECS 470 Lab 5 Linux Shell Scripting Department of Electrical Engineering and Computer Science College of Engineering University of Michigan Friday, 1 st February, 2018 (University of Michigan) Lab 5:

More information

Shell Programming (Part 2)

Shell Programming (Part 2) i i Systems and Internet Infrastructure Security Institute for Networking and Security Research Department of Computer Science and Engineering Pennsylvania State University, University Park, PA Shell Programming

More information

More Scripting Todd Kelley CST8207 Todd Kelley 1

More Scripting Todd Kelley CST8207 Todd Kelley 1 More Scripting Todd Kelley kelleyt@algonquincollege.com CST8207 Todd Kelley 1 Arithmetic Output with printf Input from a file from a command CST8177 Todd Kelley 2 A script can test whether or not standard

More information

ACS Unix (Winter Term, ) Page 92

ACS Unix (Winter Term, ) Page 92 ACS-294-001 Unix (Winter Term, 2016-2017) Page 92 The Idea of a Link When Unix creates a file, it does two things: 1. Set space on a disk to store data in the file. 2. Create a structure called an inode

More information

Introduction to Shell Scripting

Introduction to Shell Scripting Introduction to Shell Scripting Lecture 1. Shell scripts are small programs. They let you automate multi-step processes, and give you the capability to use decision-making logic and repetitive loops. 2.

More information

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

Understanding bash. Prof. Chris GauthierDickey COMP 2400, Fall 2008 Understanding bash Prof. Chris GauthierDickey COMP 2400, Fall 2008 How does bash start? It begins by reading your configuration files: If it s an interactive login-shell, first /etc/profile is executed,

More information