shpat(42) Language: English


Shell Scripting Patterns

----------------------------------------------------------------------[ Meta ]--

name		shpat
section		42
description	Shell Scripting Patterns
tags		sh patterns montage avconv note
encoding	utf8
compliance	informal
lang		en
creation	2014/06/28 00:16:24
expires		2017/01
version		1.1.0.0
copyright	Copyright (c) 2013, 2014 Ma_Sys.ma.
		For further info send an e-mail to Ma_Sys.ma@web.de.

-------------------------------------------[ Add after end of stream pattern ]--

	... | (cat; echo add) | ...

----------------------------------------------[ Multiple-step script pattern ]--

	#!/bin/sh

	conf=...

	case "$1" in
	(1)
		echo ...;;
	(2)
		echo ...;;
	(3)
		echo ...;;
	(--help|-h)
		echo "Usage $0 [1|2|3]";;
	(*)
		echo "Try $0 --help.";;
	esac

-----------------------------------------------------------[ Visitor pattern ]--

	#!/bin/sh

	lines=a.txt

	iterate() {
		while read -r line; do
			if [ -n "$line" ]; then
				"$1" "$i"
			fi
		done < "$lines"
	}

	visit() {
		echo "VISIT $1"
	}

	iterate visit

-----------------------------------------------------[ Basic program pattern ]--

	#!/bin/sh
	#===
	# App, Copyright...
	# For f...
	#===

	main() {
		if [ $# = 0 ]; then
			help
			return 1
		fi
		case "$1" in
		(-a|--a-action)
			shift
			invoc_a "$@";;
		(*)
			echo "Try $0 --help"
			return 1;;
		esac
		return $?
	}

	help() {
		head -n 5 "$0" | tail -n 4 | cut -c 2-
		echo
		cat <<EOF
	Usage $0 ...
	EOF
	}

	invoc_a() {
		echo "A $*"
	}

	main "$@" || exit $?

---------------------------------------------------[ Data extraction pattern ]--

flexible
	`f1="$(echo "$fs" | cut -d "," -f f)"`
fast
	`f1=${fs%%,*}`, `f2=${fs#*,}`
	(use recursively to get remaining fields...)

Special case for file extension matching

	if [ "${fn##*.}" = jpg ]; then
		: file ends w/ .jpg
	fi

-----------------------------------------------------------[ Timeout pattern ]--

	long-cmd & pid=$!
	(sleep timeout; kill $pid) & timeoutpid=$!
	wait $pid && kill $timeoutpid

----------------------------------------[ Java recursive compilation pattern ]--

	javac -cp ... $(find -name '*.java')

----------------------------------[ Script requires root permissions pattern ]--

	if [ "$(id -u)" = 0 ]; then
		if [ "$1" = "suinvoc" ]; then
			shift
		fi
		echo Your commands go here
	elif [ "$1" = "suinvoc" ]; then
		echo Fatal error related to the su invocation. Id not 0: $(id -u).
	else
		# unfortunately fails to escape properly...
		exec su -c "$0 suinvoc $*"
	fi

----------------------------------------------------------------[ LD_PRELOAD ]--

Multiple entries are space separated.
url(http://stackoverflow.com/questions/8474363/specifying-multiple-files-with-
								ld-preload)

------------------------------------------------------------[ Warn all users ]--

	$ echo message | wall

------------------------------------------------------------[ Convert to pdf ]--

	$ convert *.png result.pdf

-----------------------------------------------[ Kill bash w/o error message ]--

url(http://stackoverflow.com/questions/81520/how-to-suppress-terminated-message-
							after-killing-in-bash)

	$ kill $! 2> /dev/null

----------------------------------------------[ Monitor memory and CPU usage ]--

	$ ps -o comm,c,%mem,vsz,rss -p PID

------------------------[ Image Magick auto-rotate into the correct position ]--

url(http://superuser.com/questions/36645/how-to-rotate-images-automatically-
							based-on-exif-data)

Q&D
 * `$ mv $i $i.bak`
 * `$ convert -auto-orient $i.bak $i`
 * `$ rm $i.bak`

----------------------------------------------------[ Check if being sourced ]--

url(http://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-
								being-sourced)

	if [ "$0" = "<intended name>" ]; then
		: not sourced
	else
		: sourced
	fi

---------------------------------------[ Using avconv for Kater der Theather ]--

	$ avconv -i 'concat:VTS_01_1.VOB|VTS_01_2.VOB|VTS_01_3.VOB' -map :0:0 \
		-map :2:1 -acodec libvorbis /tmp/kater.ogv
	$ avconv -i 'concat:VTS_01_1.VOB|VTS_01_2.VOB|VTS_01_3.VOB' -map :0:0 \
		-map :2:1 -acodec libvorbis -q:v 10 -q:a 6 /tmp/kater.ogg

--------------------------------------------[ Write error messages to stderr ]--

url(http://linuxcommand.org/wss0150.php)

	echo MSG 1>&2

-------------------------------------------------------------------[ Montage ]--

url(http://www.imagemagick.org/Usage/montage/)

	montage font_[1-7].gif  -tile x1  -geometry 16x16+1+1  tile_x1.gif
	montage font_[1-7].gif  -tile x2  -geometry 16x16+1+1  tile_x2.gif
	montage font_[1-7].gif  -tile x4  -geometry 16x16+1+1  tile_x4.gif
	montage font_[1-7].gif  -tile 4x  -geometry 16x16+1+1  tile_4x.gif
	montage font_[1-7].gif  -tile 5x  -geometry 16x16+1+1  tile_5x.gif
	montage font_[1-7].gif  -tile 9x  -geometry 16x16+1+1  tile_9x.gif

----------------------------------------------------------[ Password pattern ]--

A simple example

	stty -echo
	read password
	stty echo

----------------------------------------------------[ Secure temporary files ]--

	( umask 077 && mkdir /tmp/tmpdir$$ )

This prevents ``symlink before'' attacks and intermittent file modifications.
Problem is missing randomness (can be DoSed)


Zum Seitenanfang