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:
#!/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}"