Tutorial Bash 02

Embed Size (px)

Citation preview

  • 8/13/2019 Tutorial Bash 02

    1/2

    Tobias Neckel Max-Planck, October 2013

    Bash course - Tutorial 2

    File renamer

    Sometimes, large numbers of files have to be renamed, e.g. by adding a prefix, appendinga suffix or replacing a certain pattern. In this task, you should write a program doing thatautomatically. The program should have three different modes:

    ./file renamer prefix +

    ./file renamer suffix +

    ./file renamer replace +

    a) If the prefix mode is executed, the given prefix should be added to all filenames

    b) If a file has no ., the suffix should just be appended to the filename. But if there is a. in the filename, the suffix should be appended before the dot. So name.txt shouldbecome namesuffix.txt and name should become namesuffix

    c) The replace-mode just applies the given pattern

    Personal Address Manager

    In this task, you have to implement a basic address manager, which you can use to add contactinformation (first name, last name, email and phone number) to a local database, search forentries, delete entries, ... There are many ways to solve the task, even avoiding all bash toolsand implementing everything yourself using basic control structures. But the purpose of thistask is to practise the use of regular expressions and several of todays bash tools, e.g. cat,

    cut, sed, awk, grep, sort, ..., so really try to make use of these tools.

    a) As a start, let your program accept the following parameters:

    -a add a new entry

    -d = [=...]delete all entries which satisfyall conditions

    -f = [=...] find all entries which satisfyall conditions

  • 8/13/2019 Tutorial Bash 02

    2/2

    2

    -l whereis one of{firstname, lastname, email,phone}. Lists all entries sorted by the given column.

    You can use the following code-block to process the parameters:

    case $1 in

    "-a" ) add;;

    "-d" ) shift; delete $*;;

    "-f" ) shift; find $*;;

    "-l" ) shift; list $*;;

    esac

    add, delete, find and list are four functions which you will have to implement.

    b) Implement the add function. It should query the user for first name, last name, emailand phone number. For the email, it should be checked whether the entered string is avalid email address. If not, the user should be asked again. Accepted telephone numbersshould consist of numbers and whitespaces, one - or / is allowed as separator, e.g.:

    089 289 18 636

    089 / 289 18636

    089-28918636

    Use regular expressions to ensure that all such phone numbers are accepted. Beforestoring them, you should transform them such that they match the pattern[[:digit:]]\+/\?[[:digit:]]\+, which means that all whitespaces should be remo-

    ved and only /should be used as a separator (or no separator at all).

    c) Implement all other functions (delete, find, list)