サブディレクトリにカレントディレクトリから空のファイルを移動します

drew197:

それはサブディレクトリを作成しますdosent場合、私はサブディレクトリ「Empty_Files」は、現在のディレクトリに存在するかどうかを確認しますbashスクリプトを作成しようとしています。これは、現在のディレクトリとファイルは、ファイルを移動したい場合は、それが要求されます空の場合、すべての定期的な非隠されたファイルをチェックします。ユーザーがそう言うなら、それはEmpty_Filesディレクトリにファイルを移動します。私は、スクリプトを実行したときしかし、それだけで、現在のディレクトリに空のファイルを言いませんが、私は、ファイルを移動したい場合は、まだ要求します。わからないなぜそれがこれをやっています。すべてのヘルプはapperitacedされるだろう。

   #!/bin/bash

#Script to move empty files from current directory into the sub directory Empty_Files

# usage:  ./move_empty


subdirectory="Empty_Files"


if [ -f $subdirectory ]  # does the Empty_Files file exist?
then
   echo $subdirectory "exists!"
else
   mkdir -p /home/student/Empty_Files
   echo "Empty_Files subdirectory created"
fi

currentfiles=$( ls . )  # check all non hidden files in current directory

for eachfile in $currentfiles
do
   checksize=$(du -sh $eachfile | awk '{print $1}')

   if [ "$checksize" = "0" ] # check if any files are empty
   then
      echo -n "Would you like to move the file Y/N:" # if a file is empty ask the user if the want to move the file
      read useranswer
   fi

   if [ "$useranswer" = "y" ] || [ "$useranswer" = "Y" ]
   then
      mv "$eachfile" /home/student/Empty_Files
      echo "mv command successful"
   elif [ "$useranswer" = "n" ] || [ "$useranswer" = "N" ]
   then
      echo "File will not be moved"
   fi

   if [ ! -z "$currentfiles" ]
   then
      echo "no empty files found in the current directory"
      #exit 55
   fi
done
Barmar:

あなたは、いくつかの問題を抱えています。

ファイルが空でない場合、あなたは彼らがファイルを移動するかどうかをユーザーに確認コードをスキップし、あなたはまだ、ファイルを移動し、コードを実行します。これは、の値を使用して$useranswer、それが次の空のファイルに到達するまで、それは、空のファイルを移動した後、それはすべての非空のファイルを移動しますので、以前のファイルから。動きは内部でなければならないコードifの長さをテストしています。

印刷しないようにするかどうかについての試験だけで間違っている「空のファイルが見つかりました」。$currentfilesすべてのファイルではなく、空のファイルのリストです。そして、あなたのテストは後方です:変数が空でない場合、あなたがテストしています。あなたは空のファイルを見つけたときに何をすべきことは、変数を設定されています。ループが完了した後、あなたは、その変数を確認することができます。

内蔵されたファイルが非ゼロの大きさを持っているかどうかについての試験、あなたが使用する必要はありませんありますdu。このためには。

あなたはの出力を解析するべきではありませんls、ワイルドカードを使用し、。

あなたは移動が成功したというメッセージを印刷するつもりなら、あなたは実際にそれがあったことを確認する必要があります。

彼らは、ファイルを移動するかどうかを尋ねる質問には、それがどのファイル言いません。

emptyfound=n
for eachfile in *
do
    if [ ! -s "$eachfile" ] # check if any files are empty
    then
        emptyfound=y
        echo -n "Would you like to move the file $eachfile Y/N:" # if a file is empty ask the user if the want to move the file
        read useranswer

        if [ "$useranswer" = "y" ] || [ "$useranswer" = "Y" ]
        then
            if mv "$eachfile" /home/student/Empty_Files
            then echo "mv command successful"
            else echo "mv command failed"
            fi
        else
            echo "File will not be moved"
        fi
    fi
done

if [ "$emptyfound" = n ]
then
    echo "no empty files found in the current directory"
    #exit 55
fi

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=13338&siteId=1