terom@60: #!/bin/bash terom@52: # terom@52: # Utility functions terom@52: terom@52: ### Command execution terom@52: ## Execute command, possibly logging its execution. terom@52: # terom@52: # cmd $cmd... terom@52: # terom@52: # Fails if the command returns an error exit code. terom@52: function cmd { terom@52: log_cmd "$@" terom@52: terom@83: "$@" || die "Failed: $@" terom@52: } terom@52: terom@66: ## Execute command as a test, logging its execution at log_cmd terom@66: # terom@66: # cmd_test $cmd... && ... || ... terom@66: # terom@66: # Fails if the command returns an error exit code. terom@66: function cmd_test { terom@66: log_cmd "$@" terom@66: terom@66: "$@" terom@66: } terom@55: ## Execute command, prefixing its output on stdout with given indent prefix. terom@55: # terom@55: # indent " " $cmd... terom@55: # terom@55: # Output is kept on stdout, exit status is that of the given command. terom@52: function indent () { terom@85: local indent="$1"; shift terom@52: terom@52: "$@" | sed "s/^/$indent/" terom@52: terom@52: return ${PIPESTATUS[0]} terom@52: } terom@52: terom@52: terom@52: ### FS utils terom@82: # Create dir if not exists. terom@52: function ensure_dir { terom@85: local dir="$1" terom@52: terom@85: if [ ! -d "$dir" ]; then terom@52: log_warn "Creating output dir: $dir" terom@85: cmd mkdir "$dir" terom@52: fi terom@52: } terom@52: terom@82: ## Output absolute path terom@52: # terom@52: # abspath $path terom@52: # terom@82: # XXX: improve...? terom@52: function abspath () { terom@85: local path="$1" terom@52: terom@82: echo "$SRV/$path" terom@52: } terom@52: terom@69: ## List names of files in dir: terom@69: # terom@69: # list_files $dir $glob terom@69: # terom@69: function list_files { terom@85: local dir="$1" terom@85: local glob="${2:-*}" terom@69: local name= terom@69: terom@69: for file in $dir/$glob; do terom@82: # only files terom@85: [ -f "$file" ] || continue terom@82: terom@69: # strip prefix terom@69: name=${file#$dir/} terom@69: name=${name%$glob} terom@69: terom@69: echo -n "$name " terom@69: done terom@69: } terom@69: terom@82: ## List names of files in dirs.. terom@82: function list_dirs { terom@82: for dir in "$@"; do terom@82: for file in $dir/*; do terom@82: echo -n "${file#$dir/}" terom@82: done terom@82: done terom@52: }