Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. set -o errexit -o nounset -o pipefail
  3. IFS=$'\n\t\v'
  4. cd `dirname "${BASH_SOURCE[0]:-$0}"`
  5.  
  6. #functions
  7. function cleanup() {
  8. rm -rf junk
  9. }
  10. function fail() {
  11. >&2 echo $1 # echo the first arg to stderr
  12. exit ${2:-1} # exit $2 or exit 1
  13. }
  14. cleanup
  15.  
  16. # variables & default values
  17. X=${1:-"default"} # X=$1, or "default" if $1 not set
  18. Y=${2:-} # Y=$2, or nothing (use to bypass nounset)
  19.  
  20. # test for empty variable
  21. # this is equivalent to `test -z "$Y"`
  22. # so you can get help with `man test`
  23. if [[ -z "$Y" ]]; then
  24. echo "Y not set"
  25. fi
  26.  
  27. # retrieve exit code from individual pipe sections
  28. # '!' bypasses pipefail
  29. ! echo "my command" | grep "whatever"
  30. if [[ ${PIPESTATUS[1]} -ne 0 ]]; then
  31. echo "grep failed" >&2 # echo to stderr
  32. >&2 echo "grep failed" # works too
  33. fi
  34.  
  35. # start & stop a background process
  36. sleep 10s &
  37. PID=$! # PID of last started background process
  38. ! ps -p $PID &> /dev/null # this will fail if background process failed to start
  39. if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
  40. fail "no background process to kill" 1
  41. else
  42. kill $PID
  43. fi
  44.  
  45. # ask for input
  46. DEFAULT_Z="Z"
  47. read -p "Type a Z (default ${DEFAULT_Z}): " MY_Z
  48. MY_Z=${MY_Z:-${DEFAULT_Z}}
  49.  
  50. # search and replace in one or more files
  51. FILELIST=$'a.sh\tb.sh'
  52. ! 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