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
● Board member ● Proctor ● Proxy
● Employee ● Unix administrator The script Single commands
$ mv file1 file1_20100416 $ mv file2 file2_20100416 $ mv file3 file3_20100416 The first script
#!/bin/sh mv file1 file1_20100416 mv file2 file2_20100416 mv file2 file3_20100416 Why no loop?
#!/bin/sh for f in file1 file2 file3 do mv "${f}" "${f}_20100416" done Using a wild card!
#!/bin/sh for f in file* 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}" ] then mv "${f}" "${f}${APPEND}" else mv "${f}" "${f^^}" fi 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
] [-a] [-s ] [-r ] -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
: Prepand to the file names -a : Append to the file names -s : Search the string in the filename -r : Replace the search part in the filename : 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 ] 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 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