4
1

More than 5 years have passed since last update.

suコマンドの終了ステータス

Posted at

suコマンドの-cオプションを使ったときの終了ステータスについて調べてみた。
testユーザは存在し、aaaユーザは存在しない。また、testファイルも存在しない。

まずはsuコマンドの正常終了時
# su - test -c "ls"
# echo $?
出力は0であった。

次にsuコマンドの異常終了時
# su - aaa -c "ls"
# echo $?
出力は1

次にsuコマンドの-cオプションの中のコマンドの異常終了時
# su - test -c "ls test"
# echo $?
出力は2

# ls test
の終了ステータスも2であり、-cオプションが失敗すると2が返されるのか、それとも-cオプションで実行されたコマンドの終了ステータスが返されるのか疑問だったため、以下を実行
# su - test -c "exit 3"
# echo $?
出力は3

まとめ

suコマンド成功で終了ステータス0、失敗で1、-cオプションを使用した場合についてはそのコマンドの終了ステータスが返される。

感想

あとあと考えてみると、suで権限変えた後、-cオプションの中のコマンド実行してるから、当然$?には-cオプションで指定したコマンドの終了ステータスが入ってるなと…

4
1
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
4
1