2017-08-31 1 views
0

これは私の最初のスクリプトですが、私はLinuxを使用していますが、私は最後に助けを求めるまで私の脳を揺らしているスクリプトが必要でした。期待どおりに新しいディレクトリが追加されているかどうかを確認するために、すでに存在するディレクトリを持つディレクトリを確認する必要があります。ディレクトリ下のサブディレクトリのbashチェック


私はこれをできるだけシンプルにしていると思います。以下は動作しますが、ディレクトリ内のすべてのファイルも表示されます。 誰かが私にファイルをリストする方法を教えてくれない限り、私はそれを続けます |私はls -dを試しましたが、エコー "何も新しい"をやっています。私は馬鹿のように感じて、早くこれを持っているはずです。

#!/bin/bash 

workingdirs=`ls ~/ | grep -viE "temp1|temp2|temp3"` 

if [ -d "$workingdirs" ] 
then 
echo "nothing new" 

else 

echo "The following Direcetories are now present" 
echo "" 
echo "$workingdirs" 

fi 
+1

何が問題なのですか? –

+0

findコマンドを使用します。 "find〜/ -type d | grep ..."のようなもの –

答えて

0

新しいディレクトリが作成されたときに何らかのアクションを実行する場合は、inotifywaitを使用します。現在またはないディレクトリが表示されますシェルスクリプトの下

trap 'rm -f $TMPDIR/manifest' 0 

# Create the expected values. Really, you should hand edit 
# the manifest, but this is just for demonstration. 
find "$Workingdir" -maxdepth 1 -type d > $TMPDIR/manifest 

while true; do 
    sleep 60 # Check every 60 seconds. Modify period as needed, or 
      # (recommended) use inotifywait 
    if ! find "$Workingdir" -maxdepth 1 -type d | cmp - $TMPDIR/manifest; then 
     : Unexpected directories exist or have been removed 
    fi 
done 
0

:あなただけ存在するディレクトリは、あなたが期待するものであることを確認したい場合は、あなたのような何かを行うことができます。

#!/bin/bash 

Workingdir=/root/working/ 
knowndir1=/root/working/temp1 
knowndir2=/root/working/temp2 
knowndir3=/root/working/temp3 
my=/home/learning/perl 

arr=($Workingdir $knowndir1 $knowndir2 $knowndir3 $my) #creating an array 

for i in ${arr[@]}  #checking for each element in array 
do 
     if [ -d $i ] 
     then 
     echo "directory $i present" 
     else 
     echo "directory $i not present" 
     fi 
done 

出力:

directory /root/working/ not present 
directory /root/working/temp1 not present 
directory /root/working/temp2 not present 
directory /root/working/temp3 not present 
**directory /home/learning/perl present** 
+0

予想されるディレクトリが存在するかどうかを知る必要はありません。だから私はtemm1 temp2と私が知っているtemp 3があると言う。 unknowndir1と表示された場合は、そのことを知る必要があります。それでおしまい。私は可能な限り簡単にするように努力しています、助けてくれてありがとう – decini

0

このファイルにリスト内の使用可能なディレクトリに保存されます。 2回目にスクリプトを実行すると、削除または追加されたディレクトリが報告されます。

#!/bin/sh 

dirlist="$HOME/dirlist" # dir list file for saving state between runs 
topdir='/some/path'  # the directory you want to keep track of 

tmpfile=$(mktemp) 

find "$topdir" -type d -print | sort -o "$tmpfile" 

if [ -f "$dirlist" ] && ! cmp -s "$dirlist" "$tmpfile"; then 
    echo 'Directories added:' 
    comm -1 -3 "$dirlist" "$tmpfile" 

    echo 'Directories removed:' 
    comm -2 -3 "$dirlist" "$tmpfile" 
else 
    echo 'No changes' 
fi 

mv "$tmpfile" "$dirlist" 

非常にエキゾチックな名前(改行を含む)のディレクトリには、スクリプトに問題があります。

関連する問題