Lecture 04: Bash Scripting

Similar documents
如何查看 Cache Engine 缓存中有哪些网站 /URL

ICP Enablon User Manual Factory ICP Enablon 用户手册 工厂 Version th Jul 2012 版本 年 7 月 16 日. Content 内容

Bourne Shell Reference

Linux 系统介绍 (III) 袁华

Command Dictionary CUSTOM

OTAD Application Note

Useful Unix Commands Cheat Sheet

Lecture 05: Basic Python Programming

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

Shell programming. Introduction to Operating Systems

Chapter 11 SHANDONG UNIVERSITY 1

Shells & Shell Programming (Part B)

第二小题 : 逻辑隔离 (10 分 ) OpenFlow Switch1 (PC-A/Netfpga) OpenFlow Switch2 (PC-B/Netfpga) ServerB PC-2. Switching Hub

Understanding IO patterns of SSDs

: Operating System 计算机原理与设计

COMP 4/6262: Programming UNIX

#MDCC Swift 链式语法应 用 陈乘

Machine Vision Market Analysis of 2015 Isabel Yang

UNIX shell scripting

Linux shell programming for Raspberry Pi Users - 2

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

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

UNIX System Programming Lecture 3: BASH Programming

Shells and Shell Programming

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

Basic Linux (Bash) Commands

PCU50 的整盘备份. 本文只针对操作系统为 Windows XP 版本的 PCU50 PCU50 启动硬件自检完后, 出现下面文字时, 按向下光标键 光标条停在 SINUMERIK 下方的空白处, 如下图, 按回车键 PCU50 会进入到服务画面, 如下图

Windows Batch VS Linux Shell. Jason Zhu

Previous on Computer Networks Class 18. ICMP: Internet Control Message Protocol IP Protocol Actually a IP packet

Shells and Shell Programming

Last Time. on the website

Shell Programming (bash)

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

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

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

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

上汽通用汽车供应商门户网站项目 (SGMSP) User Guide 用户手册 上汽通用汽车有限公司 2014 上汽通用汽车有限公司未经授权, 不得以任何形式使用本文档所包括的任何部分

Vi & Shell Scripting

ROOT Analysis FrameWork (II) Basics. Qipeng Hu March 22, 2015

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

实验三十三 DEIGRP 的配置 一 实验目的 二 应用环境 三 实验设备 四 实验拓扑 五 实验要求 六 实验步骤 1. 掌握 DEIGRP 的配置方法 2. 理解 DEIGRP 协议的工作过程

Multiprotocol Label Switching The future of IP Backbone Technology

CS Unix Tools & Scripting

Assignment clarifications

Technology: Anti-social Networking 科技 : 反社交网络

A shell can be used in one of two ways:

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

Computer Systems and Architecture

9.2 Linux Essentials Exam Objectives

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

nbns-list netbios-type network next-server option reset dhcp server conflict 1-34

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

Linux 系统介绍 (I) 袁华

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

1. Features. 2,Block diagram. 3. Outline dimension V power supply. 3. Assembled with 20 x 4 character displays

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

Safe Memory-Leak Fixing for C Programs

Computer Systems and Architecture

Lecture 02 The Shell and Shell Scripting

Processes and Shells

三 依赖注入 (dependency injection) 的学习

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

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

CSC 2500: Unix Lab Fall 2016

组播路由 - MSDP 和 PIM 通过走

Altera 器件高级特性与应用 内容安排 时钟管理 时钟管理 片内存储器 数字信号处理 高速差分接口 高速串行收发器. 时钟偏斜 (skew): 始终分配到系统中到达各个时钟末端 ( 器件内部触发器的时钟输入端 ) 的时钟相位不一致的现象 抖动 : 时钟边沿的输出位置和理想情况存在一定的误差

Bash scripting basics

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

Logitech G302 Daedalus Prime Setup Guide 设置指南

Conditional Control Structures. Dr.T.Logeswari

Chapter 9. Shell and Kernel

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

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

Title:[ Variables Comparison Operators If Else Statements ]

New Media Data Analytics and Application

Basic Shell Scripting

Lecture 3 for pipelining

S 1.6V 3.3V. S Windows 2000 Windows XP Windows Vista S USB S RGB LED (PORT1 PORT2 PORT3) S I 2 C. + 表示无铅 (Pb) 并符合 RoHS 标准 JU10 JU14, JU24, JU25

Lecture 07: Python Generators, Iterators, and Decorators

Linux Bash Shell Scripting

SNMP Web Manager. User s Manual

bash Execution Control COMP2101 Winter 2019

1. DWR 1.1 DWR 基础 概念 使用使用 DWR 的步骤. 1 什么是 DWR? Direct Web Remote, 直接 Web 远程 是一个 Ajax 的框架

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

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

静态路由 : ip route ; 命令格式 router(config)#ip route ; 静态路由举例 router(config)#ip route

U-CONTROL UMX610/UMX490/UMX250. The Ultimate Studio in a Box: 61/49/25-Key USB/MIDI Controller Keyboard with Separate USB/Audio Interface

IP unnumbered 实验讲义 一. 实验目的 : 二. 实验设备 : 三. 实验拓扑 : 四. 实验内容 :

Operating Systems. Chapter 4 Threads. Lei Duan

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

Lecture 03: Regular Expression

学习沉淀成长分享 EIGRP. 红茶三杯 ( 朱 SIR) 微博 : Latest update:

Chapter 3: Introduction to SQL

XML allows your content to be created in one workflow, at one cost, to reach all your readers XML 的优势 : 只需一次加工和投入, 到达所有读者的手中

Lecture 5. Essential skills for bioinformatics: Unix/Linux

UNIX Shell Programming

Transcription:

BI296: Linux and Shell Programming Lecture 04: Bash Scripting Maoying,Wu ricket.woo@gmail.com Dept. of Bioinformatics & Biostatistics Shanghai Jiao Tong University Spring, 2018 Maoying Wu (CBB) BI296-Lec04 Spring, 2018 1 / 48

Lecture Outline Bash Programming (BASH 脚本编程 ) Bash: Introuction (BASH 发展历史与相关概念 ) Types of Variables ( 变量声明与定义 ) Conditional Statements and Flow Control( 条件结构和流程控制 ) Command Line: Arguments ( 命令行 ) BASH Functions ( 函数的定义 ) Built-in Variables and Functions ( 内置变量与函数 ) Applications of BASH(BASH 应用案例 ) Maoying Wu (CBB) BI296-Lec04 Spring, 2018 2 / 48

The first BASH scripts greeting.sh #!/bin/bash # This is the first bash implementation. # Scriptname: greetings.sh # Written by: Ricky Woo (ricket.woo@gmail.com) echo -e "What s your name and your age: " read name age echo -e "I m $name, and I m $age years old." read # grant the executable permission chmod u+x greeting.sh # run the script./greeting.sh bash greeting.sh Run the script file Maoying Wu (CBB) BI296-Lec04 Spring, 2018 3 / 48

Interpretation of BASH scripts greeting.sh #!/bin/bash # This is the first bash implementation. # Scriptname: greetings.sh # Written by: Ricky Woo (ricket.woo@gmail.com) echo -e "What s your name and your age: " read name age echo -e "I m $name, and I m $age years old." read Interpretation #!/bin/bash: Shebang to indicate the command to interpret the following scripts. #: Comments ( 注释 ). $name: Variable substitution ( 变量替换 ). read: Obtain the variable value interactively ( 交互式变量赋值 ). Maoying Wu (CBB) BI296-Lec04 Spring, 2018 4 / 48

Shells: Types and Descriptions The default shell for each user is defined in /etc/passwd. Described in the file /etc/shells. sh: Bourne Shell, light-weighted shell (UNIX) ksh: Korn shell, superset of sh csh: Berkeley UNIX C-shell tcsh: Enhanced csh. bash: Bourne Again SHell,sh+csh Maoying Wu (CBB) BI296-Lec04 Spring, 2018 5 / 48

BASH Advantages and Disadvantages Bourne Again SHell In memory of Stephen Bourne de facto standard for shell scripting sh-compatible Derive some useful features from ksh as well as csh Maoying Wu (CBB) BI296-Lec04 Spring, 2018 6 / 48

BASH Advantages and Disadvantages Bourne Again SHell In memory of Stephen Bourne de facto standard for shell scripting sh-compatible Derive some useful features from ksh as well as csh When not to use Resource-intensive tasks (sorting, hashing, recursion,...) Heavy-duty math operations (floating-point arithmetic) Precision calculations (use c++/fortran instead) Cross-platform portability required (c/java instead) Maoying Wu (CBB) BI296-Lec04 Spring, 2018 6 / 48

Variables: Declaration ( 变量声明 ) declare: syntax ( 语法 ) declare [-aaffgilrtux] [-p] [name[=value]...] -a/-a: indexed/associative array ( 索引 / 关联数组 ); -f/-f: function body/name ( 函数实体 / 函数名 ); -i: integer ( 整数 ) -l/-u: lowercase/uppercase ( 小写 / 大写字母 ) -r: read-only ( 只读 ); -g: global ( 全局变量 ) declare: examples ( 示例 ) # i : integer declare -i int # a : arrary declare -a arr # A : associative array declare -A Arr # r : read-only declare -r ro # f : Maoying function Wu (CBB) BI296-Lec04 Spring, 2018 7 / 48

Variables: Definitions ( 变量定义 ) name=value Variable Names ( 变量命名 ) name=/^[a-za-z_][a-za-z0-9_]*$/ Use interpretable variable names. Variable Assignment ( 变量赋值 ) Space is forbidden besides = operator. Note: Use double quotes to enclose the value. # i is an integer, 2 declare -i i="8/3" echo ${i} Examples ( 变量定义的例子 ) Maoying Wu (CBB) BI296-Lec04 Spring, 2018 8 / 48

Built-in Variables ( 内置变量 ) Variables Description PATH the directories of executable commands ( 可执行命令路径 ). MANPATH the directories of manuals ( 手册目录 ). FS the field separator, default. PS1, PS2, PS3 the prompt ( 提示符 ). PWD, OLDPWD the current/previous working directory ( 工作路径 ). SHELL the default shell for current user ( 当前 shell). USER, $USERNAME, $LOGNAME the current user name ( 当前用户 ). HOSTNAME the MACHINE name ( 主机名 ). HOME the home directory for the current user ( 用户家目录 ). $? the return code of the last command issued ( 退出码 ). Maoying Wu (CBB) BI296-Lec04 Spring, 2018 9 / 48

Variable Manipulation FILEPATH=/path/to/my/output.lis echo $FILEPATH echo ${FILEPATH%t*} # non-greedy tail-truncation echo ${FILEPATH%%t*} # greedy tail-truncation echo ${FILEPATH#*/} # non-greedy head-truncation echo ${FILEPATH##*/} # greedy head-truncation unset a; b=${a-5}; echo $b; unset b unset a; b=${a=5}; echo $b; unset b unset a; b=${a+5}; echo $b; unset b a=3; b=${a-5}; echo $b; unset b a=3; b=${a=5}; echo $b; unset b a=3; b=${a+5}; echo $b; unset b echo ${t:?undefined}; t=5; echo ${t:?undefined} dna="acctaggacg"; echo ${dna:3:4} # substring dna2= echo $dna rev ; echo ${dna2} # reverse Maoying Wu (CBB) BI296-Lec04 Spring, 2018 10 / 48

Exercise: Variables 1 Which of the following variable names are not legal? If not, tell why. 3x5, x=1, x+, wt-5, _xyz, #tt, CMT, echo, if, $test 2 Output the results. #!/bin/bash num1=3;num2=5 sum=$num1+$num2; echo ${sum} # sum? declare -i sum=$num1+$num2; echo ${sum} # sum? diff=$num2-$num1; echo ${diff} # diff? declare -i diff=$num2-$num1; echo ${diff} # diff? prod=$num1*$num2; echo ${prod} # product? declare -i prod=$num1*$num2; echo ${prod} # product? div=$num2/$num1; echo ${div} # division? declare -i div=$num2/$num1; echo ${div} # division? Maoying Wu (CBB) BI296-Lec04 Spring, 2018 11 / 48

Next we will talk about... 1 bash: an introduction 2 flow control conditional statements if statement loop statement 3 mathematical computing integer computation float computation 4 command line positional parameters command-line options 5 functions Maoying Wu (CBB) BI296-Lec04 Spring, 2018 12 / 48

Testing Files ( 文件的检验 ) Expression Description [ -a FILE ] TRUE if FILE exists ( 存在性 ). [ -b FILE ] TRUE if FILE is a block device file ( 块设备 ). [ -c FILE ] TRUE if FILE is character device file ( 字符设备 ). [ -d FILE ] TRUE if FILE is a directory ( 目录 ). [ -e FILE ] TRUE if FILE exists ( 存在 ). [ -f FILE ] TRUE if FILE is a regular file ( 普通文件 ). [ -g FILE ] TRUE if FILE SGID bit is set (SGID). [ -h FILE ] TRUE if FILE is a symbolic link ( 符号链接 ). [ -k FILE ] TRUE if FILE sticky bit is set ( 粘附位 ). [ -p FILE ] TRUE if FILE is a named pipe (FIFO, 管道文件 ). [ -r FILE ] TRUE if FILE is readable ( 可读 ). [ -s FILE ] TRUE if FILE size is greater than 0 ( 非空 ). [ -t FD ] TRUE if FILE DESCRIPTOR FD refers to a terminal ( 终端 ). [ -u FILE ] TRUE if FILE SUID bit is set (SUID). [ -w FILE ] TRUE if FILE is writable ( 可写 ). [ -x FILE ] TRUE if FILE is executable ( 可执行 ). [ FILE1 -nt FILE2 ] TRUE if FILE1 is newer than FILE2 ( 新 ). [ FILE1 -ot FILE2 ] TRUE if FILE1 is older than FILE2 ( 旧 ). [ FILE1 -ef FILE2 ] TRUE if FILE1 and FILE2 refers to the same ( 同 ). Maoying Wu (CBB) BI296-Lec04 Spring, 2018 13 / 48

Test of strings and numbers ( 字符串和数值的检验 ) Expression Description [ -z STRING ] TRUE if STRING is of zero-length ( 空串 ). [ -n STRING ] TRUE if STRING is non-null ( 非空 ). [ STRING ] TRUE if STRING is of non-zero length ( 非空 ). [ STRING1 == STRING2 ] TRUE if two strings are equal ( 相等 ). [ STRING1!= STRING2 ] TRUE if two strings are not equal ( 不等 ). [ STRING1 \< STRING2 ] TRUE if STRING1 is lexically less than STRING2. [ STRING1 \> STRING2 ] TRUE if STRING1 is lexically greater than STRING2. [ ARG1 op ARG2 ] Arithmetic binary comparison ( 数值比较 ). op=(-eq -ne -lt -le -ge -gt) [! EXPR ] TRUE if EXPR is FALSE ( 逻辑反 ). [ (EXPR)] Returns the value of EXPR. [ EXPR1 -a EXPR2 ] Logical AND ( 逻辑与 ). [ EXPR1 -o EXPR2 ] Logical OR ( 逻辑或 ). Maoying Wu (CBB) BI296-Lec04 Spring, 2018 14 / 48

Next we will talk about... 1 bash: an introduction 2 flow control conditional statements if statement loop statement 3 mathematical computing integer computation float computation 4 command line positional parameters command-line options 5 functions Maoying Wu (CBB) BI296-Lec04 Spring, 2018 15 / 48

Conditional Statement: if if [ cond_statement1 ]; then do_something elif [ cond_statement2 ]; then do_other_thing else do_else_thing fi Syntax Example #!/bin/bash read -p "Please input a score: " score if [ $score -gt 90 ]; then echo "You got A" elif [ $score -gt 75 ]; then echo "You got B" elif [ $score -gt 60 ]; then echo "You got C" else echo "You failed" fi Maoying Wu (CBB) BI296-Lec04 Spring, 2018 16 / 48

Three Conditional Expressions ( 条件表达式 ) [ EXPR ] [[ EXPR ]] test EXPR Word splitting Yes No Yes Pathname expansion Yes No Yes Pattern globs No Yes No work splitting: using double quotes Pattern globbing ( 模式匹配 ) var="split word"; [ $var == "split word" ]; echo $? # ERROR [ "$var" == "split word" ]; echo $? # WORK [[ $var == "split word" ]]; echo $? # WORK [[ "$var" == "split word" ]]; echo $? # WORK test $var == "split word"; echo $? # ERROR test "$var" == "split word"; echo $? # WORK [ "expression" == expr* ]; echo $? [[ "expression" == expr* ]]; echo $? test "expression" == expr*; echo $? pathname expansion ( 路径扩展 ) [ "./lec4.tex" -ef./lec4.te* ]; echo $? [[ "./lec4.tex" -ef./lec4.te* ]]; echo $? test "./lec4.tex" -ef./lec4.te*; echo $? Maoying Wu (CBB) BI296-Lec04 Spring, 2018 17 / 48

The Conditional Statements ( 条件语句 ) if..then..elif..then..else..fi if test "$(whoami)" == "root"; then echo "You are using a privileged account" exit 1 fi Equivalent && and test "$(whoami)"!= "root" && (echo "you are using a non-privileged account"; exit 1) test "$(whoami)" == "root" echo "Please verify that you have the super-user privilege." Maoying Wu (CBB) BI296-Lec04 Spring, 2018 18 / 48

case...in...esac construct Syntax Examples case var in val1) statement1 ;;; val2) statement2 ;;; *) statement3 ;;; esac #!/bin/bash echo "Enter your favorite color: " read color case $color in r* R*) echo "Your favorite color is red." ;;; b* B*) echo "Your favorite color is blue." ;;; g* G*) echo "Your favorite color is green." ;;; *) echo "The color you entered is invalid." ;;; esac Maoying Wu (CBB) BI296-Lec04 Spring, 2018 19 / 48

select: creating menu ( 选择菜单 ) #!/bin/bash PS3="Choose your favorite dish (q to exit): " select dish in "Roast Duck" "Mapo Tofu" "Sweet and Sour Ribs" do case $dish in R*) echo "You may be from Beijing." ;; M*) echo "You may be from Sichuan." ;; S*) echo "You may be from Shanghai." ;; q) break ;; *) echo "Let me guess." ;; esac break done Maoying Wu (CBB) BI296-Lec04 Spring, 2018 20 / 48

Next we will talk about... 1 bash: an introduction 2 flow control conditional statements if statement loop statement 3 mathematical computing integer computation float computation 4 command line positional parameters command-line options 5 functions Maoying Wu (CBB) BI296-Lec04 Spring, 2018 21 / 48

while loop Syntax while condition do do_something done Example #!/bin/bash IFS=":" while read f1 f2 f3 f4 f5 f6 f7 do if [ "$f7" == "/sbin/nologin" ]; then echo "$f1 cannot login to the system." fi done < /etc/passwd Maoying Wu (CBB) BI296-Lec04 Spring, 2018 22 / 48

General for loop Syntax for var in val-list do do_something done Examples #!/bin/bash # for1.sh declare -i j for i in seq 20 do j=$i%5 if [ $j -eq 0 ]; then continue fi echo $i done Maoying Wu (CBB) BI296-Lec04 Spring, 2018 23 / 48

C-style for loop Syntax for ((start-condition;end-condition;loop-action)) do do_something done #!/bin/bash declare -i j for ((i=1;i<=20;i++)) do j=$i%5 if [ $j -eq 0 ]; then continue fi echo $i done Examples Maoying Wu (CBB) BI296-Lec04 Spring, 2018 24 / 48

Exercise 1 Guess what the following scripts do. #!/bin/bash # space.sh: A very simple test for checking disk space. space= df -h awk {print $5} grep % grep -v Use sort -n tail -1 cut -d "%" -f1 alertvalue="80" if [ "$space" -ge "$alertvalue" ]; then echo "At least one disk is nearly full!" else echo "Disk space normal" fi Maoying Wu (CBB) BI296-Lec04 Spring, 2018 25 / 48

Next we will talk about... 1 bash: an introduction 2 flow control conditional statements if statement loop statement 3 mathematical computing integer computation float computation 4 command line positional parameters command-line options 5 functions Maoying Wu (CBB) BI296-Lec04 Spring, 2018 26 / 48

Four integer computing ( 整数 ) 1 declare -i 2 (()) 3 expr 4 let # declare declare -i int int=13%5 echo $int # (()) x=3 ((x++)) echo $x # expr expr 3 * 5 # let let "x = x * 3" echo $x Example Maoying Wu (CBB) BI296-Lec04 Spring, 2018 27 / 48

Next we will talk about... 1 bash: an introduction 2 flow control conditional statements if statement loop statement 3 mathematical computing integer computation float computation 4 command line positional parameters command-line options 5 functions Maoying Wu (CBB) BI296-Lec04 Spring, 2018 28 / 48

floating computing ( 浮点数 ) The command bc does help. example echo "3.3*4" bc # 13.2 echo "2.1ˆ4" bc # 19.4 echo "scale=2; (2.1ˆ2)ˆ2" bc # 19.44 echo "scale=1; (2.1ˆ2)ˆ2" bc # 19.3 echo "scale=2; 1.5*100/2" bc # 75.00 # computing with function echo "sqrt(2)" bc echo "sqrt(2)" bc -l echo "a(1)" bc -l echo "scale=3; 4*a(1)" bc Maoying Wu (CBB) BI296-Lec04 Spring, 2018 29 / 48

expr in string operation ( 字符串 ) # return the string length expr length "DNA transcription" # return the substring expr substr "DNA transcription" 5 13 # return the index expr index "DNA transcription" c # regex match expr account.doc : doc expr account.doc : acc # regex match expr 1234bcdf : ".*" # capture by regex expr abcdefgh :...\(...\).. # capture by regex expr account.doc : \(.*\).doc Maoying Wu (CBB) BI296-Lec04 Spring, 2018 30 / 48

Next we will talk about... 1 bash: an introduction 2 flow control conditional statements if statement loop statement 3 mathematical computing integer computation float computation 4 command line positional parameters command-line options 5 functions Maoying Wu (CBB) BI296-Lec04 Spring, 2018 31 / 48

Positional Parameters ( 位置参数 ) $* treats all positional parameters as a whole string. $@ treats all positional parameters as an array. $0 is the script file itself. $1,$2,... is the first, second,... positional parameters. $#: number of positional parameters. #!/bin/bash for i in {1..$#}; do echo ${!i} done Examples Maoying Wu (CBB) BI296-Lec04 Spring, 2018 32 / 48

Exercise 1 Print all the positional arguments, one per line. 2 Compute the product of all the positional arguments. 3 If your arguments contain some float numbers, what to do then? Maoying Wu (CBB) BI296-Lec04 Spring, 2018 33 / 48

Next we will talk about... 1 bash: an introduction 2 flow control conditional statements if statement loop statement 3 mathematical computing integer computation float computation 4 command line positional parameters command-line options 5 functions Maoying Wu (CBB) BI296-Lec04 Spring, 2018 34 / 48

options ( 选项 )./test.sh -a -b -c short options without argument ( 无参数短选项 )../test.sh -abc short options as above ( 同上 )../test.sh -a arg -b -c short options, where -a arg needs argument, while -b and -c do not need argument ( 有 / 无参数短选项 )../test.sh --a-long=arg --b-long long options where --a-long=arg requires argument, while --b-long does not require argument ( 有 / 无参数长选项 ). Maoying Wu (CBB) BI296-Lec04 Spring, 2018 35 / 48

getopts getopts does not support long options. Example #!/bin/bash # testoptions.sh #./testoptions.sh -a something -bc other-stuff while getopts "a:bc" arg # colon indicates a requires argument do case $arg in a) echo "a s arg: $OPTARG" ;; b) echo "b" ;; c) echo "c" ;;?) echo "unknown argument"; exit 1 ;; esac done echo "The argument is at $OPTIND" Maoying Wu (CBB) BI296-Lec04 Spring, 2018 36 / 48

getopt: supports long options #!/bin/bash # testlongopts.sh #./testlongopts.sh -a arg1 arg2 --c-long wow!*\? -cmore -b " very long " TEMP= getopt -o ab:c:: --long a-long,b-long:,c-long:: \ -n testlongopts.sh -- "$@" if [ $?!= 0 ]; then echo "Terminating..." >&2 ; exit 1 ; fi # set will reorder the parameter eval set -- "$TEMP" while true ; do case "$1" in -a --a-long) echo "Option a"; shift ;; -b --b-long) echo "Option b, argument \ $2 " ; shift 2 ;; -c --c-long) case "$2" in "") echo "Option c, no argument"; shift 2 ;; *) echo "Option c, argument \ $2 " ; shift 2 ;; esac ;; --) shift ; break ;; *) echo "Internal error!" ; exit 1 ;; esac done echo "Remaining arguments: " for arg do echo --> "\ $arg " ; done Maoying Wu (CBB) BI296-Lec04 Spring, 2018 37 / 48

Exercise 1 As a simple exercise, write a shell script that prints the file type for each file passed as an argument to the script. Here is an example: $./ftypes.sh ftypes.sh.emacs.bashrc public_html ftypes.sh: Bourne-Again shell script text executable.emacs: Lisp/Scheme program text.bashrc: ASCII English text, with very long lines public_html: symbolic link to www Maoying Wu (CBB) BI296-Lec04 Spring, 2018 38 / 48

Functions: Definition Function defintion ( 函数定义 ) # function func_name() { commands; } function dir() { target=${1:-.} if [ -e $target ]; then echo "List Directories in $target: "; ls -l $target awk /ˆd/{print $NF} ; else echo "$target not exists." return -1 fi } Function call ( 函数调用 ) #!/bin/bash read -p "Input the directory name: " dirname dir $dirname Maoying Wu (CBB) BI296-Lec04 Spring, 2018 39 / 48

Variable Scopes ( 变量作用域 ) Local Variables ( 局部变量 ): inside a function Global Variables ( 全局变量 ): declare -g name=val Environmental Variables ( 环境变量 ): export name=val Examples ( 示例 ) #!/bin/bash function1() { local func1var=20 echo "Within function1, \$func1var = $func1var." function2 } function2 () { echo "Within function2, \$func1var = $func1var." } function1 echo "Outside function, \$func1var = $func1var." exit 0 Maoying Wu (CBB) BI296-Lec04 Spring, 2018 40 / 48

A little project: prime finder (1) #!/bin/bash # SCRIPT: primefactors.sh # USAGE: primefactors.sh <Positive Integer> # PURPOSE: Produces prime factors of a given number #################################################### # Argument checking #################################################### if [ $# -ne 1 ]; then echo "Usage: $0 <Positive Integer>" exit 1 fi expr $1 + 1 &>/dev/null if [ $? -ne 0 ]; then echo "Sorry, you supplied a non-numerical value." exit 1 fi [ $1 -lt 2 ] && echo "Value < 2 are not prime numbers" && exit 1 num=$1 Maoying Wu (CBB) BI296-Lec04 Spring, 2018 41 / 48

A little project: prime finder (2) ######################################## # Functions ######################################## # script to find prime number primenumber() { primenum=$1 for ((cnt2=2;$((cnt2*cnt2))<=$primenum; cnt2++)); do [ $((primenum%cnt2)) -eq 0 ] && return 1 done return 0 } primefind() { primenumber $1 && echo "$1" && exit 0 for ((cnt1=$2;cnt1<=$1;$cnt1++)); do primenumber $cnt1 && factorcheck $1 $cnt1 && break done } Maoying Wu (CBB) BI296-Lec04 Spring, 2018 42 / 48

A little project: prime finder (3) factorcheck() { prime=$2 newnum=$1 remainder=$((newnum%prime)) if [ $remainder -eq 0 ]; then printf "%dx" $prime newnum=$((newnum/prime)) primefind $newnum 2 return else let prime++ primefind $newnum $prime fi } Maoying Wu (CBB) BI296-Lec04 Spring, 2018 43 / 48

A little project: prime finder (4) ############################################### # main ############################################### echo -n "Prime Factor of $1: " primefind $num 2 print "\b\n" Maoying Wu (CBB) BI296-Lec04 Spring, 2018 44 / 48

Exercise Explain why the final output is blank. #!/bin/bash OUTPUT="name1 ip ip status" # normally output of another command with multi line output if [ -z "$OUTPUT" ] then echo "Status WARN: No messages from Hacker" exit $STATE_WARNING else echo "$OUTPUT" while read NAME IP1 IP2 STATUS do if [ "$STATUS"!= "Optimal" ] then echo "CRIT: $NAME - $STATUS" echo $((++XCODE)) else echo "OK: $NAME - $STATUS" fi done fi echo $XCODE Maoying Wu (CBB) BI296-Lec04 Spring, 2018 45 / 48

Indexed Arrays ( 索引数组 ) declare -a arrayname Only 1-dim array is allowed Array Assignment 1 array=(val1 val2 val3... valn) 2 array=([0]=val0 [1]=val1... [N]=valN) 3 array[0]=val0; array[1]=val1;... Size: ${#array[@]}, ${#array[*]} echo ${array[2]} echo ${array[@]:3:2} Maoying Wu (CBB) BI296-Lec04 Spring, 2018 46 / 48

Associative Arrays ( 关联数组 ) declare -A Arrayname Array Definition 1 declare -A Array=([unix]=1 [windows]=2 [mac]=3) 2 declare -A Array[unix]=1; Array[windows]=2; Array [mac]=3 Iterate over all keys and values: for key in "${!Array[@]}"; do echo $key, ${Array[$key]}; done Maoying Wu (CBB) BI296-Lec04 Spring, 2018 47 / 48

Summary Run script file: shebang (#!/bin/bash) Variable declaration (declare), assignment (=) and destroy (unset) Conditional statement ([ ], test, [[ ]]) if...then...fi statement for...do...done statement case...in...esac statement while...do...done statement Function definition (func(){...}) Positional parameters ($0, $1,$*, $@) getopts statement Maoying Wu (CBB) BI296-Lec04 Spring, 2018 48 / 48