LoginSignup
2
1

More than 5 years have passed since last update.

引数の数をcountでチェックするなら一度変数に入れる

Posted at
ダメ
function hoge
    if count $argv -ne 1
       echo "error argv"  $argv
       return
    end
end
いい
function hoge
    set length (count $argv)
    if test $length -ne 1
       echo "error! argv" $argv
       return
    end
end

countの結果は標準出力に出力されるので
逆に終了ステータスで判定したい場合はif countの最後に> /dev/nullとリダイレクトして出力を捨てないといけない。
if countとすると終了ステータスのチェックになるのでだめ。

久しぶりに書くとうっかりこうなる。

bashの$#はないのかな?

引数を処理する | UNIX & Linux コマンド・シェルスクリプト リファレンス


と思ってたんですが、ダメな方をよく見ると、引数なしの実行で標準出力に吐かれるintが2。引数を足していくとインクリメントしていく。
set length (count $argv)の変数の値と、count $argvの出力が一致しないので、何か見落としている気がする。

2
1
2

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