2
1

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 5 years have passed since last update.

shellを書いているときに躓いたところ

Posted at

概要

現象は起きているものの、原因がわからなった問題から対処方法がわかったものを書き残します。

コマンド実行後、返り値をshell内で扱うための変数

特殊変数
php index.php #返り値のあるコマンド
echo $? #最後に実行された返り値が表示される

ifで見かける 比較演算子

比較演算子
# 左右の値が等しければ真
if [ $? -eq 0 ]; then
  echo "No Problem :)"
fi

# 左右の値が等しくなければ真
if [ $? -ne 0 ]; then
  echo "Have Problem :("
fi

PHPからshellへ値を返すときの関数

PHPからの書き方
<?php
//正常状態を返す場合
exit;
exit();
exit(0);

//異常状態を返す場合
exit(1);
exit(222);

?>

while内で使っていた変数をwhileの外でも使いたい場合

パイプを使った方法では期待した値が表示されませんでした

期待した結果が表示されない
COUNT=0
cat 外部ファイルのパス | while read line
do
let COUNT++
done
echo $COUNT #whileでループされた分は表示されず、0が表示される
期待した結果が表示される
COUNT=0
while read line
do
let COUNT++
done < 外部ファイルのパス
echo $COUNT #whileでループされた分だけ表示される

参考投稿

Shell 特殊変数
シェルスクリプト(bash)のif文やwhile文で使う演算子について
シェルスクリプト最大の罠、while問題
シェルスクリプトで外部ファイルに記述された変数を利用する方法

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?