まず、eval
特に必要のない場合は悪です。あなたの場合、必要eval
ありません!
あなたが示したコーディングの恐怖を次のように置き換えます。
ls | head -1
それをテストステートメントに含めるには:
if [[ $(ls | head -1) = "arguprog.sh" ]]; then echo "TRUE"; else echo "FALSE"; fi
しかし、これは間違っていて壊れています (以下を参照)。
より一般的なこと:の出力を解析しないでls
ください。現在のディレクトリで最初のファイル (またはディレクトリなど) を見つけたい場合は、グロブと次のメソッドを使用します。
shopt -s nullglob
files=( * )
# The array files contains the names of all the files (and directories...)
# in the current directory, sorted by name.
# The first one is given by the expansion of "${files[0]}". So:
if [[ "${files[0]}" = "arguprog.sh" ]]; then echo "TRUE"; else echo "FALSE"; fi
メソッドの解析ls
が間違っていることに注意してください。見て:
$ # Create a new scratch dir
$ mkdir myscratchdir
$ # Go in there
$ cd myscratchdir
$ # touch a few files:
$ touch $'arguprog.sh\nwith a newline' "some other file"
$ # I created 2 files, none of them is exactly arguprog.sh. Now look:
$ if [[ $(ls | head -1) = "arguprog.sh" ]]; then echo "TRUE"; else echo "FALSE"; fi
TRUE
$ # HORROR!
これにはねじれた回避策がありますが、実際には、私が示した方法が最善の方法です。
終わり!