Remove operation help.
[anna.git] / example / diameter / launcher / resources / scripts / operation_signal.sh
index da0449b..87e4ce5 100755 (executable)
@@ -16,17 +16,17 @@ _exit() {
 
 usage() {
   echo
-  echo "Usage: $0 [-h|--help] [-t|--timeout <value>] [-f|--file] <data>"
+  echo "Usage: $0 [-h|--help] [-t|--timeout <value>] [-f|--file <file>] [-p|--ping] [operation] "
   echo
-  echo "       -h|--help:     this usage help."
-  echo "       -t|--timeout:  timeout for operation in seconds."
-  echo "                      Defaults to $TIMEOUT__dflt seconds if not provided."
-  echo
-  echo "       -f|--file:     the parameter 'data' will be interpreted as a file"
-  echo "                      with one operation per line. If missing, it will be"
-  echo "                      a single operation string."
-  echo
-  echo "       data:          operation string or file with several operations."
+  echo "       -h|--help:             this usage help."
+  echo "       -t|--timeout <value>:  timeout for operation in seconds."
+  echo "                              Defaults to $TIMEOUT__dflt seconds if not provided."
+  echo "       -f|--file <file>:      file with one operation per line (comments # allowed)."
+  echo "       -p|--ping:             Check the target process id."
+  echo "                              Returns 1 (dead) or 0 (alive)."
+  echo "       operation:             quoted operation string. Will be ignored if file"
+  echo "                              option is present. It is a positional argument"
+  echo "                              (the last one) when present."
   echo
   echo "       For example:"
   echo "          $0 help"
@@ -39,10 +39,12 @@ usage() {
 parse_arguments() {
   is_file=
   timeout=$TIMEOUT__dflt
-  data=
+  file=
+  operation=
+  ping=
 
   while [ $# -gt 0 ]; do
-    case $1 in
+    case "$1" in
       -h|--help)
         usage
       ;;
@@ -55,28 +57,33 @@ parse_arguments() {
 
       -f|--file)
         is_file=yes
-        data=$2
-        [ -z "$data" ] && _exit "Missing file"
-        [ ! -f "$data" ] && _exit "Can't found provided file '$data'."
+        file="$2"
+        [ -z "$file" ] && _exit "Missing file"
+        [ ! -f "$file" ] && _exit "Can't found provided file '$file'."
         shift
       ;;
 
+      -p|--ping)
+        ping=yes
+      ;;
+
       *)
         first=$(echo $1 | cut -c1)
         [ "$first" = "-" ] && _exit "Unsupported script option: $1. Type '$SCR_BN -h' (or --help) to print the available options."
-        data=$1
+        operation="$@"
+        break
       ;;
     esac
     shift
   done
 
-  [ -z "$data" ] && _exit "Missing data value"
+  [ -z "$is_file" -a -z "$operation" -a -z "$ping" ] && _exit "Missing operation or file with operations"
 }
 
 # $1: pid to check
 check_pid() {
   kill -0 $1 2>/dev/null
-  [ $? -ne 0 ] && _exit "Operation error: missing process with pid $1"
+  return $?
 }
 
 #############
@@ -91,29 +98,35 @@ PID=`cat .pid`
 [ "$1" = "" -o "$1" = "--help" -o "$1" = "-h" ] && usage
 parse_arguments $@
 
+# Check pid:
+check_pid $PID
+res=$?
+[ -n "$ping" ] && exit $res
+[ $res -ne 0 ] && _exit "Operation error: missing process with pid $PID"
+
 # Send operation:
 if [ -n "$is_file" ]
 then
-  cp $data sigusr2.in
+  cp $file sigusr2.in
 else
-  echo $data > sigusr2.in
+  echo $operation > sigusr2.in
 fi
 0>sigusr2.out
 check_pid $PID
 kill -s SIGUSR2 $PID
 
 # Detect EOF and print all except that last line:
-count=$((10*timeout))                                                      
+count=$((10*timeout))
 expired=yes
-while [ $count -gt 0 ]                                           
-do                                                                               
-  sleep 0.1                                                                      
-  count=$((count-1))                                                             
+while [ $count -gt 0 ]
+do
+  sleep 0.1
+  count=$((count-1))
   if tail -1 sigusr2.out | grep "^EOF" >/dev/null; then
     expired=
     break;
   fi
-done                                                                             
+done
 
 if [ -z "$expired" ]
 then