rokkonet

PC・Androidソフトウェア・アプリの開発・使い方に関するメモ

親プロセスのコマンド、コマンドライン引数を取得するシェルスクリプト

2020 Apr. 19.

## get parent process which started this script
# 6th column of "ps ax" is parent command
# columns after 7th are arguements of parent command
NumColPs=`ps ax | grep $PPID | grep -v 'grep' | awk '$1 == '"${PPID}"' {print NF}'`
if [ ${NumColPs} -gt 5 ]
then
  ## case of some parent command started this script
  # get parant command & arguements to store to array
  #   String in ParentStr of result of awk is separated by LF not space.
  ParentStr=`ps ax | grep $PPID | grep -v 'grep' | awk '{for (i = 6; i <= NF; i++ ) print $i 
}'`
  # replace LF to space in ParentStr
  #   ParentStr: 空白区切りの親プロセスのコマンドとコマンドライン引数文字列
  #   ParentStrArray: 親プロセスのコマンドとコマンドライン引数を格納した配列
  ParentStr=`echo $ParentStr | sed 's/\n/ /g'`
  ParentStrArray=($ParentStr)
fi