<commit_before>#!/bin/zsh - 
#===============================================================================
#
#          FILE: waitfor.sh
# 
#         USAGE: ./waitfor.sh <PID>, or
#                ./waitfor.sh <process name>
# 
#   DESCRIPTION: waits for one process to finish without using loops. 
# 
#       OPTIONS: None so far
#  REQUIREMENTS: inotifywait installed
#          BUGS: None so far
#         NOTES: ---
#        AUTHOR: Cláudio "Patola" Sampaio (Patola), <EMAIL>
#  ORGANIZATION: MakerLinux
#       CREATED: 07/18/2017 05:43:26 PM -03
#      REVISION:  01
#===============================================================================

set -o nounset                              # Treat unset variables as an error

usage() {
  echo " Usage:" >&2
  echo "$0 <pid of process>, or" >&2
  echo "$0 <name of process>" >&2
  echo -e "\nWaits for one process to finish by select() instead of polling (i.e., not looping until it exits)." >&2
}

[[ $# -eq 1 ]] || {
  usage;
  exit 3
}

[[ -x /usr/bin/inotifywait ]] || {
  echo "This script requires inotifywait present on the system to work." >&2
  echo "Please install inotifywait." >&2
  exit 54
}

if [[ $1 =~ ^[0-9][0-9]*$ ]] # formed only of digits
then
  PID=$1
  [[ -d /proc/$PID ]] || {
    echo "Process with PID $PID does not exist." >&2
    usage
    exit 4
  }
else
  PID=$(/usr/bin/pgrep "$1")
  [[ $(echo $PID | /usr/bin/wc -w) -gt 1 ]] && {
    echo "There are $(echo $PID | wc -w ) processes with '$1' on it: $(echo $PID | sed ':a;N;$!ba;s/\n/, /g')." >&2
    echo "The command requires only one process." >&2
    usage
    exit 5
  }
fi

/usr/bin/inotifywait -e close /proc/$PID/cgroup > /dev/null 2>&1
exit $?
<commit_msg>Discarded inotifywait for tail and added timeout parameter<commit_after>#!/bin/zsh - 
#===============================================================================
#
#          FILE: waitfor.sh
# 
#         USAGE: ./waitfor.sh <PID>, or
#                ./waitfor.sh <process name>
# 
#   DESCRIPTION: waits for one process to finish without using loops. 
# 
#       OPTIONS: None so far
#  REQUIREMENTS: inotifywait installed
#    EXIT CODES: 4 no PID, 5 more than one named process, 3 more than one process,
#                54 for timeout command not present, 124 for timeout elapsed
#          BUGS: None so far
#         NOTES: ---
#        AUTHOR: Cláudio "Patola" Sampaio (Patola), <EMAIL>
#  ORGANIZATION: MakerLinux
#       CREATED: 07/18/2017 05:43:26 PM -03
#      REVISION:  01
#===============================================================================

set -o nounset                              # Treat unset variables as an error

usage() {
  echo " Usage:" >&2
  echo "$0 <pid of process>, or" >&2
  echo "$0 <name of process>" >&2
  echo -e "\nWaits for one process to finish by select() instead of polling (i.e., not looping until it exits)." >&2
}

zparseopts -D -E -A Args -- t:

TIMECMD=""
(( ${+Args[-t]} )) && {
  if [[ ${Args[-t]} =~ ^[0-9][0-9]*$ ]]
  then
    TIMECMD="/usr/bin/timeout"
    TIMEDURATION="${Args[-t]}"
  else
    echo "Time parameter must be numeric (seconds), '${Args[-t]}' is not." >&2
    usage
    exit 2
  fi
}

[[ $# -eq 1 ]] || {
  usage
  exit 3
}

[[ -x /usr/bin/timeout ]] || {
  echo "This script requires the timeout command present on the system to work." >&2
  echo "Please install coreutils." >&2
  exit 54
}

if [[ $1 =~ ^[0-9][0-9]*$ ]] # formed only of digits
then
  PID=$1
  [[ -d /proc/$PID ]] || {
    echo "Process with PID $PID does not exist." >&2
    usage
    exit 4
  }
else
  PID=$(/usr/bin/pgrep "$1")
  [[ $(echo $PID | /usr/bin/wc -w) -gt 1 ]] && {
    echo "There are $(echo $PID | wc -w ) processes with '$1' on it: $(echo $PID | sed ':a;N;$!ba;s/\n/, /g')." >&2
    echo "The command requires only one process." >&2
    usage
    exit 5
  }
fi

#/usr/bin/inotifywait -e close /proc/$PID/cgroup > /dev/null 2>&1
$TIMECMD $TIMEDURATION /usr/bin/tail -f /dev/null --pid=$PID
exit $?

{'fraction_non_alphanumeric': 0.23282154543166372, 'fraction_numerical': 0.02668009061162849, 'mean_word_length': 2.573741007194245, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1136988', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1372, 'n_words': 447}