• No results found

Shell scripting From single

N/A
N/A
Protected

Academic year: 2021

Share "Shell scripting From single"

Copied!
21
0
0

Bezig met laden.... (Bekijk nu de volledige tekst)

Hele tekst

(1)

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

(2)

Shell Scripting?

(3)

Marcel Nijenhof

Nllgg

Chairman

10 Year Board member

LPI Nederland

Board member

Proctor

Proxy

Employee

Unix administrator

(4)

The script

(5)

Single commands

$ mv file1 file1_20100416

$ mv file2 file2_20100416

$ mv file3 file3_20100416

(6)

The first script

#!/bin/sh

mv file1 file1_20100416

mv file2 file2_20100416

mv file2 file3_20100416

(7)

Why no loop?

#!/bin/sh

for f in file1 file2 file3 do

mv "${f}" "${f}_20100416"

done

(8)

Using a wild card!

#!/bin/sh

for f in file*

do

mv "${f}" "${f}_20100416"

done

(9)

Why hard code the names?

ren file*

#!/bin/sh

for f in "$@"

do

mv "${f}" "${f}_20100416"

done

(10)

Dynamical extensions

ren _20100808 file*

#!/bin/sh APPEND=$1 shift

for f in "$@"

do

mv "${f}" "${f}${APPEND}"

done

(11)

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]")

(12)

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}" ] then

mv "${f}" "${f}${APPEND}"

else

mv "${f}" "${f^^}"

fi done

(13)

Using getopts

...

while [ "$ARG" != "?" ] do

getopts :a:u case "$ARG" in

a)

APPEND=${OPTARG}

;;

u)

APPEND=""

;;

?) shift $((OPTIND-1)) break

;;

esac done ...

(14)

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

(15)

General structure

Initialisation

Getopts

Check of the options

Rename loop

Check if file exist

Generate the new name

Check name change

Rename

(16)

Check options

if [ -z "${CMD}" ] then

echo "${CMD_ERROR}" >&2 exit 1

fi

if [ "${REPLACE}" -a "${CMD}" != SEARCH ] then

echo "ERROR: Replace is only valid with search" >&2 exit 1

fi

if [ "$#" -le 0 ] then

echo "ERROR: No file list" >&2 exit 1

fi

(17)

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}/")

(18)

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

(19)

Het volledige script

(20)

Questions

(21)

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

Referenties

GERELATEERDE DOCUMENTEN

We report on the fabrication and measurements of platinum-self-aligned nanogap devices containing cubed iron (core)/iron oxide (shell) nanoparticles (NPs) with two average

Wij willen verder opmerken dat de groothandelsmarkt voor gas een internationale markt is, waarvan de ontwikkelingen op zijn minst regionaal (in Europees verband) en in toenemende

Wetenschappers die recent een collectie van oude schelpen bestudeerden, waren verrast omdat ze niet enkel geen bewijs voor evolutie in de specimens vonden, maar wel duidelijk

• De prijs per eenheid UMTS zal in Nederland lager worden dan in andere landen, omdat de lage opbrengst van de veiling leidt tot (relatief) lage constante kosten voor de

The output of your code is saved into the file provided as the second optional argument of \iexec (the default value is iexec.tmp ):. 6 Today is \iexec[date.txt]{date +\%e-\%b-\%Y |

These functions can only access the shell if a document is proceeded using –shell-escape, which allows unrestricted access to command line functions.. In general, arbitrary

Van losse commando's naar een shell script. Ubuntu release party

Since the emission intensity of upconversion materials is highly dependent on the the excitation power and power regime, the comparison of the brightness of upconversion