All pastes #40221 Raw Edit

scripts

public shellscript v1 · immutable
#40221 ·published 2006-02-05 23:29 UTC
rendered paste body
#!/bin/zsh# Trunkate given files (or create them if the don't exist)function purge {	print "Warning, the following files will be destroyed: $@"	read '?Continue? '	for f in $@; do		echo -n "" > $f	done	}# Pretty color printingfunction clr {	cl=$1	shift	print -- "${fg[$cl]}$@$reset_color"}function bclr {	cl=$1	shift	print -- "${fg_bold[$cl]}$@$reset_color"}function red {	clr red $@}function bred {	bclr red $@}function green {	clr green $@}# Replace . with spacesfunction fnorm {	if [[ $# -eq 0 ]]; then set * ; fi	for f in $@; do		dir=$(dirname $f)		file=$(basename $f)		base=${file:r:l}		sufx=${file:e:l}				new=${base//./ }		if [[ -d $f ]]; then			new=${new// /_}		else			new=${new//_/ }		fi		if [[ -n $sufx ]]; then			new=$new.$sufx		fi		#print "$file -> $(green $new)"		nf=$dir/$new		if [[ -e $nf ]]; then			print -- "[$(bred file exists)] $nf"		else			mv -i "$f" "$nf"		fi	done	}function simple_replace {	case $1 in		'underscores'|'u') mode=1;;		'spaces'|'s') mode=2;;		'lower'|'l') mode=3;;		*) return 1;;	esac	shift		if [[ $# -eq 0 ]]; then set * ; fi	for f in $@; do		dir=$(dirname $f)		file=$(basename $f)		case $mode in 			1) file=${file// /_} ;;			2) file=${file//_/ } ;;			3) file=${file:l} ;;		esac		new="$dir/$file"		if [[ ! -e $new ]]; then			if mv "$f" "$new"		fi	done}function underscores {	simple_replace u $@}function spaces {	simple_replace s $@}function lower {	simple_replace l $@}# Extract raw H264 streams from AVI files, and mux to MP4function avi2mp4 {	if [[ -z $1 ]] ; then		print -- "usage: $(basename $0) video.avi"		return 1	fi	mp4="MP4Box"	file="$1"	# emulate mp4box name generation	base="${file:r}"	h264="_video.h264"	tmp="${base}${h264}"		cmd="$mp4 -aviraw video $file"	print -- "[extracting]: $cmd"	if eval $cmd ; then		cmd="$mp4 -add ${tmp} ${base}.mp4"		print -- "[muxing]: $cmd"		if eval $cmd ; then			cmd="rm ${tmp}"			print -- "[cleanup]: $cmd"			if eval $cmd ; then return 0 ; fi		fi	fi	return 1}# Wrap around MKVmerge with support for AVI muxed H264 filesfunction mkv264 {	params=()	for f in $@; do		if [[ -n $(file -- $f | grep -i 'riff.*avi') ]]; then			avifile="$f"			mp4file="${f:r}.mp4"			params+="$mp4file"		else			params+="$f"		fi	done	if [[ -e $avifile ]]; then		if avi2mp4 $avifile ; then			cmd="mkvmerge $params"			print -- "[merging]: $cmd"			if eval $cmd ; then				cmd="rm $mp4file"				print -- "[cleanup]: $cmd"				eval $cmd				return 0			fi		fi	fi	return 1}# Simple alarm clockfunction alarm {	usage="usage: $(basename $0) HH[:MM[:SS]] [action]"	if [[ $# -lt 1 ]]; then		print -- $usage		return 1	fi	wtime=$1	shift	if [[ $# -gt 0 ]]; then		action="$@"	else		action="dcop amarok player play"	fi	wsplit=(${(s.:.)wtime})	integer H=${wsplit[1]} M=${wsplit[2]:-0} S=${wsplit[3]:-0}	if [[ $#wsplit > 3 || $H -ge 24 || $M -ge 60 || $S -ge 60 ]]; then 		print -- $usage		return 1	fi	dtime=$(date +%T)	ds=(${(s.:.)dtime})	integer CH=${ds[1]} CM=${ds[2]} CS=${ds[3]}	(( sleeptime = ($H - $CH) * 3600 + ($M - $CM) * 60 + $S - $CS ))	if (( $sleeptime < 0 )); then (( sleeptime = (24*3600) + $sleeptime )); fi	sleep $sleeptime	print -- "[$(red alarm) $(green $wtime)]: $action"	if eval $action; then return 0; fi	return 1}alarm $@