Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- set -o errexit -o nounset -o pipefail
- IFS=$'\n\t\v'
- cd `dirname "${BASH_SOURCE[0]:-$0}"`
- #functions
- function cleanup() {
- rm -rf junk
- }
- function fail() {
- >&2 echo $1 # echo the first arg to stderr
- exit ${2:-1} # exit $2 or exit 1
- }
- cleanup
- # variables & default values
- X=${1:-"default"} # X=$1, or "default" if $1 not set
- Y=${2:-} # Y=$2, or nothing (use to bypass nounset)
- # test for empty variable
- # this is equivalent to `test -z "$Y"`
- # so you can get help with `man test`
- if [[ -z "$Y" ]]; then
- echo "Y not set"
- fi
- # retrieve exit code from individual pipe sections
- # '!' bypasses pipefail
- ! echo "my command" | grep "whatever"
- if [[ ${PIPESTATUS[1]} -ne 0 ]]; then
- echo "grep failed" >&2 # echo to stderr
- >&2 echo "grep failed" # works too
- fi
- # start & stop a background process
- sleep 10s &
- PID=$! # PID of last started background process
- ! ps -p $PID &> /dev/null # this will fail if background process failed to start
- if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
- fail "no background process to kill" 1
- else
- kill $PID
- fi
- # ask for input
- DEFAULT_Z="Z"
- read -p "Type a Z (default ${DEFAULT_Z}): " MY_Z
- MY_Z=${MY_Z:-${DEFAULT_Z}}
- # search and replace in one or more files
- FILELIST=$'a.sh\tb.sh'
- ! sed -i -- "s|my search|${MY_Z}|g" ${FILELIST} &>/dev/null # ignore both stdout and stderr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement