Bash

Mitigate typos and errors in your scripts

# Tell bash that it should exit your script if any statement returns a non true return value:
set -e
 
# This will exit your script if you try to use an uninitialised variable:
set -u

Some useful bash shell constructs or one liners:

for ((a=0; a <= 5 ; a++)) do mkdir weekly.$a; done

or

for i in 0 1 2 3; do mkdir weekly.$i; done

Create 4 directories:
weekly.0 weekly.1 … weekly.4

Something special:

for i in *; do cd $i; [ -d somedir ] && rm -r somedir; cd ..; done

List the current directory (assumed there are only directories, no files). Go into every subdirectory ($i). Look if there is “somedir”. If it exists rm -r somedir. Move back and go into the next directory ($i)…

Transform data:
The data is given in the following form:

1.3. 19.00 YouGo-Team
2.3. 19.00 Weltgebetstag in der Christuskirche, Ohmbach
6.3. 20.00 Probe Liturgischer Singkreis

and should be formatted for dokuwiki tables:

^1.3.|19.00 Uhr| YouGo-Team|
^2.3.|19.00 Uhr| Weltgebetstag in der Christuskirche, Ohmbach|
^6.3.|20.00 Uhr| Probe Liturgischer Singkreis|
#!/bin/bash
set -u
set -e
 
while read line 
do
	# extract date
        DATUM=$(echo $line | cut -d ' ' -f1)
        # extract time
	ZEIT=$(echo $line | cut -d ' ' -f2)
        # remove date from from $line
	line=${line#$DATUM}
        # remove time from $line
	line=${line# $ZEIT}
        # some special formatting to do rowspan
	if [ $DATUM == ":::" ]; then
		DATUM=" ::: "
	fi
        # center '-' in table col
	if [ $ZEIT == "-" ]; then
		ZEIT='  -  '
	else
                # append the Word "Uhr" to time
		ZEIT="$ZEIT Uhr"
	fi
        # reassemble the output line:
	OUTPUT="^${DATUM}|${ZEIT}|$line|"
	echo ${OUTPUT}
done < "termine-neu"
shell/bash.txt · Last modified: 2012/03/04 12:22 by tb
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0