LoginSignup
0
0

More than 3 years have passed since last update.

[sh]コマンドの実行結果を変数に格納する方法

Posted at

シェル内で、コマンドを実行し、結果を分割して、変数に格納したくなった時に利用しています。

$ set $(echo "1 2 3" | awk -F" " '{print $1, $2, $3}' )
$ echo $1
1
$ echo $2
2
$ echo $3
3

psコマンドと組み合わせて、PPID=1になっているゾンビプロセスを
退治するのに使っています。

cmd="ゾンビプロセス名"   # バグにより生まれるゾンビプロセス
ppid=1                  # PPID=1

ps all | while read F UID PID
do
    set $(ps o pid,ppid,cmd -p $PID | tail -n1 | awk -F" " '{print $1, $2, $3}'
    if [ $ppid = $2 ] && [ $cmd = $3 ] ; then
        sudo kill -9 $PID
    fi
done
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