Download presentation
Presentation is loading. Please wait.
Published byJoris Peeters Modified over 6 years ago
1
Shell scripting From single commands to a shell script
Eth-0 zomer 2010 Wieringerwerf 10 Augustus Marcel Nijenhof
2
Shell Scripting?
3
Marcel Nijenhof Nllgg Chairman 10 Year Board member LPI Nederland
Proctor Proxy Employee Unix administrator
4
The script
5
Single commands $ mv file1 file1_20100416 $ mv file2 file2_20100416
6
The first script #!/bin/sh mv file1 file1_20100416
7
#!/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}_ " done
8
#!/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}_ " done
9
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}_ " 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=$ # ren -a _ file* shift 2 else Shift # ren * fi for f in do if [ "${APPEND}" ] mv "${f}" "${f}${APPEND}" mv "${f}" "${f^^}" 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 *.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 ] echo "ERROR: Replace is only valid with search" >&2 if [ "$#" -le 0 ] echo "ERROR: No file list" >&2
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.