1

外部ドライブへのバックアップ用の BASH スクリプトを作成しようとしています。このスクリプトを実行して、接続されているドライブを確認してから、rsync を実行します。私はBASHが初めてで、それがif then elseであるとはよくわかりません。誰でも助けてもらえますか?私が考えていたのは、ということでした。

If [ /Volumes/Drive_1]
 then
     sudo rsync -avx /Volumes/1/2\ __3__/ /Volumes/Drive_1
 else
     If [ /Volumes/Drive_2]
 then
     sudo rsync -avx /Volumes/1/2\ __3__/ /Volumes/Drive_2
 fi
4

2 に答える 2

3

まず、それifは ではありませんIf(大文字と小文字が区別されます)。ドライブが存在するかどうかを確認するには、指定された文字列がディレクトリの名前であるかどうかを確認する必要があるため、-dプライマリが必要になります。

 if [ -d /Volumes/Drive_1 ]
 then
     sudo rsync -avx /Volumes/1/2\ __3__/ /Volumes/Drive_1
 elif [ -d /Volumes/Drive_2 ]
 then
     sudo rsync -avx /Volumes/1/2\ __3__/ /Volumes/Drive_2
 fi

[との間のコードから分離するスペース]が必要です。

于 2013-05-20T17:36:45.323 に答える
2

複数のパスでまったく同じことを行うには、Variable と For を使用するのが最も簡単です。

例えば

# Stores all Paths to BackupPaths
BackupPaths=( "/Path/to/First" "/Path/to/second" .... )

# Iterates for each Path and stores current in volume
# $volume allows for accessing the content of the variable
for volume in "${BackupPaths[@]}"; do
    if [ -d "$volume" ]
    then
        sudo rsync -avx /Volumes/1/2\ __3__/ "$volume"
    fi
done
于 2013-05-20T17:38:26.320 に答える