2017-11-10 3 views
0
for file in /home/TEMP/filename.txt 
do 
if [ "$file" = /home/TEMP/filename.txt ] 
then 

echo "$file present" 
else 

echo "file not present" 
fi 

フォルダまたはないシェルスクリプトでファイルが存在しているかどうかをチェックするためにファイルが存在するかどうかを確認するにどのようにそのは、常に状態の場合に行くと「現在のファイル」を表示

+0

[シェルスクリプトにワイルドカードが含まれているかどうかを確認する](https://stackoverflow.com/questions/6363441/check-if-a-file-exists-with-wildcard-in-シェルスクリプト) –

+0

両方の文字列が一致する文字列比較を実行しています。あなたのことは、これは常に真実に評価されます.... – user1934428

答えて

0

を助けてください:

if [[ -e "$file" ]] 

あなたは何をしているのか、そしてあなたの期待される結果は何かについてもう少し説明できますか? /home/TEMP/filename.txtファイルの内容は何ですか?

0

すなわちifExists.sh、ファイルが存在する場合は、制御するためのファイルを作成し、以下のスクリプト

files=`ls ./` # put your path here like `ls /me/you/him/` 
for file in $files 
do 
    echo "$file" 
    if [ "$file" == "test2.sh" ];then #put the file name you want to compare here in place of test2.sh 
    echo "$file present" 
      #if you want to break the loop once file found just add statement `break` here 
    else 
    echo "file not present" 
fi 
done 
1

で試してみてください:呼び出し、その後、

if [ -f $1 ]; 
then 
echo $1 " is already present" 
else 
echo $1 " is not present" 
fi 

と:

$ . ifExists.sh your_file

かあなたのスクリプトの中で直接電話する:

dir = /home/TEMP; export dir 
    file = filename.txt; export file 

    if [ -f $dir/$file ]; 
    then 
    echo $dir/$file " is already present" 
    else 
    echo $dir/$file " is not present" 
    fi 
関連する問題