
if(-z "$file1" && "file2") {
print "file1 and file2 are empty";
} else {
print "execute";
}
これを作成すると、ファイルが空のときに印刷され、execute
ファイルが空でないときに印刷されます
file1 and file2 are empty
。
条件が真のときに印刷する必要があります。file1 and file2 are empty
そうですか?まだ間違っていますか?
答え1
-z
とがありません$
。-z "$file2"
また、ファイル名を引用する必要はありません(ただし、これはエラーを引き起こしません)。 Perl 1行の例を使用して、次のテストを実行します。
rm -rf foo bar
touch foo
perl -le 'my $file1 = "foo"; my $file2 = "bar"; if ( -z $file1 && -z $file2 ) { print "file1 and file2 are empty"; } else { print "execute"; }'
# File 'bar' does not exist, so -z $file2 evaluates to false:
# execute
rm -rf foo bar
touch foo bar
perl -le 'my $file1 = "foo"; my $file2 = "bar"; if ( -z $file1 && -z $file2 ) { print "file1 and file2 are empty"; } else { print "execute"; }'
# Both files exist and are zero size, so both '-z' tests evaluate to true:
# file1 and file2 are empty
rm -rf foo bar
touch foo bar
echo '1' > bar
perl -le 'my $file1 = "foo"; my $file2 = "bar"; if ( -z $file1 && -z $file2 ) { print "file1 and file2 are empty"; } else { print "execute"; }'
# File 'bar' iz non-zero size, so -z $file2 evaluates to false:
# execute
答え2
-z
あなたの明細書に1つがありませんif
。
if ( -z "$file1" && -z "$file2" ) {
print "file1 and file2 are empty";
}
else {
print "execute";
}