#!/usr/bin/ksh93 ################################################################ #### Program: xargs_k93 #### #### Description: Execute a command once using each line of standard #### input as command line arguments #### #### Author: Dana French (dfrench@mtxia.com) #### #### Date: 07/22/2004 #### ################################################################ function usagemsg_xargs_k93 { print " Program: xargs_k93 Reads items from the standard input, delimited by blanks or newlines, and executes the command (default is \"print\") one or more times with any initial-arguments followed by items read from standard input. Blank lines on the standard input are ignored. Usage: ${1##*/} [-?vV] [-a file] [-E|e eof-str] [-i replace-str] [-L|l max-lines] [-n max-args] [-s max-chars] [-p] [-r] [-t] [-x] Where: -v = Verbose mode -V = Very Verbose Mode -a file Read items from file instead of standard input. If you use this option, stdin remains unchanged when commands are run. Other‐ wise, stdin is redirected from /dev/null. -E [eof-str], -e [eof-str] Set the end of file string to eof-str. If the end of file string occurs as a line of input, the rest of the input is ignored. If eof-str is omitted, there is no end of file string. If this option is not given, no end of file string is used. -i [replace-str] Replace occurences of replace-str in the initial-arguments with names read from standard input. Also, unquoted blanks do not terminate input items; instead the separator is the newline character. If replace-str is omitted, it defaults to \"{}\" (like for 'find -exec'). Implies -x and -l 1. -L [max-lines], -l [max-lines] Use at most max-lines nonblank input lines per command line; max-lines defaults to 1 if omitted. Trailing blanks cause an input line to be logically continued on the next input line. Implies -x. -n max-args Use at most max-args arguments per command line. Fewer than max-args arguments will be used if the size (see the -s option) is exceeded, unless the -x option is given, in which case xargs will exit. -p Prompt the user about whether to run each command line and read a line from the terminal. Only run the command line if the response starts with 'y' or 'Y'. Implies -t. -r If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. -s max-chars Use at most max-chars characters per command line, including the command and initial-arguments and the terminating nulls at the ends of the argument strings. The default is 131072 characters, not including the size of the environment variables (which are provided for separately so that it doesn't matter if your envi‐ ronment variables take up more than 131072 bytes). The operat‐ ing system places limits on the values that you can usefully specify, and if you exceed these a warning message is printed and the value actually used is set to the appropriate upper or lower limit. -t Print the command line on the standard error output before exe‐ cuting it. -x Exit if the size (see the -s option) is exceeded. Author: Dana French (dfrench@mtxia.com) Copyright 2005 \"AutoContent\" enabled " } ################################################################ function xargs_k93 { typeset TRUE="0" typeset FALSE="1" typeset VERBOSE="${FALSE}" typeset VERYVERB="${FALSE}" typeset USEINFILE="${FALSE}" typeset INPUTFILE="" typeset EOFSTR="" typeset RPLSTR="" typeset MAXLINES="" typeset MAXARGS="" typeset PROMPTUSER="${FALSE}" typeset DONOTRUN="${FALSE}" typeset MAXCHARS="" typeset PRINTCMD="${FALSE}" typeset EXITSIZE="${FALSE}" typeset CMDRUN="" typeset CMDOK="${TRUE}" while getopts ":vVa:E:e:i:Llnprstx" OPTION do case "${OPTION}" in 'v') VERBOSE="${TRUE}";; 'V') VERYVERB="${TRUE}";; 'a') USEINFILE="${TRUE}" INPUTFILE="${OPTARG}";; 'E') EOFSTR="${OPTARG}";; 'e') EOFSTR="${OPTARG}";; 'i') RPLSTR="${OPTARG}";; 'L') MAXLINES="${OPTARG}";; 'l') MAXLINES="${OPTARG}";; 'n') MAXARGS="${OPTARG}";; 'p') PROMPTUSER="${TRUE}";; 'r') DONOTRUN="${TRUE}";; 's') MAXCHARS="${OPTARG}";; 't') PRINTCMD="${TRUE}";; 'x') EXITSIZE="${TRUE}";; '?') usagemsg_xargs_k93 "${0}" && exit 1;; esac done shift $(( ${OPTIND} - 1 )) typeset CMD="${@:-print -r --}" ################################################################ trap "usagemsg_xargs_k93 ${0}" EXIT if (( USEINFILE == TRUE )) && [[ ! -s "${INPUTFILE}" ]] then print -u 2 "# ERROR: Input file does not exist" return 2 fi trap "-" EXIT (( VERYVERB == TRUE )) && set -x ################################################################ (( USEINFILE == TRUE )) && exec 0<"${INPUTFILE}" while read -r -- CMDARGS do #### #### If the "-r" option was specified on the command line, check #### each line read from standard input and check to see if it contains #### any non-blank characters, if not, skip this input line. #### if (( DONOTRUN == TRUE )) then [[ "_${CMDARGS}" = _*([$' \t\n']) ]] && continue fi #### #### If the -E or -e option was specified on the command line, #### then check the line read from standard input to see if it matches #### the EOF string specified on the command line. If it matches, #### then break out of the while loop and return from this function. #### [[ "_${EOFSTR}" != "_" && "_${EOFSTR}" = "_${CMDARGS}" ]] && break #### #### Build the command to run using the remaining command line arguments #### followed by the line read from standard input. If the "-i" option #### was specified on the command line, then replace the occurances of #### the replacement string specified on the command line in the #### remaining command line arguments, with the line read from standard #### input. #### CMDRUN="${CMD} ${CMDARGS}" if [[ "_${RPLSTR}" != "_" ]] then CMDRUN="${CMD/${RPLSTR}/${CMDARGS}}" fi #### #### If the "-t" option was specified on the command line and #### the "-p" option was not, then print the command to be executed #### to standard output. #### (( PRINTCMD == TRUE )) && (( PROMPTUSER == FALSE )) && print "${CMDRUN}" #### #### If the "-p" option was specified on the command line, then prompt #### the user with each command and ask whether or not to execute #### the command. Any response other than "Y" or "y" is regarded as #### a negative response. #### CMDOK="${TRUE}" if (( PROMPTUSER == TRUE )) then print -n "${CMDRUN} ?..." read REPLY < /dev/tty [[ "_${REPLY}" != _@(Y|y)* ]] && CMDOK="${FALSE}" fi #### #### Execute the command if the "-p" option was not specified on the #### command line, or if the "-p" option was specified, execute the #### command if the user replied that this command should be executed. #### (( CMDOK == TRUE )) && ${CMDRUN} done return 0 } ################################################################ xargs_k93 "${@}"