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

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

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

Shell programming. Introduction to Operating Systems

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

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

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

Assignment clarifications

Shells & Shell Programming (Part B)

Shells and Shell Programming

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Shells and Shell Programming

Useful Unix Commands Cheat Sheet

Shell Programming (bash)

UNIX System Programming Lecture 3: BASH Programming

Linux shell programming for Raspberry Pi Users - 2

COMP 4/6262: Programming UNIX

bash Execution Control COMP2101 Winter 2019

A shell can be used in one of two ways:

Bourne Shell Reference

Conditional Control Structures. Dr.T.Logeswari

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

UNIX shell scripting

Last Time. on the website

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

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

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

Advanced Unix Programming Module 03 Raju Alluri spurthi.com

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?

Computer Systems and Architecture

More Scripting Techniques Scripting Process Example Script

Computer Systems and Architecture

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

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

UNIX 및실습. 13 장보충 bash(2)

Log into Linux Reminders: Homework 1 due today; Homework 2 due on Thursday Questions?

Week 6 Lesson 1: Control-Flow Statements (Continued)

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

Bourne Shell Programming Topics Covered

Title:[ Variables Comparison Operators If Else Statements ]

Essentials for Scientific Computing: Bash Shell Scripting Day 3

INTRODUCTION TO SHELL SCRIPTING ITPART 2

Vi & Shell Scripting

Lab 4: Shell scripting

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

CS Unix Tools & Scripting

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

85321, Systems Administration Chapter 6: The shell

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

Windshield. Language Reference Manual. Columbia University COMS W4115 Programming Languages and Translators Spring Prof. Stephen A.

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

9.2 Linux Essentials Exam Objectives

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

Processes and Shells

Manual Shell Script Linux If Not Equals. Statement >>>CLICK HERE<<<

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

CSE 341 Section Handout #6 Cheat Sheet

Bash scripting basics

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

bash, part 3 Chris GauthierDickey

CSC 2500: Unix Lab Fall 2016

Part 1: Basic Commands/U3li3es

Shell Programming (Part 2)

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

Chapter 5 The Bourne Shell

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

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

Bash shell programming Part II Control statements

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

Chapter 9. Shell and Kernel

bash 3.x Advanced Shell Scripting

30 Bash Script Examples

Types, Operators and Expressions

GNU Bash. An Introduction to Advanced Usage. James Pannacciulli Systems (mt) Media Temple

More Scripting Todd Kelley CST8207 Todd Kelley 1

Basic Linux (Bash) Commands

Types, Operators and Expressions

File Handling in C. EECS 2031 Fall October 27, 2014

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

Linux shell scripting Getting started *

SANJAY GHODAWAT POLYTECHNIC

Advanced Bash Scripting

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

Environment Variables

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

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

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

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

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

Linux Bash Shell Scripting

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

Mills HPC Tutorial Series. Linux Basics II

Output with printf Input. from a file from a command arguments from the command read

EECS 2031E. Software Tools Prof. Mokhtar Aboelaze

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

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

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

Shell Programming (ch 10)

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

Advanced Bash Scripting

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

Transcription:

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 did EECS2301 Linux/Unix Part 3 These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University 1 Example for SCRIPT in /path/to/scripts/dir/* do if [ -f $SCRIPT -a -x $SCRIPT ] then $SCRIPT fi 2 Testing #!/usr/bin/bash # cookbook filename: strvsnum # # the old string vs. numeric comparison dilemma # VAR1=" 05 " VAR2="5 printf "%s" "do they -eq as equal? " $ bash strvsnum do they -eq as equal? YES do they = as equal? NO $ if [ "$VAR1" -eq "$VAR2" ] then echo YES else echo NO fi printf "%s" "do they = as equal? " if [ "$VAR1" = "$VAR2" ] then echo YES else echo NO fi 3 1

Testing Dealing with failures You want to do 2 commands, but the second one depends on the success of the first cd testdir then rm *.dat One solution is to use the double ampersand operator (run the second only if the first succeed $cd testdir 2>/dev/null && rm *.dat OR Arithmetic cd testdir expansion if(($?)); then rem *.dat; fi 4 Testing Dealing with failures Another way is to set the e option exit the first time encounter error (i.e. a non zero exit status) from any command in the script except while loops and if statement set e cd testdir rm *.dat 5 Conditions We want to do more than just execute other programs command1 && command2 executes command1 - if command1 has an exit status of 0 (true), then command2 is executed Like C, this is short-circuiting 6 2

Conditions We have an or operator as well: command1 command2 executes command1 - if command1 has an exit status of non-zero (false), then command2 is executed Note that there is no space between the characters in and && 7 for variable in list_of_items do command1 command2... last_command 8 for filename in * do echo $filename for filename in *.doc do echo "Copying $filename to $filename.bak" cp $filename $filename.bak 9 3

for i in 1 2 3 4 5 6 7 8 9 10 do echo -n "...$i" echo # Clean up for next shell prompt 10 # Counts by looping for a fixed number of times # Note do on same line requires semicolon. for i in 1 2 3 4 5 6 7 8 9 10; do echo -n "...$i" echo # Output newline Counts by looping for a fixed number of times # Note do on same line requires semicolon. for i in 1 2 3 4 5 6 7 8 9 10; do echo -n "...$i" sleep 5 echo # Output newline 11 # Counts backwards for i in 10 9 8 7 6 5 4 3 2 1 do echo -n "...$i" echo # Output new line echo "Blast off!" $ sh counter2...10...9...8...7...6...5...4...3...2...1 Blast off! 12 4

for x in hello there world do echo next word is $x Output: next word is hello next word is there next word is world 13 Use outputs of another program as the word list using back-quotes for user in `cut -d: -f1 < /etc/passwd` do echo n "$user:" finger $user 14 # C-language-like for loop. # Must be run with bash. max=10 for ((i=1; i <= max ; i++)) do echo -n "$i..." echo 15 5

Redirection What is that? ls /fred > /dev/null 2> /dev/null 16 While Example: reads every line from stdin while read l do echo line: $l 17 While Reads from a file instead while read line do echo line: $line <file Or run in subshell (while read line; do; echo line:$line; ) <file 18 6

Until Same as while but test is inverted until test-cmd do command Execute command while test-cmd is false 19 Command line argument Special form of for that implicitly loops over all command line arguments to script for x do x loops over all arguments 20 Break and Continue Usual loop escape mechanisms: break [n] continue [n] The optional integer n specifies how many levels should be broken or continued. 21 7

case esac Like a switch statement case string in expr1 ) command ;; expr2 ) command ;; esac 22 case esac Note that each case must end with ;; Unlike C: no fall-through Take first matching case then skip to bottom Expressions are special - wildcards plus, where - means or "hello goodbye" matches hello or goodbye 23 Example Mycal program In class discussion 24 8

#!/cs/local/bin/sh case $# in 0) set `date`; m=$2; y=$6;; 1) m=$1; set `date`; y=$6;; 2) m=$1; y=$2;; esac case $m in jan* Jan*) m=1;; feb* Feb*) m=2;; mar* Mar*) m=3;; apr* Apr*) m=4;; may* May*) m=5;; jun* Jun*) m=6;; jul* Jul*) m=7;; aug* Aug*) m=8;; sep* Sep*) m=9;; oct* Oct*) m=10;; nov* Nov*) m=11;; dec* Dec*) m=12;; [1-9] 10 11 12) ;; *) y=$m;m="";; esac /usr/bin/cal $m $y sh-4.1$ sh_case.sh March 2016 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 sh-4.1$ sh_case.sh march March 2016 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 sh-4.1$ sh_case.sh march 2016 March 2016 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 25 Shift Built-in command: shift moves all arguments down 1 $2 is moved into $1, $3 is moved into $2, etc. $1 is thrown away 26 Shift #!/bin/sh while [ $# -gt 0 ]; do echo "processing arg: $1" shift 27 9

Shell Functions We can also write functions in the shell name() { commands } Parentheses () are always empty Note: no return type, no arguments declared Call via: name [arg] [arg] 28 Shell Functions Shell functions can take any number of arguments and return an exit status Arguments use the command-line argument syntax ($1, $2, etc) shift works here as well! Return the exit status of the function 29 Shell Functions allfiles() { # true if all args are files for x in $*; do if [! -f $x ]; then return 1 fi return 0 } 30 10

Shell Functions A function must be defined before we can use it We call it like any other command allfiles file1 dir1 file2 No parentheses in function call Effectively creates a new (temporary) shell command 31 Shell Functions #!/bin/sh inc_a() { # Increment A by 1 A=`expr $A + 1` } A=1 while [ $A -le 10 ] do echo $A inc_a 32 Shell Functions #!/bin/sh x="1111" f1() { x="hello" echo from function $x } f1 echo from main $x from function Hello from main 111 33 11

Environmental variables All shell variables are local to the shell To change any environment variable need to export them. Env. variables visible in programs via etenv(3c). Syntax: variable=value -- normal assignment export variable Shell command env lists all env. variables 35 12