Page 1 of 1

heredocs ftp and variables

Posted: Mon 06 Jul 2020, 15:58
by Eesmets
I have a yad list gui which receives output from the ftpdo function. I want to send commands to the ftpdo function from a another yad gui. The problem is my heredocs EOF statement does not expand the variables from the yad gui.
What's wrong with it?

Code: Select all

function ftpdo (){
ftp -inv "$1" "$2" <<"EOF" | while read line; do svgnum $line; export cnt=$((cnt + 1)); done > /tmp/listpipe
user $3 $4
$5
$6
ls
EOF
}
export -f ftpdo

function ftpfn (){
HOST="$(cat /tmp/ftp | cut -f1 -d'|')"
PORT="$(cat /tmp/ftp | cut -f2 -d'|')"
USER="$(cat /tmp/ftp | cut -f3 -d'|')"
PASSWORD="$(cat /tmp/ftp | cut -f4 -d'|')"
EXEC1="$("$1")"
EXEC2="$($2)"	

[ ! -f /tmp/FTP ] && yad  \
--form \
--field="HOST" "$HOST" \
--field="Port" "$PORT" \
--field="USER:H" "$USER" \
--field="PASSWORD:H" "$PASSWORD" \
--field="EXEC1" "$EXEC1" \
--field="EXEC2" "$EXEC2" \
--field="Do:FBTN" "bash -c \"ftpdo "%1" "%2" "%3" "%4" "%5" "%6" \"" \
--columns=2 > /tmp/ftp
touch /tmp/FTP
case $? in
0) rm -f /tmp/FTP;;
1) rm -f /tmp/FTP
exit;;
esac
}
export -f ftpfn

Re: heredocs ftp and variables

Posted: Tue 07 Jul 2020, 00:59
by phat7
Eesmets wrote:Subject description: SOLVED!
:?:

Posted: Tue 07 Jul 2020, 05:06
by Eesmets
Shortly after posting I found a solution. I had to double quote the variaables within the EOF statement. But it's not perfect .... spaces are treated as seperate arguments when reaching the function.

Posted: Tue 07 Jul 2020, 12:13
by misko_2083
Eesmets wrote:Shortly after posting I found a solution. I had to double quote the variaables within the EOF statement. But it's not perfect .... spaces are treated as seperate arguments when reaching the function.
Send the fields as a file to the function.

Code: Select all

function ftpdo (){
local field _HOST _PORT _USER _PASS _EXEC1 _EXEC2
for field in _HOST _PORT _USER _PASS _EXEC1 _EXEC2
     do
        read -s $field
done
ftp -inv "$_HOST" "$_PORT" <<"EOF" | while read line; do svgnum $line; export cnt=$((cnt + 1)); done > /tmp/listpipe
user $_USER $_PASS
$_EXEC1
$_EXEC2
ls
EOF
}
export -f ftpdo

function ftpfn (){
HOST="$(cut -f1 -d'|' /tmp/ftp)"
PORT="$(cut -f2 -d'|' /tmp/ftp)"
USER="$(cut -f3 -d'|' /tmp/ftp)"
PASSWORD="$(cut -f4 -d'|' /tmp/ftp)"
EXEC1="$("$1")"
EXEC2="$($2)"   

[ ! -f /tmp/FTP ] && yad  \
--form \
--field="HOST" "$HOST" \
--field="Port" "$PORT" \
--field="USER:H" "$USER" \
--field="PASSWORD:H" "$PASSWORD" \
--field="EXEC1" "$EXEC1" \
--field="EXEC2" "$EXEC2" \
--field="Do:FBTN" \
'bash -c "ftpdo <<EOF
"%1"
"%2"
"%3"
"%4"
"%5"
"%6"
EOF
" ' \
--columns=2 > /tmp/ftp
touch /tmp/FTP
case $? in
0) rm -f /tmp/FTP;;
1) rm -f /tmp/FTP
exit;;
esac
}
export -f ftpfn
edit I think I finaly got the right quoting

Posted: Tue 07 Jul 2020, 16:59
by Eesmets
Hi misko_2083

I tried your code, which at first didn't seem to work, finally unquoting "EOF" > EOF did work. So now sending the command works as desired. However a new problem arose with the output not showing correctly in the column list, but using tee I was able to capture the output to a file, which appears as expected,

Code: Select all

function ftpdo (){
local field _HOST _PORT _USER _PASS _EXEC1 _EXEC2 _EXEC3 _EXEC4
for field in _HOST _PORT _USER _PASS _EXEC1 _EXEC2 _EXEC3 _EXEC4
     do
        read -s $field
done
ftp -inv "$_HOST" "$_PORT" <<EOF | tee $track/stuff | while read line; do svgnum $line; export cnt=$((cnt + 1)); done
user $_USER $_PASS
$_EXEC1
$_EXEC2
$_EXEC3
$_EXEC4
EOF
}
export -f ftpdo

Code: Select all

ls DCIM/Camera

(tee redirected output)

Connected to 192.168.43.1.
220 Service ready for new user.
331 User name okay, need password for marcos.
230 User logged in, proceed.
200 Command PORT okay.
150 File status okay; about to open data connection.
drwx------   3 user group            0 Jul  4 08:57 cache
-rw-------   1 user group      5170040 Apr 19  2019 IMG_20190419_181408.jpg
-rw-------   1 user group      5115968 Apr 19  2019 IMG_20190419_181506.jpg
-rw-------   1 user group      5279320 Apr 19  2019 IMG_20190419_181507.jpg
-rw-------   1 user group      5584121 Apr 19  2019 IMG_20190419_181521.jpg
-rw-------   1 user group      7895737 Apr 19  2019 IMG_20190419_183633.jpg
-rw-------   1 user group      9781535 Apr 19  2019 IMG_20190419_183640.jpg
-rw-------   1 user group       139264 Jul  6 13:06 typescript
226 Closing data connection.
221 Goodbye.

EDIT: there are missing quotes in my code @ svgnum "$line" ... now everything works great!

Thanks again
Eesmets

Posted: Tue 07 Jul 2020, 17:08
by misko_2083
Nice.
Remember to add -r to read for backslash.

I didn't try with ftp.
This is the script I used to test:

Code: Select all

#!/bin/bash

function ftpdo (){

local _HOST _PORT _ABUSER _PASS _EXEC1 _EXEC2 FIELD

for FIELD in _HOST _PORT _USER _PASS _EXEC1 _EXEC2
     do
       read -s -r ${FIELD}
       echo "${FIELD}=${!FIELD}"
done

}
export -f ftpdo 

function ftpfn (){  
yad  \
--form \
--field="HOST" "$HOST" \
--field="Port" "$PORT" \
--field="USER:H" "$_USER" \
--field="PASSWORD:H" "$PASSWORD" \
--field="EXEC1" "$EXEC1" \
--field="EXEC2" "$EXEC2" \
--field="Do:FBTN" \
'bash -c "ftpdo <<EOF | yad --text-info --timeout=3
"%1"
"%2"
"%3"
"%4"
"%5"
"%6"
EOF
" ' \
--columns=2 

} 

ftpfn