これは私の最初のBashスクリプトであり、ほとんどの答えを直接見つけようと努力しましたが、ついに障害物にぶつかりました。
スクリプトは(ほとんど)動作しているようですが、iMac側ではファイルを受け取りません。
ここでのアイデアは、自動的にファイルをプライマリコンピュータに転送し、RPiシステムのディレクトリ/ファイルを整理してスペースを節約するための専用RPiトレントボックスを持つことです。
スクリプトは、SCP実行機能に影響を与えないスペースなどを含む、投げられたすべてのディレクトリおよび/またはファイルを処理するようです。
私のエラーを見つけるために構文を確認するには、Bashスクリプトの経験を持つ人が必要です。ここに私の完全なスクリプトがあります。効率性を向上させるためのどんな提案でも大変感謝いたします。
これまでに使用されていた修正で更新されました。
問題の範囲を絞り込む。私はselect_target_directory
この機能を正しくtarget_directory_selected=
実行していますか?この変数がいっぱいになっているかどうかはわかりません。
#!/bin/bash
# variables declared
directory_on_localhost="/mnt/32gb_pny_usbdrive/completed/*"
directory_on_remote_host_primary="/Volumes/Drobo/zIncoming"
directory_on_remote_host_secondary="/Users/josh/Desktop/zIncoming"
target_directory_selected=""
# functions defined
# This function basically verifies the Drobo is mounted on the iMac.
select_target_directory () {
if [ 'ssh [email protected] test -d /Volumes/Drobo/zIncoming' ]
then
target_directory_selected="$directory_on_remote_host_primary"
else
target_directory_selected="$directory_on_remote_host_secondary"
fi
}
# This function copies target <directories/files> to the target directory (via scp)
# and then deletes them from the local machine to conserve valuable storage space.
process_the_files () {
for current_target in $directory_on_localhost
do
scp -r "$current_target" [email protected]:"$target_directory_selected"
rm -rf "$current_target"
done
}
# main logic begins
# [Tests "$directory_host" for contents] && [iMac status (i.e. powered off or on)]
# IF "$directory_host" is not empty AND iMac is powered on THEN functions are invoked
# And Main Logic is completed and script ends, ELSE script ends.
if [ "$(ls -A $directory_on_localhost)" ] && [ 'nc -z 10.0.1.2 22 > /dev/null' ]
then
select_target_directory
process_the_files
else
exit
fi
# main logic ends
ベストアンサー1
代わりに:
if [ 'ssh [email protected] test -d /Volumes/Drobo/zIncoming' ] then target_directory_selected="$directory_on_remote_host_primary" else target_directory_selected="$directory_on_remote_host_secondary" fi
意味するものは次のとおりです。
if ssh [email protected] test -d /Volumes/Drobo/zIncoming
then
target_directory_selected="$directory_on_remote_host_primary"
else
target_directory_selected="$directory_on_remote_host_secondary"
fi
これは[ 'non-empty string' ]
常に本当です。ssh
私が書き直した方法のように、コマンドに条件を使用したい場合があります。
[ 'nc -z 10.0.1.2 22 > /dev/null' ]
同様に、後でスクリプトで置き換えることもできますnc -z 10.0.1.2 22 > /dev/null
。