2

I have a script to clear the some files from a host(Host_1). In another host(Host_2), I am doing an ssh to the script in Host_1.

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
    return 1
fi

In Host_2, I am doing an ssh to Host_1.

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

It returns blank here. No output. I tried to see if any output was coming at all to Host_2 by just doing an

echo $a

This returned me blank. I am not sure what I missed here. Please suggest to get the memory space as well from a single ssh command.

0
1

The return statement is used to set an exit code; it is not used as output for a variable assignment. If you want to capture a string as output then you're probably going to need to write it to standard output. A quick fix would be the following modification of your script:

#!/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
2
  • 1
    A filesystem could (in theory) have 1 available (or even a negative number) so echo nofolder or something might better distinguish echo 1 from what df might return. – thrig Nov 1 '17 at 18:16
  • @thrig That's a good point. There are probably a few improvements that could be made here. I'm going to elaborate a little bit, but I'm debating how much to include without going to far afield. – igal Nov 1 '17 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.