Shell scripting
Van losse commando's naar een shell script
Ubuntu release party 2010 Ede
29 Mei
Marcel Nijenhof
marcel.nijenhof@nllgg.nl http://www.nllgg.nl
Shell Scripting?
Marcel Nijenhof
●
Nllgg
●
Voorzitter
●
10 Jaar bestuur
●
LPI Nederland
●
Bestuurslid
●
Examinator
●
Proxy
●
Medewerker
●
Unix beheer
Het script
Losse commando's
$ mv file1 file1_20100416
$ mv file2 file2_20100416
$ mv file3 file3_20100416
Het eerste script
#!/bin/sh
mv file1 file1_20100416
mv file2 file2_20100416
mv file2 file3_20100416
Waarom geen loop?
#!/bin/sh
for f in file1 file2 file3 do
mv "${f}" "${f}_20100416"
done
En een wildcard voor de files
#!/bin/sh
for f in file*
do
mv "${f}" "${f}_20100416"
done
Waarom de filenamen in het script?
#!/bin/sh
for f in "$@"
do
mv "${f}" "${f}_20100416"
done
Ook het achtervoegsel dynamisch
#!/bin/sh APPEND=$1 shift
for f in "$@"
do
mv "${f}" "${f}${APPEND}"
done
Maar ik wil hoofdletters
#!/bin/sh
for f in "$@"
do
mv "${f}" "${f^^}"
Done
Note: "${f^^}" is een bash versie 4 optie.
Voor bash 3 en posix:
$(echo "${f}" | tr "[a-z]" "[A-Z]")
De combinatie
#!/bin/sh
if [ "$1" = -a ] then
APPEND=$2 shift 2
else shift fi
for f in "$@"
do
if [ "${APPEND}" ] then
mv "${f}" "${f}${APPEND}"
else
mv "${f}" "${f^^}"
fi done
Getopts
...
while [ "$ARG" != "?" ] do
getopts :a:u case "$ARG" in
a)
APPEND=${OPTARG}
;;
u)
APPEND=""
;;
?) shift $((OPTIND-1)) break
;;
esac done ...
En nu alle opties
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
Algemene opbouw
●
Initialisatie
●
Getopts
●
Controle opties
●
Loop hernoemen files
●
Testen of het file bestaat
●
Nieuwe naam bepalen
●
Testen of naam gelijk is
●
Hernoemen
Controle op opties
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
Nieuwe filenaam
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
Vragen
Presentatie
●
Presentatie:
●
http://pion.xs4all.nl/lezingen/
–
ShellScripting2010.odp
–
ShellScripting2010.pdf
–
ren
–