Raspberry Pi에서 컴퓨터로 - 스크립트로 작성된 파일 전송 및 제거

Raspberry Pi에서 컴퓨터로 - 스크립트로 작성된 파일 전송 및 제거

이것은 내 첫 번째 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.

관련 정보