0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

シェルスクリプトで呼び出し元プロセスの情報を表示する (take2)

Posted at

シェルスクリプトで呼び出し元プロセスの情報を表示する (take2)

シェルスクリプトで呼び出し元プロセスの情報を表示する で書いた記事の拡張版です。「呼び出し元プロセスの情報を表示する」という観点では追加はありません。子スクリプトがどのように呼ばれたか、デバッグするときに便利な情報を追加しました。

スクリプト定義

parent.sh (親プロセス側)

# !/bin/bash

./child.sh test1 test2 test3
./child.sh test11333 test2 test3

child.sh (子プロセス側)

# !/bin/sh

TIMESTAMP=$(date "+%Y/%m/%d-%H:%M:%S")
CALLER=$(ps $PPID | tail -n 1 | awk '{c="";for(i=5;i<=NF;i++) c=c $i" "; print c}')
echo -n "["
echo -n "$TIMESTAMP"
echo -n "]: "
echo -n "("
echo -n $CALLER
echo -n ") "
echo $*

実行例

$ ./parent.sh 1 2 3
[2021/01/10-09:57:24]: (/bin/sh ./parent.sh 1 2 3) test1 test2 test3
[2021/01/10-09:57:24]: (/bin/sh ./parent.sh 1 2 3) test11333 test2 test3

$ ./parent.sh 1 12 123 | column -t
[2021/01/10-09:58:37]:  (/bin/sh  ./parent.sh  1  12  123)  test1      test2  test3
[2021/01/10-09:58:37]:  (/bin/sh  ./parent.sh  1  12  123)  test11333  test2  test3

参考リンク

column -t は以下を参考にしました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?