2017-11-06 10 views
0

シェルプログラムで[-s $ fname]を指定していますので、誰かに教えてください。私はそれを検索しましたが、 '-s'については何もありません。whatsはシェルの[if [-s filename] 'の意味ですか?

if [ -s $fname ] 
then 
    echo -e "\tname\tph\t\tcity\tpin\tstate" 
    cat $fname 
else 
    echo -e "\nfile is empty" 
fi 
;; 

これは私のコードの一部です。必要な場合には-sが使用されます。ありがとうございます!

+0

を活性化し、印刷してコードスニペットになることあなたが使用している多くの利用可能なシェルのどれに依存します。 –

+0

検索スキルを向上させる必要があります https://www.unix.com/unix-for-dummies-questions-and-answers/46660-explanation-if-filename.html これは、ファイルが存在し、 0バイトより – Alex

+2

とhttps://www.gnu.org/software/bash/manual/bash.html#Bash-Conditional-Expressions –

答えて

1

変数$fname内のファイルが存在し、空でない場合

-s $fname式がTrueに評価:-)あなたの質問自体に答えを持っています。 $fnameファイルが存在しないか、空であれば、その後、あなたのelseブロックはfile is empty

いくつかの例を、

fname="test.txt" 

## case when the file is not existing 
if [ -s $fname ] 
then 
    echo -e "case-1: $fname exists and is not empty" 
    cat $fname 
else 
    echo -e "case-1: $fname do not exists or is empty" 
fi 

## case when the file exists but is of empty size 
touch $fname 

if [ -s $fname ] 
then 
    echo -e "case-2: $fname exists and is not empty" 
    cat $fname 
else 
    echo -e "case-2: $fname do not exists or is empty" 
fi 

## case when the file exists and is having contents too 
echo "now its not empty anymore" > $fname 

if [ -s $fname ] 
then 
    echo -e "case-3: $fname exists and is not empty" 
    cat $fname 
else 
    echo -e "case-3: $fname do not exists or is empty" 
fi 

出力され、

case-1: test.txt do not exists or is empty 
case-2: test.txt do not exists or is empty 
case-3: test.txt exists and is not empty 
now its not empty anymore 
関連する問題