Feb-18-2017, 06:29 AM
i have this bash script that is frying nilamo's mind that i want to rewrite into python 3:
it's not in python,yet, it's still in bash:
it's not in python,yet, it's still in bash:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
#!/bin/bash #---------------------------------------------------------------- # Copyright © 2007 - Philip Howard - All rights reserved # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public # License along with this program; if not, write to the # Free Software, Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. #---------------------------------------------------------------- # command logcmd # # purpose Execute a command with logging of output. # # syntax logcmd [options] command # # options -b -c --sh --bash # Execute a command string via the bash shell # Note that the command string is one argument # following all options. # # -d --dir --logdir # Output the log to a file with a generated name # in the specified directory. # # -i --ignore-environment # Do not use the inherited environment variables. # # -f --file --logfile # Output the log to the exact specified file. # # -n --name --logname # Output the log to a file with this name and time. # # -p --prefix # Output the log to a file the this prefix to name. # # -s --suffix # Output the log to a file the this suffix to name. # # author Phil Howard #---------------------------------------------------------------- self = "${0}" if [[ "x${1}" = "x----dropenv" ]] ; then shift _dropenv_flag_ = 1 fi if [[ "x${1}" = "x----exec" ]] ; then if [[ - n "${EXEC_COMMANDS}" ]] ; then trap "echo SIGINT ; exit 1" INT trap "echo SIGTERM ; exit 1" TERM if [[ - n "${_dropenv_flag_}" ]] ; then _command_array_ = ( env - i ) else _command_array_ = ( ) fi _command_index_ = 0 _message_string_ = "" while [[ "${_command_index_}" - lt "${EXEC_COMMANDS}" ]] ; do eval '_one_argument_="${EXEC_COMMAND_' "${_command_index_} "'}" ' case "${_one_argument_}" in ( * = * ) _message_string_ = "${_message_string_} '${_one_argument_}'" eval "export ${_one_argument_}" ;; ( * ) break ;; esac eval "unset EXEC_COMMAND_${_command_index_}" _command_index_ = $[ $_command_index_ + 1 ] done while [[ "${_command_index_}" - lt "${EXEC_COMMANDS}" ]] ; do eval '_one_argument_="${EXEC_COMMAND_' "${_command_index_} "'}" ' _message_string_ = "${_message_string_} '${_one_argument_}'" _command_array_ = ( "${_command_array_[@]}" "${_one_argument_}" ) eval "unset EXEC_COMMAND_${_command_index_}" _command_index_ = $[ $_command_index_ + 1 ] done unset EXEC_COMMANDS _time_string_ = $( exec date '+%H:%M:%S' ) echo "${_time_string_} [$$] EXECUTING:${_message_string_}" time "${_command_array_[@]}" _status_value_ = $? _time_string_ = $( exec date '+%H:%M:%S' ) echo "${_time_string_} [$$] FINISHED - status = ${_status_value_}" sleep 1 else echo "option ----exec used without EXEC_COMMANDS environment variable set" 1 >& 2 _status_value_ = 1 fi exit $_status_value_ fi if [[ "x${1}" = "x----bash" ]] ; then if [[ - n "${SHELL_COMMAND}" ]] ; then trap "echo SIGINT ; exit 1" INT trap "echo SIGTERM ; exit 1" TERM c = "${SHELL_COMMAND}" unset SHELL_COMMAND t = $( exec date '+%H:%M:%S' ) echo "${t} [$$] EXECUTING: ${c}" if [[ - n "${_dropenv_flag_}" ]] ; then time env - i bash - c "${c}" s = $? else time bash - c "${c}" s = $? fi t = $( exec date '+%H:%M:%S' ) echo "${t} [$$] FINISHED - status = ${s}" sleep 1 else echo "option ----bash used without SHELL_COMMAND environment variable set" 1 >& 2 s = 1 fi exit $s fi logdir = "" logfile = "" if [[ - z "${1}" ]] ; then echo "" 1 >& 2 echo "logcmd [-d logdirectory] command [args ...]" 1 >& 2 echo "logcmd [-f logfile] command [args ...]" 1 >& 2 echo "" 1 >& 2 exit 1 fi while [[ "x${1:0:1}" = "x-" ]] ; do case "x${1}" in ( x - b | x - c | x - - sh | x - - bash ) bashcmd = 1 ;; ( x - d | x - - dir | x - - logdir ) shift; logdir = "${1}" ;; ( x - d = * ) logdir = "${1:3}" ;; ( x - - dir = * ) logdir = "${1:6}" ;; ( x - - logdir = * ) logdir = "${1:9}" ;; ( x - i | x - - ignore - environment ) noenv = 1 ;; ( x - f | x - - file | x - - logfile ) shift; logfile = "${1}" ;; ( x - f = * ) logfile = "${1:3}" ;; ( x - - file = * ) logfile = "${1:7}" ;; ( x - - logfile = * ) logfile = "${1:10}" ;; ( x - p | x - - prefix ) shift; prefix = "${1}" ;; ( x - p = * ) prefix = "${1:3}" ;; ( x - - prefix = * ) prefix = "${1:9}" ;; ( x - s | x - - suffix ) shift; suffix = "${1}" ;; ( x - s = * ) suffix = "${1:3}" ;; ( x - - suffix = * ) suffix = "${1:9}" ;; ( * ) echo "Unknown option: ${1}" ; die = 1 ;; esac shift done [[ - z "${die}" ]] || exit 1 pid = $$ tp = $( printf "%s-%06u" $( date '+%Y%m%d-%H%M%S' ) "${pid}" ) [[ - n "${prefix}" ]] && tp = "${prefix}-${tp}" [[ - n "${suffix}" ]] && tp = "${tp}-${suffix}" tp = "${tp}.log" if [[ - n "${logfile}" ]] ; then if [[ - n "${prefix}" || - n "${suffix}" ]] ; then echo "Cannot use --logfile with either --prefix or --suffix" 1 >& 2 exit 1 fi fi if [[ - z "${logfile}" || "x${logfile}" = "x." ]] ; then logfile = "${tp}" if [[ - z "${logdir}" ]] ; then for d in ${HOME} / {.,}cmdlog.d ${HOME} / {.,}cmdlog . / tmp ; do if [[ - d "${d}" && - w "${d}" ]] ; then logdir = "${d}" break fi done unset d fi if [[ - z "${logdir}" ]] ; then echo "No log directory available" 1 >& 2 exit 1 fi export SCRIPT_LOGFILE = "${logdir}/${logfile}" else if [[ - n "${logdir}" ]] ; then export SCRIPT_LOGFILE = "${logdir}/${logfile}" else export SCRIPT_LOGFILE = "${logfile}" fi fi unset logfile unset logdir if [[ $ # -lt 1 ]] ; then echo "${0} [ -d <logdir> | -f <logfile> ] <command> ..." exit 1 fi if [[ - e "${SCRIPT_LOGFILE}" ]] ; then echo "Log file '${SCRIPT_LOGFILE}' already exists" exit 1 fi if [[ - n "${bashcmd}" ]] ; then if [[ $ # -gt 1 ]] ; then shift echo "Extra arguments after shell command: $*" 1 >& 2 exit 1 fi export SHELL_COMMAND = "${1}" unset bashcmd c = "----bash" else i = 0 for a in "$@" ; do eval "export EXEC_COMMAND_${i}=" '"${a}"' i = $[ $i + 1 ] done export EXEC_COMMANDS = "${i}" unset i unset a c = "----exec" fi if [[ - n "${noenv}" ]] ; then c = "----dropenv ${c}" fi d = $( exec dirname "${SCRIPT_LOGFILE}" ) [[ - d "${d}" ]] || mkdir - p "${d}" || exit 1 [[ - d "${d}" ]] || exit 1 unset d exec script - f - c "exec ${self} ${c}" "${SCRIPT_LOGFILE}" |
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.