Shell scripting From single commands to a shell script

Slides:



Advertisements
Similar presentations
More Shell Programming Software Tools. Slide 2 Keyword Shell Variables l The shell sets keyword shell variables. You can use (and change) them. HOME The.
Advertisements

Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
Basics of Shell Programming. What’s Shell? It’s acts an interface between the user and OS (kernel).It’s known as “ command interpreter”. When you type.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Integer variables #!/bin/csh # sum of numbers from $argv[1] to $argv[2] set low = $argv[1] set high = $argv[2] set sum = 0 set current = $low while ( $current.
23-Jun-15Advanced Programming Spring 2002 bash Henning Schulzrinne Department of Computer Science Columbia University.
CS 497C – Introduction to UNIX Lecture 34: - Shell Programming Chin-Chih Chang
CIS 240 Introduction to UNIX Instructor: Sue Sampson.
Bash Shell Scripting 10 Second Guide Common environment variables PATH - Sets the search path for any executable command. Similar to the PATH variable.
Shell Script Examples.
Shell Control Structures CSE 2031 Fall August 2015.
1 Operating Systems Lecture 3 Shell Scripts. 2 Shell Programming 1.Shell scripts must be marked as executable: chmod a+x myScript 2. Use # to start a.
1 Operating Systems Lecture 3 Shell Scripts. 2 Brief review of unix1.txt n Glob Construct (metacharacters) and other special characters F ?, *, [] F Ex.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 8 Shell.
Linux+ Guide to Linux Certification, Third Edition
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
Keyword Shell Variables The shell sets keyword shell variables. You can use (and change) them. HOME The path to your home directory PATH Directories where.
CMPSC 60: Week 6 Discussion Originally Created By: Jason Wither Updated and Modified By: Ryan Dixon University of California Santa Barbara.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
BASH Scripting A beginners guide to Bourne Again SHell scripting.
CSC 352– Unix Programming, Spring 2015 March 2015 Shell Programming (Highlights only)
This slide deck is for LPI Academy instructors to use for lectures for LPI Academy courses. ©Copyright Network Development Group Module 9 Basic Scripting.
Shell Programming. Creating Shell Scripts: Some Basic Principles A script name is arbitrary. Choose names that make it easy to quickly identify file function.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
Shell Control Statements and More
Agenda Positional Parameters / Continued... Command Substitution Bourne Shell / Bash Shell / Korn Shell Mathematical Expressions Bourne Shell / Bash Shell.
1 Unix/Linux commands and shell programming-Part 2 (Dr. Mohamed El Bachir Menai)
LING 408/508: Programming for Linguists Lecture 5 September 9 th.
UNIX Shell Script (2) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Introduction to Bash Shell. What is Shell? The shell is a command interpreter. It is the layer between the operating system kernel and the user.
If condition1 then statements elif condition2 more statements […] else even more statements fi.
Group, group, group One after the other: cmd1 ; cmd2 One or both: cmd1 && cmd2 Only one of them: cmd1 || cmd2 Cuddling (there):( cmd1 ; cmd2 ) Cuddling.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
Shell Scripting What is Shell Programming? Putting UNIX commands in a file Seldom used where speed is important Often used to manipulate files To create.
CSc 352 An Introduction to make Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Various 3. Noclobber 1 echo hello > output.txt But what if output is an inportant file and you do not want to make a mistake!!! set –o noclobber Now you.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 UNIX Operating Systems II Part 2: Shell Scripting Instructor: Stan Isaacs.
Debugging. Debug Syntax checking Shell tracing I-Enabling Debugging #!/bin/sh option.
Shell Control Structures CSE 2031 Fall June 2016.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Computer Programming Batch & Shell Programming 9 th Lecture 김현철 컴퓨터공학부 서울대학교 2009 년 가을학기.
AWK One tool to create them all AWK Marcel Nijenhof Eth-0 11 Augustus 2010.
Rexx for Shell Scripting or “We don't need no stinkin' bashes...”
Shell Control Structures
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
CSC 352– Unix Programming, Fall 2012
Shell Scripting March 1st, 2004 Class Meeting 7.
CIRC Summer School 2017 Baowei Liu
Lecture 9 Shell Programming – Command substitution
Writing Shell Scripts ─ part 3
Writing Shell Scripts ─ part 3
What is Bash Shell Scripting?
LING 408/508: Computational Techniques for Linguists
CHAPTER 8 (skip , ) CHAPTER 10
LING 408/508: Computational Techniques for Linguists
Agenda Functions: What are functions? / Purpose of Functions
UNIX Reference Sheets CSE 2031 Fall 2010.
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Shell Control Structures
CSC 352– Unix Programming, Fall, 2011
Shell Control Structures
Basic shell scripting CS 2204 Class meeting 7
Presentation transcript:

Shell scripting From single commands to a shell script Eth-0 zomer 2010 Wieringerwerf 10 Augustus Marcel Nijenhof marcel.nijenhof@nllgg.nl http://www.nllgg.nl

Shell Scripting?

Marcel Nijenhof Nllgg Chairman 10 Year Board member LPI Nederland Proctor Proxy Employee Unix administrator

The script

Single commands $ mv file1 file1_20100416 $ mv file2 file2_20100416

The first script #!/bin/sh mv file1 file1_20100416

#!/bin/sh for f in file1 file2 file3 do mv "${f}" "${f}_20100416" done Why no loop? #!/bin/sh for f in file1 file2 file3 do mv "${f}" "${f}_20100416" done

#!/bin/sh for f in file* do mv "${f}" "${f}_20100416" done Using a wild card! #!/bin/sh for f in file* do mv "${f}" "${f}_20100416" done

ren file* #!/bin/sh for f in "$@" do mv "${f}" "${f}_20100416" done Why hard code the names? ren file* #!/bin/sh for f in "$@" do mv "${f}" "${f}_20100416" done

Dynamical extensions ren _20100808 file* #!/bin/sh APPEND=$1 shift for f in "$@" do mv "${f}" "${f}${APPEND}" done

But i want capitals #!/bin/sh for f in "$@" do mv "${f}" "${f^^}" Done Note: "${f^^}" is a bash version 4 option. for bash 3 and posix use: $(echo "${f}" | tr "[a-z]" "[A-Z]")

How to combine #!/bin/sh if [ "$1" = -a ] then APPEND=$2 # ren -a _20100808 file* shift 2 else Shift # ren * fi for f in "$@" do if [ "${APPEND}" ] mv "${f}" "${f}${APPEND}" mv "${f}" "${f^^}" done

Using getopts ... while [ "$ARG" != "?" ] do getopts :a:u case "$ARG" in a) APPEND=${OPTARG} ;; u) APPEND="" ?) shift $((OPTIND-1)) break esac done

All options of the final script ren: [-X] [-H] [-V] [-n] [-i] [-lu] [-p <PRE>] [-a <APPEND>] [-s <SEARCH>] [-r <REPLACE>] <file list> -X: Debug -H: Help -V: Version -n: Don't rename files only show how files are renamed -i: Interactive to prevent overwriting -l: Change names to lower case -u: Change names to upper case -p <PREPEND>: Prepand <PREPEND> to the file names -a <APPEND>: Append <APPEND> to the file names -s <SEARCH>: Search the string in the filename -r <REPLACE>: Replace the search part in the filename <file list>: List of files to rename Select only one of the options: -l, -u, -p -a, -s Example ren -s DATE -r 20100416 *.DATE

General structure Initialisation Getopts Check of the options Rename loop Check if file exist Generate the new name Check name change Rename

Check options if [ -z "${CMD}" ] then echo "${CMD_ERROR}" >&2 exit 1 fi if [ "${REPLACE}" -a "${CMD}" != SEARCH ] echo "ERROR: Replace is only valid with search" >&2 if [ "$#" -le 0 ] echo "ERROR: No file list" >&2

New file name case "${CMD}" in ... ;; LOWER) newfile="${file,,}" SEARCH) newfile="${file/${SEARCH}/${REPLACE}}" *) echo "ERROR: No such command: ${CMD}" >&2 exit 2 esac Note: "${file/${SEARCH}/${REPLACE}}" is een bash optie. Voor posix: $(echo "${file}" | sed "s/${SEARCH}/${REPLACE}/")

Main loop for file in "$@" do if [ -f "${file}" ] then Nieuwe filenaam [ "${file}" != "${newfile}" ]\ && ${SHOW} mv ${MV_ARGS} \ "${file}" "${newfile}" else echo "ERROR: File not found: ${file}" >&2 fi done

Het volledige script

Questions

Presentation Presentation: http://pion.xs4all.nl/lezingen/ ShellScripting_eth-0_2010.odp ShellScripting_eth-0_2010.pdf ren Ren-posix Copyright: CC Some rights reserved The following items are not covered by cc Tux Nllgg logo Lpi logo Proxy logo