Shell Programming (Part 2)

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

Computer Systems and Architecture

COMP 4/6262: Programming UNIX

Lab 4: Shell scripting

Bourne Shell Reference

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

bash Execution Control COMP2101 Winter 2019

Shell programming. Introduction to Operating Systems

Useful Unix Commands Cheat Sheet

A shell can be used in one of two ways:

Linux shell programming for Raspberry Pi Users - 2

Shell Programming (bash)

Computer Systems and Architecture

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

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

CSC 2500: Unix Lab Fall 2016

Bash scripting basics

Basic Linux (Bash) Commands

CS Unix Tools & Scripting

Exercise 1: Basic Tools

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Title:[ Variables Comparison Operators If Else Statements ]

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

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

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

Assignment clarifications

DAVE LIDDAMENT INTRODUCTION TO BASH

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Exercise sheet 1 To be corrected in tutorials in the week from 23/10/2017 to 27/10/2017

Conditional Control Structures. Dr.T.Logeswari

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.

Shells and Shell Programming

Shells and Shell Programming

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

BASH Programming Introduction

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

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

UNIX System Programming Lecture 3: BASH Programming

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

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

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

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

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

Multiple Choice - 42 Questions - 10 of 20%

Introduction to Supercomputing

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

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

Lecture 02 The Shell and Shell Scripting

Mills HPC Tutorial Series. Linux Basics II

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

Bash Shell Programming Helps

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

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

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

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

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

Processes and Shells

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

Shell Start-up and Configuration Files

CMPS 12A Introduction to Programming Lab Assignment 7

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

Linux Command Line Interface. December 27, 2017

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

Bash shell programming Part II Control statements

CSE374 Winter 2016, Midterm Examination February 8, 2016 Please do not turn the page until the bell rings.

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

SHELL SCRIPT BASIC. UNIX Programming 2014 Fall by Euiseong Seo

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

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

Welcome to the Bash Workshop!

LOG ON TO LINUX AND LOG OFF

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

Manual Shell Script Linux If Not Equal String Comparison

Essentials for Scientific Computing: Bash Shell Scripting Day 3

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

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

CSC374, Spring 2010 Lab Assignment 1: Writing Your Own Unix Shell

SHELL SCRIPT BASIC. UNIX Programming 2015 Fall by Euiseong Seo

Last Time. on the website

Scripting Languages Perl Basics. Course: Hebrew University

COMS 6100 Class Notes 3

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Week 2 Lecture 3. Unix

CS 3410 Intro to Unix, shell commands, etc... (slides from Hussam Abu-Libdeh and David Slater)

Chapter 4. Unix Tutorial. Unix Shell

Welcome to the Bash Workshop!

Shells & Shell Programming (Part B)

CMPT 300. Operating Systems. Brief Intro to UNIX and C

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

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?

Introduc)on to Unix and Perl programming

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

Introduction to the shell Part II

Linux 系统介绍 (III) 袁华

Pipes. Pipes Implement a FIFO. Pipes (cont d) SWE 545. Pipes. A FIFO (First In, First Out) buffer is like a. Pipes are uni-directional

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

Transcription:

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 (Part 2) Devin J. Pohly <djpohly@cse.psu.edu> Page 1

Some references Advanced Bash-Scripting Guide http://tldp.org/ldp/abs/html/ Actually a great reference from beginner to advanced commandlinefu.com Lots of gems, somewhat more advanced Fun to figure out how they work Bash man page man bash Very complete, once you're used to reading man pages Page 2

Code for today $ $ $ $ wget tiny.cc/311shell2 tar -xvzf 311shell2 cd shell2 make Page 3

How to kill a process Today we re learning some loops If it starts to run away, Ctrl-C is your friend Sends a signal that ends the process More on signals later... Works on many different programs, as long as they were started from the command line Displayed as ^C Page 4

Return from main In C, the main function always returns an int Used as an error code for the entire process Same convention as any other function Zero: success Nonzero: failure, error, killed by a signal, etc. Called the exit status of the process Page 5

Exit status in scripts $?: get exit status of the previous command The exit status of a script comes from the last command it runs Or use the exit builtin to exit early, e.g. exit 1! cmd reverses the value: 0 for failure and 1 for success Works just like the logical not (!) operator in C Page 6

Status sample program $./status 0 $ echo $? $./status 2 $ echo $? $!./status 2 $ echo $? #include <stdlib.h> int main(int argc, char **argv) { // Quick-and-dirty int conversion return atoi(argv[1]); } $./status -1 $ echo $? Page 7

Custom prompt for today You can include $? in your prompt I personally like this it lets me know for sure when something fails For today, let s do this: source newprompt Now try:./status 42 Page 8

Test commands Builtin commands that test handy conditions true: always succeeds false: always fails Many other conditions: test builtin Returns 0 if test is true, 1 otherwise Full list: help test Page 9

What do these do? $ test -e status.c $ test -e asdf $ test -d status.c $ test -d /etc $ $ $ $ test test test test 10 10 10 12 -gt -lt -le -ge 5 10 10 15 Page 10

Useful tests test -e file True if file exists test -d dir True if dir exists and is a directory test -z "$var" True if var is empty (zero-length) test -n "$var" True if var is nonempty test str1 = str2 test num1 -gt num2 or -lt, -ge, -le, -eq, -ne Page 11

Command lists Simple command list: ; Runs each command regardless of exit status Example: do_this; do_that Shortcutting command lists && stops after failure stops after success Examples: foo && echo success bar echo failed Page 12

Try it out true && echo one true echo two false && echo three false echo four test -e Makefile && make cat dog echo bird./status 4 && echo 4./status 0 && echo 0 cat dog; cat status.c touch status.c; make make clean && make Page 13

Shorthand tests Shorthand test: [[ ]] Workalike for test For example: age=20 test $age -ge 16 && echo can drive [[ $age -ge 16 ]] && echo can drive Now say age=3 and try again Page 14

Conditionals Exit status is used as the test for if statements: if list; then cmds fi Runs list, and if the exit status is 0 (success), then cmds is executed There are also elif and else commands that work the same way. Page 15

Conditional loops You can write a while loop using the same idea: while list; do cmds done Runs list, cmds, list, cmds, list... for as long as list succeeds (exit status 0) Similarly, the until loop will execute as long as list fails Page 16

Conditional practice if! [[ -e foo ]]; then echo hello > foo fi while [[ "$x" -lt 99999 ]]; do echo "$x" x="1$x" done if cat foo; then echo Same to you fi if cat dog; then echo Woof fi Page 17

For statement The for loop is foreach style: for var in words; do cmds done The cmds are executed once for each argument in words, with var set to that argument Page 18

For example... (get it??) for a in A B C hello 4; do echo "$a$a$a" done for ext in h c; do cat "hello.$ext" done Page 19

Globbing Funny name for wildcards (Comes from global command ) * means any number of characters: $ echo * $ echo *.c? means any one character: $ echo hello.? Bulk rename: for f in hello.*; do mv "$f" "$f.bak" done Page 20

Some more useful tools touch foo: modify the file foo without really changing it sleep t: wait for t seconds fgrep string: filter stdin to just lines containing string find. -name '*.c': list all the.c files under the current directory Many other things you can search for; see man find file foo: determine what kind of file foo is wc: counts words/characters/lines from stdin (-w/-c/-l = separately) bc: command line calculator Page 21

Exercises Print out foo once per second until ^C d Find all the.png files in dir/ Find all the files which are actually PNG graphics in dir/ Use a pipe and bc to calculate the product of 199 and 42 Page 22