COMP 4/6262: Programming UNIX

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

Bourne Shell Reference

A shell can be used in one of two ways:

Assignment clarifications

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

Shell programming. Introduction to Operating Systems

Bash scripting basics

Lab 4: Shell scripting

Shell Programming (bash)

Control Structures. CIS 118 Intro to LINUX

Shells and Shell Programming

Shells and Shell Programming

Conditional Control Structures. Dr.T.Logeswari

bash Execution Control COMP2101 Winter 2019

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

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

Bourne Shell Programming Topics Covered

Computer Systems and Architecture

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

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

CS Unix Tools & Scripting

Computer Systems and Architecture

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Linux shell programming for Raspberry Pi Users - 2

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

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

Vi & Shell Scripting

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Shell Start-up and Configuration Files

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

Bash shell programming Part II Control statements

UNIX Shell Programming

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

Bash command shell language interpreter

CSC 2500: Unix Lab Fall 2016

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

9.2 Linux Essentials Exam Objectives

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

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

UNIX shell scripting

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

Last Time. on the website

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

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

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

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

Processes and Shells

Linux 系统介绍 (III) 袁华

Grep and Shell Programming

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

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

Chapter 5 The Bourne Shell

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

Title:[ Variables Comparison Operators If Else Statements ]

Lecture 5. Essential skills for bioinformatics: Unix/Linux

Shells & Shell Programming (Part B)

Shell Programming (Part 2)

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

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

Essential Skills for Bioinformatics: Unix/Linux

5/20/2007. Touring Essential Programs

Chapter 9. Shell and Kernel

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

What is Bash Shell Scripting?

More Scripting Techniques Scripting Process Example Script

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

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

Linux Command Line Interface. December 27, 2017

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

UNIX System Programming Lecture 3: BASH Programming

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

Lecture 4. Log into Linux Reminder: Homework 1 due today, 4:30pm Homework 2 out, due next Tuesday Project 1 out, due next Thursday Questions?

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

Useful Unix Commands Cheat Sheet

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

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

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

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

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

Basic Linux (Bash) Commands

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

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

Shell Scripting. Xiaoxu Guan High Performance Computing, LSU February 17, 2016

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

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

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

Basic Shell Scripting

COMS 6100 Class Notes 3

Shell Programming (ch 10)

DAVE LIDDAMENT INTRODUCTION TO BASH

Practice 5 Batch & Shell Programming

Introduction: What is Unix?

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

Linux shell scripting Getting started *

Shell Control Structures

EECS2301. Lab 1 Winter 2016

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

Transcription:

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 debug

the relationship of shell functionality Korn Shell Bourne Shell common core C Shell common core

core shell functionality built-in commands scripts variables (local, environment) redirection wildcards pipes sequences subshells background processing command substitution

common shells the shell you use is a matter of taste, power, compatibility, and availability C shell is better than Bourne shell for interactive work, but slightly worse in some respects for script programming Korn shell incorporates the best of features of the Bourne shell and C shells Bash also best of all worlds, includes features from all the other major shells Bourne shell comes with every version of UNIX Bash is the newest, is becoming the most popular, open software

Bash Bash Bourne Again Shell Backward compatibility with Bourne Shell Includes most useful features from C Shell and Korn Shell Shell of choice for Linux Bash Specifics Variable manipulation Command-line processing, aliases, and history Arithmetic, conditional expressions, control structures Directory stack Job control Shell functions

changing shell chsh tcsh ksh

So Many Commands Unix has a lot of commands, but there is no way it has everything What do you do if no command exists that does what you want? Build it yourself! The shell itself is actually a programming language A shell program/script consists of a sequential list of commands think of it as a listing of programs to run in order

Ways To Run Shell Executables source FILENAME /bin/bash FILENAME Make the file executable and run it as a program First line should specify which shell/interpreter to use #!/bin/bash #!/usr/bin/perl

Example Check whether a user is logged on

Command Line Parameters Just like all other Unix programs, shell scripts can read parameters off of the command line Different from user input Value is known before execution, not typed in during execution Example: >commandsfile.sh param1.test

Passing Arguments Accessing command line parameters requires special variables $0 The name of the running program $1-$9 The first nine arguments to the program $* All of the command line arguments $# The total number of command line arguments

Example Check whether a user is logged on Command file ison who grep $1 Usage./ison 6262-00

Shift only first 9 arguments are directly accessible via $1, $2,, $9 for 10 or above use ${10}, ${11}, etc. if more are supplied use shift command to access them Shifts the arguments to the left and brings new values in from the right $1=$2 $2=$3 $9=${10} $# is automatically decremented by one example: testshift

Visual Representation Of shift $1 $2 $3 $4 $5 $6 $7 $8 $9 a b c d e f g h i j shift b c d e f g h i j $1 $2 $3 $4 $5 $6 $7 $8 $9

exit status Every program and shell returns an integer value to indicate success or failure 0 - Success Any other number Failure Used to inform other programs how things went

exit status variable $? the exit status of the last command executed in a pipeline, the exist status is that of the last command in the pipe examples who grep 6262-00 echo $? who grep good echo $? echo $?

Control Flow And Conditionals Shell scripts can be more powerful than just a list of commands Sometimes you want to perform some commands only if certain conditions are true Example: Only print out a file if it is not a binary file

The test Command built-in shell command to check if certain conditions in an if command Usage: test expression test evaluate expression if the result is true, returns an exit status of 0 otherwise, the result is false, returns a nonzero exit status

Shortcut For Test Because this is used very often, a shortcut exists [] [ is the name of the command still initiates execution of the same test command, only that test expects to see a closing ] spaces after [ and before ] example test f tfile [ -f tfile ]

test operators (1) string operators string1 = string2 string1!= string2 string (return 0 if string is not null) -n string -z string (return 0 if string is null) integer operators i1 eq i2 e.g. $count eq 0 i1 ne i2 e.g. $# ne 0 i1 lt i2 i1 le i2 i1 gt i2 i1 ge i2

test examples x1= 005 x2= 10 [ $x1 = 5 ] echo $? [ $x1 -eq 5 ] [ $x2 = 10 ] [ $x2 -eq 10 ] name= test $name = 6262-00 test $name = 6262-00

test operators (2) file operators -d file exit status is 0 if file is a directory -e file exit status is 0 if file exists -f file exit status is 0 if file is a normal file -s file exit status is 0 if file is non-empty -x file exit status is 0 if file is executable

test operators (3)! Not Negates the next check Example: test! x tfile # if the file exists as exec -a And two conditions Example: test $1 eq $2 a $2 gt 5 -o Or two conditions Example: test $1 eq $2 o $2 gt 5

When Do You Use This? test is used to control the operation of your script The answer from test should guide the execution of your code one way or another Used in conditional statements

if if commandt then command command fi commandt is executed and its exit status is tested if exit 0, the commands between then and fi are executed otherwise, they are skipped

Example Edit a file safecat.sh with #!/bin/bash if [! x $1 ] ; then cat $1 fi How to run? At prompt type./safecat.sh filename

else if commandt then commands-yes else commands-no fi

Example Edit a file safecat.sh with #!/bin/bash if [! x $1 ] ; then cat $1 else echo File $1 is an executable: not printing! fi How to run? At prompt type./safecat.sh filename

elif if command1 then commands-1 elif command2 then commands-2 elif commandn then commands-n else commands fi

case case value in pattern1) commands-1 ;; pattern2) commands-2 ;; esac value is successively compared against the values pattern1, until a match is found the commands listed after the matching value, up to ;; are executed execution of case is terminated if a match is not found, none of the commands is executed

Example Edit a file casescript.sh #!/bin/bash case $1 in *.sh) echo Shell Script ;; *.txt) echo Text File ;; *) echo Some Other File ;; esac

Exercise Check whether a user is logged on ison who grep $1 on if the user is logged on, print.. is logged on otherwise print.. is not logged on check the number of arguments

Exercise print the type of the single character given as an argument: digits, uppercase letters, lowercase letters, special characters. add check: make sure just a single char is given as the argument ctype

Debug using shell s x option sh x programname arguments trace the execution of the program start a new shell to execute the program commands are printed at the terminal as they are executed, preceded by a +