LoginSignup
0
0

More than 1 year has passed since last update.

jps を associative array に格納する

Last updated at Posted at 2021-11-02

取得

jps.sh
JPS=<PATH_TO_JPS>/jps

# 宣言
declare -A JPS_ASSOCIATE_ARRAY=(); 

# jps からの読み込み
while read -r a b; 
do
  JPS_ASSOCIATE_ARRAY["$a"]="$b"; 
done < <($JPS)

#結果

$ declare -p JPS_ASSOCIATE_ARRAY
declare -A JPS_ASSOCIATE_ARRAY ='([22870]="Jps" [2727]="CassandraDaemon" ...

使用方法

# zsh の場合

% echo ${(k) JPS_ASSOCIATE_ARRAY}          
"85853" "75014" "85855"

% echo ${(v) JPS_ASSOCIATE_ARRAY}
Jps org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar Jps

% echo ${(kv) JPS_ASSOCIATE_ARRAY}
"85853" Jps "75014" org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar 

# bash の場合

$ echo ${JPS_ASSOCIATE_ARRAY[@]}
3420 5749 6984 5516 6494 7649 10606 2727 9507 6507 9530

$ echo ${! JPS_ASSOCIATE_ARRAY[@]}
Bootstrap Jps .....

注1) Bash での Associate Array は Version 4 からになります。
注2) java 以外も ps -e -o pid,comm で代替可能

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0