Fall 2006 Shell programming, part 3. touch

Similar documents
More tr examples. translate ampersands to hash. squeeze consecutive tabs to one tab. Unix Tools: Shells, part 4

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

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

5/20/2007. Touring Essential Programs

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

A shell can be used in one of two ways:

Basic Linux (Bash) Commands

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

Shells and Shell Programming

Files and Directories

Shells & Shell Programming (Part B)

UNIX II:grep, awk, sed. October 30, 2017

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

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

bash Tests and Looping Administrative Shell Scripting COMP2101 Fall 2017

CS1101: Lecture 9 The Shell as a Programming Language

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

Shells and Shell Programming

Bash scripting basics

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

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Vi & Shell Scripting

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

Essentials for Scientific Computing: Bash Shell Scripting Day 3

Advanced training. Linux components Command shell. LiLux a.s.b.l.

Mastering Modern Linux by Paul S. Wang Appendix: Pattern Processing with awk

Lecture 3 Tonight we dine in shell. Hands-On Unix System Administration DeCal

5/8/2012. Exploring Utilities Chapter 5

9.2 Linux Essentials Exam Objectives

Table of Contents. Evaluation Copy

CS 307: UNIX PROGRAMMING ENVIRONMENT FIND COMMAND

Conditional Control Structures. Dr.T.Logeswari

QUESTION BANK ON UNIX & SHELL PROGRAMMING-502 (CORE PAPER-2)

CSCI 2132 Software Development. Lecture 4: Files and Directories

Shell Control Structures

Computer Systems and Architecture

Linux Systems Administration Getting Started with Linux

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

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

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?

Answers to AWK problems. Shell-Programming. Future: Using loops to automate tasks. Download and Install: Python (Windows only.) R

More on Unix Shell SEEM

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

Chapter 1 - Introduction. September 8, 2016

UNIX shell scripting

Freescale FAE75 Training Genesi Pegasos II LinuxTraining. Maurie Ommerman June 10, 2004

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

Lecture #13 AWK Part I (Chapter 6)

18-Sep CSCI 2132 Software Development Lecture 6: Links and Inodes. Faculty of Computer Science, Dalhousie University. Lecture 6 p.

UNIX System Programming Lecture 3: BASH Programming

Subcontractors. bc math help for the shell. interactive or programatic can accept its commands from stdin can accept an entire bc program s worth

Wildcards and Regular Expressions

UNIX Shell Programming

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

Awk & Regular Expressions

CS Unix Tools & Scripting

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

List of Linux Commands in an IPm

Shell Control Structures

The UNIX Shells. Computer Center, CS, NCTU. How shell works. Unix shells. Fetch command Analyze Execute

Introduction to Systems Security

Shell Script Programming 2

Bourne Shell Programming Topics Covered

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

bash Scripting Introduction COMP2101 Winter 2019

Last Time. on the website

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

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

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

Useful Unix Commands Cheat Sheet

COMP 4/6262: Programming UNIX

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

1. What statistic did the wc -l command show? (do man wc to get the answer) A. The number of bytes B. The number of lines C. The number of words

CMPS 12A Introduction to Programming Lab Assignment 7

Multiple Choice - 42 Questions - 10 of 20%

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

44 - Text formating with awk Syntax: awk -FFieldSeperator /SearchPatern/ {command} File z.b. awk '/ftp/ {print $0}' /etc/services

Lab #2 Physics 91SI Spring 2013

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

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

Grep and Shell Programming

Computer Systems and Architecture

A Brief Introduction to the Linux Shell for Data Science

Unleashing the Shell Hands-On UNIX System Administration DeCal Week 6 28 February 2011

Manual Shell Script Linux If Not Equal String Comparison

UNIX Essentials Featuring Solaris 10 Op System

Chapter 4. Flow of Control

Files

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

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

Shell Programming (bash)

Provides ability to perform UNIX commands at some time in the future. At time time on day day, the commands in filefile will be executed.

EECS2301. Lab 1 Winter 2016

Bash command shell language interpreter

Chapter 4. Unix Tutorial. Unix Shell

CSC UNIX System, Spring 2015

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

More Scripting Techniques Scripting Process Example Script

Week Overview. Simple filter commands: head, tail, cut, sort, tr, wc grep utility stdin, stdout, stderr Redirection and piping /dev/null file

Shell Programming Overview

Transcription:

touch Touch is usually a program, but it can be a shell built-in such as with busybox. The touch program by default changes the access and modification times for the files listed as arguments. If the file does not exist, it is created as a zero length file (unless you use the -c option.) You can also set either or both of the times to arbitrary values, such as with the -t, -d, -B, and -F options.

Backquotes and textual substitution If you surround a command with backquotes, the standard output of the command is substituted for the quoted material. For instance, [langley@sophie 2006-Fall]$ echo ls 0*tex 01-introduction.tex 02-processes.tex 03-shells1.tex 03-shells2.tex 04-shells3.tex [langley@sophie 2006-Fall]$ echo egrep -l Langley * 03-shells2.tex Syllabus-Fall.html Syllabus-Fall.html.1 Syllabus Summer.html [langley@sophie 2006-Fall]$ now= date [langley@sophie 2006-Fall]$ echo $now Mon Sep 11 09:55:09 EDT 2006

Backquotes and textual substitution if [ wc -l < /etc/hosts -lt 10 ]; then echo "lt"; fi # use < to prevent filename from

xargs xargs COMMAND -n N [INITIAL-ARGUMENTS] xargs reads from stdin to obtain arguments for the COMMAND. You may specify initial arguments with the COMMAND. If you specify -n N, then only up to N arguments are given to any invocation of COMMAND. For instance, [langley@sophie 2006-Fall]$ cat /etc/hosts xargs -n 1 ping -c 1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.075 ms

--- 127.0.0.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.075/0.075/0.075/0.000 ms, pipe 2 PING localhost.localdomain (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=64 time=0.060 ms --- localhost.localdomain ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.060/0.060/0.060/0.000 ms, pipe 2 PING localhost.localdomain (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=64 time=0.071 ms

The for statement for name in LIST0 ; do LIST1 ; done for name ; do LIST1 ; done # useful for scripts for (( EXPR1 ; EXPR2 ; EXPR3 )) ; do LIST1 ; done In the last form, EXPR? are evaluated as arithmetic expressions.

The for statement [langley@sophie 2006-Fall]$ for (( ip = 0 ; ip < 5 ; ip = ip+1)) do echo $ip ; done 0 1 2 3 4

The for statement for i in cat /etc/hosts do ping -c 1 $i done

break and continue statements break terminates the current loop immediately and goes on to the next statement after the loop. continue starts the next iteration of a loop.

break and continue statements For example, for name in * do if [ -f $name ] then echo skipping $name continue else echo process $name fi done

expr You can use expr to evaluate arithmetic statements, some regular expression matching, and some string manipulation. (You can also use either bc or dc for more complex arithmetic expressions.)

expr files=10 dirs= expr $files + 5 limit=15 if [ expr $files + $dirs < $limit ] then echo okay else echo too many! fi

awk One of the more powerful programs found on Unix machines is awk, and its updated versions, nawk and gawk. It is most useful for handling text information that is separated into a series of uniform records. The most common one that it handles is records of one line, divided by either column numbers or by a field separator. For instance, handling the password file is a snap with awk.

awk The password file on a Unix machine looks something like: root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

awk You can quickly get a list of usernames into a single string variable with: [langley@sophie 2006-Fall]$ usernames= awk -F: {print $1} /etc/passwd [langley@sophie 2006-Fall]$ echo $usernames root bin daemon adm lp sync shutdown halt mail [langley@sophie 2006-Fall]$ usernames= awk {print $1} FS=: /etc/passwd [langley@sophie 2006-Fall]$ echo $usernames root bin daemon adm lp sync shutdown halt mail

awk Fundamentally, awk scripts consist of a series of pairs: PATTERN { ACTION }

awk where the PATTERN can be a /regular expression/ relational expression pattern-matching expression BEGIN or END

awk By default, the record separator is a newline so awk works on a line-by-line basis by default. If no PATTERN is specified, then the ACTION is always taken for each record. If no ACTION specified, then the each records that matches a pattern is written to stdout.

awk You can specify that an ACTION can take place before any records are read with the keyword BEGIN for the PATTERN. You can specify that an ACTION can take place after all records are read with the keyword END for the PATTERN. With PATTERNs, you can also negate (with!) them, logically and two PATTERNs (with &&), and logically or two PATTERNs (with ).

awk Some examples of regular expressions in awk: [langley@sophie 2006-Fall]$ awk /[Ll]angley/ {print $0} /etc/passwd langley:x:500:500:randolph Langley:/home/langley:/bin/bash [langley@sophie 2006-Fall]$ awk /^#/ /etc/hosts # Do not remove the following line, or various programs # that require network functionality will fail.

awk $0 refers to the whole record, $N refers to the Nth field in a record NF refers to the number of fields in a record (example, awk -F: END print NF /etc/passwd tells you that there are seven fields used in the password file.) NR refers to which record (by default, line) you are currently at.

awk Some examples of relational expressions: $1 == lane # does the first field equal the string lane? $1 == $7 # are fields one and seven equal? NR > 1000 # have we processed more than 1000 records? NF > 10 # does this record have more than 10 fields? NF > 5 && $1 = me # compound test /if/&&/up/ # does the record contain both strings if and up?

awk You can also check a given field against a regular expression: $1 ~ /D[Rr]\./ # does the first field contain a Dr. or DR.? $1!~ /#/ # does the first field have a # in it?

awk ACTIONs are specified with { }. You can use semicolons to separate statements with the braces (although newlines work also). Popular statements are print, if {} else {}, and system. awk is very powerful! Henry Spencer wrote an assembler in awk.

awk example scripts { print $1, $2 } # print the first two fields of each record $0!~ /^$/ # print all non-empty lines $2 > 0 && $2 < 10 { print $2 } # print field 2 if it is 0 < $2 < 10 BEGIN {FS= : sum = 0} # sum field 3 and print the sum {sum += $3} END {print sum}

The tr utility Allows you to delete, replace, or squeeze characters from standard input. The -d option deletes the characters specified in the first argument; -s squeeze removes all repetitions of characters in the first argument with a single instance of the character. The normal mode is to substitute characters from the first argument with characters from the second argument.

The tr utility [langley@sophie 2006-Fall]$ cat /etc/hosts # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 localhost.localdomain localhost 128.186.120.8 sophie.cs.fsu.edu 127.0.0.1 a.as-us.falkag.net 127.0.0.1 clk.atdmt.com [langley@sophie 2006-Fall]$ cat /etc/hosts tr a-z A-Z # DO NOT REMOVE THE FOLLOWING LINE, OR VARIOUS PROGRAMS # THAT REQUIRE NETWORK FUNCTIONALITY WILL FAIL. 127.0.0.1 LOCALHOST.LOCALDOMAIN LOCALHOST 128.186.120.8 SOPHIE.CS.FSU.EDU 127.0.0.1 A.AS-US.FALKAG.NET 127.0.0.1 CLK.ATDMT.COM