clobber.sh 1.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

# LECA Bash library
#
# The LECA bash library provides a set of function used for helping
# development of bash script mainly to write job script on the luke
# cluster
#
# The files from the LECA Bash library must be sourced from your main script
#
#
# Manages the clobber/noclobber property of the bash shell
# ========================================================
#
# When the noclobber mode is on you cannot substitute an existing file by a
# empty one using a unix redirection.
#
# Functions provided:
#
19 20 21
#    - setnoclobber
#      Sets the noclobber mode on.
#      You cannot erase an existing file by a redirection
22
#
23 24 25
#    - setclobber
#      Sets the noclobber mode off.
#      You can erase an existing file by a redirection
26
#
27 28 29 30
#    - getnoclobber
#      Returns the noclobber status as a string
#          -  on: the noclobber mode is on
#          - off: the noclobber mode is off
31
#
32 33
#    - pushclobber
#      Saves the current noclobber mode on a stack and sets it to 'off'
34
#
35 36
#    - pushnoclobber
#      Saves the current noclobber mode on a stack and sets it to 'on'
37
#
38 39
#    - popclobber
#      Restores the last pushed noclobber mode
40
#
41 42
#    - popnoclobber
#      Alias for the popclobber function
43 44 45
#


46 47 48 49
include logging
logdebug "Load clobber package"

include stacks
50 51 52 53 54

newstack __LECABASHLIB_CLOBBERSTACK__


function setnoclobber() {
55
  set -o noclobber
56 57 58
}

function setclobber() {
59
  set +o noclobber
60 61 62
}

function getnoclobber() {
63
  set -o | awk '($1 ~ /noclobber/) {print $2}'
64 65 66
}

function pushclobber() {
67 68
  pushvalue __LECABASHLIB_CLOBBERSTACK__ $(getnoclobber)
  setclobber
69 70 71
}

function pushnoclobber() {
72 73
  pushvalue __LECABASHLIB_CLOBBERSTACK__ $(getnoclobber)
  setnoclobber
74 75 76
}

function popclobber() {
77 78
  local state=0
  popvalue __LECABASHLIB_CLOBBERSTACK__ state
79

80 81 82 83 84 85 86 87 88
  if [[ "$?" == "0" ]]; then
    if [[ "${state}" == "on" ]]; then
      setnoclobber
    else
      setclobber
    fi
  else
    return 1
  fi
89

90
  return 0
91 92 93 94

}

function popnoclobber() {
95 96
  popclobber
  return $?
97
}