bashlftp

how to store the values to a variable


I'm try to get the used storage for a ftp server through lftp.

lftp :~> open username:password@IP
lftp username@IP:~> du
897146  ./volume(sda1)
897146  .

I want to get the value of 897146 from a sh script.

This is what I got so far:

#!/bin/bash

FTP_PASS=password
FTP_HOST=IP
FTP_USER=username
LFTP=lftp


lftp  << EOF
open ${FTP_USER}:${FTP_PASS}@${FTP_HOST}
FOO="$(du)"
quit
EOF
echo "$FOO"

But I'm getting

Unknown command `FOO=9544       ./logs'.
Unknown command `9636'.

Solution

  • The du command inside the FTP session will output within the lftp command output. So to get the output of the du command, you need to capture the output of the lftp command inside your variable:

    #!/usr/bin/env bash
    FTP_PASS=password
    FTP_HOST=IP
    FTP_USER=username
    
    
    FOO=$(lftp  << EOF | filter_out_things_unrelated_to_du
    open ${FTP_USER}:${FTP_PASS}@${FTP_HOST}
    du
    quit
    EOF
    )
    echo "$FOO"
    

    You will probably need to filter-out the FTP session header and MOTD from the remote FTP server and anything not related with the output of du.