SSH 명령에서 값 가져오기

SSH 명령에서 값 가져오기

호스트(Host_1)에서 일부 파일을 지우는 스크립트가 있습니다. 다른 호스트(Host_2)에서 Host_1의 스크립트로 SSH 연결 중입니다.

Host_1의 스크립트:

if [ condition here ]
then
    rm -r /folder #command to remove the files here
    b=$(df -k /folder_name| awk '{print $4}' | tail -1) #get memory after clearing files.
    echo "$b"
else
    return 1
fi

Host_2에서 Host_1로 SSH 연결 중입니다.

mail_func()
{
val=$1
host=$2
if [ $val -ne 1 ]
        then
        echo "$host $val%" >> /folder/hostnames1.txt #writing host and memory to text file
else
        exit
fi
}
a=$(ssh -q Host_1 "/folder/deletefile.sh")
mail_func a Host_1

여기에는 공백이 반환됩니다. 출력이 없습니다. 다음을 수행하여 Host_2의 출력이 있는지 확인하려고 했습니다.

echo $a

이로 인해 나는 공백으로 남았습니다. 내가 여기서 무엇을 놓치고 있는지 잘 모르겠습니다. 단일 SSH 명령에서도 메모리 공간을 확보하도록 제안하십시오.

답변1

return명령문은 종료 코드를 설정하는 데 사용되며 변수 할당을 위한 출력으로 사용되지 않습니다. 문자열을 출력으로 캡처하려면 해당 문자열을 표준 출력에 써야 할 수도 있습니다. 빠른 수정은 스크립트를 다음과 같이 수정하는 것입니다.

#!/bin/bash

#    Script in Host_1

if [ condition here ]
then
    rm -r /folder #command to remove the files here
    b=$(df -k /folder_name| awk '{print $4}' | tail -1) #get memory after clearing files.
    echo "$b"
else
    # NOTE:
    #     This return statement sets the exit-code variable: `$?`
    #     It does not return the value in the usual sense.
    # return 1

    # Write the value to stdout (standard output),
    # so it can be captured and assigned to a variable.
    echo 1
fi

관련 정보