• Correct quoting through several levels of tools (shell, expect, ftp)

    From Janis Papanagnou@21:1/5 to All on Fri Jan 28 21:29:29 2022
    I have a shell script like the one depicted below as excerpt to invoke
    simple ftp-transfers; the script invokes expect, and expect spawns ftp.
    That script has issues with (today common) pathological file names.

    How would the correct quoting be done to pass the file with the below
    depicted filename through all these nested tools?


    server=...
    file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'
    ...
    expect <<EOT
    spawn ftp -i "${server}"
    ...
    expect "ftp> "
    send "put ${file}\r"
    ...
    EOT


    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Elvidge@21:1/5 to Janis Papanagnou on Fri Jan 28 22:52:16 2022
    On 28/01/2022 20:29, Janis Papanagnou wrote:
    I have a shell script like the one depicted below as excerpt to invoke
    simple ftp-transfers; the script invokes expect, and expect spawns ftp.
    That script has issues with (today common) pathological file names.

    How would the correct quoting be done to pass the file with the below depicted filename through all these nested tools?


    server=...
    file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'
    ...
    expect <<EOT
    spawn ftp -i "${server}"
    ...
    expect "ftp> "
    send "put ${file}\r"
    ...
    EOT


    Janis


    Sorry, please disregard suggestion (except / in filename)

    --
    Chris Elvidge
    England

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Elvidge@21:1/5 to Chris Elvidge on Fri Jan 28 22:41:48 2022
    On 28/01/2022 22:39, Chris Elvidge wrote:
    On 28/01/2022 20:29, Janis Papanagnou wrote:
    I have a shell script like the one depicted below as excerpt to invoke
    simple ftp-transfers; the script invokes expect, and expect spawns ftp.
    That script has issues with (today common) pathological file names.

    How would the correct quoting be done to pass the file with the below
    depicted filename through all these nested tools?


    server=...
    file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'
    ...
    expect <<EOT
    spawn ftp -i "${server}"
    ...
    expect "ftp> "
    send "put ${file}\r"
    ...
    EOT


    Janis


    $file contains an illegal character (/) perhaps?

    Why are you using expect?

    ftp -niv $HOST <<EOT
    $USER
    $PWD
    put $FILE
    quit

    seems to work.

    Sorry forgot the EOT after quit.


    --
    Chris Elvidge
    England

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Elvidge@21:1/5 to Janis Papanagnou on Fri Jan 28 22:39:08 2022
    On 28/01/2022 20:29, Janis Papanagnou wrote:
    I have a shell script like the one depicted below as excerpt to invoke
    simple ftp-transfers; the script invokes expect, and expect spawns ftp.
    That script has issues with (today common) pathological file names.

    How would the correct quoting be done to pass the file with the below depicted filename through all these nested tools?


    server=...
    file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'
    ...
    expect <<EOT
    spawn ftp -i "${server}"
    ...
    expect "ftp> "
    send "put ${file}\r"
    ...
    EOT


    Janis


    $file contains an illegal character (/) perhaps?

    Why are you using expect?

    ftp -niv $HOST <<EOT
    $USER
    $PWD
    put $FILE
    quit

    seems to work.

    --
    Chris Elvidge
    England

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Chris Elvidge on Sat Jan 29 09:32:42 2022
    On 28.01.2022 23:39, Chris Elvidge wrote:
    On 28/01/2022 20:29, Janis Papanagnou wrote:
    I have a shell script like the one depicted below as excerpt to invoke
    simple ftp-transfers; the script invokes expect, and expect spawns ftp.
    That script has issues with (today common) pathological file names.

    How would the correct quoting be done to pass the file with the below
    depicted filename through all these nested tools?


    server=...
    file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'
    ...
    expect <<EOT
    spawn ftp -i "${server}"
    ...
    expect "ftp> "
    send "put ${file}\r"
    ...
    EOT


    Janis


    $file contains an illegal character (/) perhaps?

    My fault; for posting purposes I made up a long file name and wanted
    to abbreviate "with" with "w/", missing that this would change the
    filename semantics. The actual filenames have blanks and parenthesis,
    but no '/', '\', '\\' or other Unix/DOS directory separators.


    Why are you using expect?

    When I wrote it ftp seemed to only support an interactive mode, so
    I had to implement the question/answer protocol; the quasi-standard
    tool for such tasks was expect.


    ftp -niv $HOST <<EOT
    $USER
    $PWD
    put $FILE
    quit

    seems to work.

    I haven't tried it but I saw your other post "Sorry, please disregard suggestion", and suppose you meant this one actually doesn't work?

    Nevermind, I inspected per "apropos ftp" available programs and found
    ncftpget and ncftpput. Yet I haven't tried them but they might serve
    the tasks I have. Only thing I don't see in the man page is that there
    seems to be no equivalent to mput/mget for the transfer of many files available. (I'll have to see.) Avoiding the multiple authentications
    with its delay in case of multiple file transfers would be desirable.

    Besides this (potential) alternative I'd nonetheless be curious about
    insights with regard of my original question; correct quoting through
    several levels of these tools (in case I'll need it at times, or have
    to resort to that approach).

    Thanks.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Sat Jan 29 12:48:20 2022
    In article <st1jn9$poc$[email protected]>,
    Janis Papanagnou <[email protected]> wrote:
    I have a shell script like the one depicted below as excerpt to invoke
    simple ftp-transfers; the script invokes expect, and expect spawns ftp.
    That script has issues with (today common) pathological file names.

    How would the correct quoting be done to pass the file with the below >depicted filename through all these nested tools?


    server=...
    file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'
    ...
    expect <<EOT
    spawn ftp -i "${server}"
    ...
    expect "ftp> "
    send "put ${file}\r"
    ...
    EOT

    There are basically 3 ways to "script" FTP transfers:

    1) Directly. By this, I mean by supplying the actual FTP commands on stdin and/or writing them to a file, then invoking FTP with some silly options to
    get it to run in script mode. Most of the answers so far on this thread
    have been in this category. I find this method hinky and try to avoid it
    if at all possible.

    2) Using Expect. This is the method I'm most comfortable with, and I
    encourage you to stay with it. I would do it more like this, though:

    #!/usr/bin/expect --
    spawn ftp [lindex $argv 0]
    ...
    expect "ftp> "
    send "put [lindex $argv 1]\r"
    ...

    Then I would call that script passing the server and the filename as
    command line args. Note that in all my years of Expect scripting, I never figured out a way to access the command line args (In Expect/TCL-speak,
    $argv) from a command line (-c 'Expect Script Here') Expect program. It
    only seems to work when the script is in a file. I am open to suggestions about how to do that.

    Note, BTW, that I *think* what you did originally would/should work if you
    had used "-c" to pass the Expect script on the command line, rather than feeding the script in via stdin.

    3) Using some other command line tool that is designed for automating FTP transfers. I've used ncftpget/ncftpput in the past; there are more
    current versions of the general concept now, like "curl" and "iftp" and so
    on. These all seem to work well, and I've nothing bad to say about them.

    Finally, note that the oft-given-advice at this point is to not use FTP
    since it is "insecure". The kewl people nowadays all use ssh/scp or some variant thereof. Any particular reason you aren't doing that too? Any
    reason you don't want all your friends to think you're kewl?

    --
    Faced with the choice between changing one's mind and proving that there is
    no need to do so, almost everyone gets busy on the proof.

    - John Kenneth Galbraith -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Eder@21:1/5 to Kenny McCormack on Sat Jan 29 14:09:45 2022
    On Sa 29 Jan 2022 at 12:48, [email protected] (Kenny McCormack) wrote:

    3) Using some other command line tool that is designed for automating FTP transfers. I've used ncftpget/ncftpput in the past; there are more
    current versions of the general concept now, like "curl" and "iftp" and so on. These all seem to work well, and I've nothing bad to say about them.

    There is also lftp which works quite well with scripts.

    'Andreas

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Sat Jan 29 14:07:13 2022
    In article <[email protected]>,
    Andreas Eder <[email protected]> wrote:
    On Sa 29 Jan 2022 at 12:48, [email protected] (Kenny McCormack) wrote:

    3) Using some other command line tool that is designed for automating FTP
    transfers. I've used ncftpget/ncftpput in the past; there are more
    current versions of the general concept now, like "curl" and "iftp" and so >> on. These all seem to work well, and I've nothing bad to say about them.

    There is also lftp which works quite well with scripts.

    I think I meant to say 'lftp' when I said 'iftp'.

    --
    "I think I understand delicate, but why do I have to wash my hands, and
    be standing in cold water when doing it?"

    Kaz Kylheku <[email protected]> in comp.lang.c

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ed Morton@21:1/5 to Janis Papanagnou on Sat Jan 29 07:19:14 2022
    On 1/28/2022 2:29 PM, Janis Papanagnou wrote:
    I have a shell script like the one depicted below as excerpt to invoke
    simple ftp-transfers; the script invokes expect, and expect spawns ftp.
    That script has issues with (today common) pathological file names.

    How would the correct quoting be done to pass the file with the below depicted filename through all these nested tools?


    server=...
    file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'

    Does updating it with printf '%q` get you to where you need to be:

    $ file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'
    $ printf -v file '%q' "$file"
    $ echo "$file"
    A\ \(pathological\)\ file\ name\ w/\ blanks\,\ parenthesis\,\ etc.pdf

    Ed.


    ...
    expect <<EOT
    spawn ftp -i "${server}"
    ...
    expect "ftp> "
    send "put ${file}\r"
    ...
    EOT


    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to [email protected] on Sun Jan 30 03:49:12 2022
    In article <st4biv$7af$[email protected]>,
    Janis Papanagnou <[email protected]> wrote:
    ...
    $ file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'
    $ printf -v file '%q' "$file"

    Unlike bash's, my (I.e., ksh's) printf has no -v, just BTW.

    Isn't -v just a shorthand for:

    var=$(printf ...)

    ?

    So, you would just use that instead.

    What about %q? Does ksh have that? That also might be bash-specific -
    but I don't know for sure...

    --
    The coronavirus is the first thing, in his 74 pathetic years of existence,
    that the orange menace has come into contact with, that he couldn't browbeat, bully, bullshit, bribe, sue, legally harrass, get Daddy to fix, get his siblings to bail him out of, or, if all else fails, simply wish it away.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Kenny McCormack on Sun Jan 30 11:13:55 2022
    On 30.01.2022 04:49, Kenny McCormack wrote:
    In article <st4biv$7af$[email protected]>,
    Janis Papanagnou <[email protected]> wrote:
    ...
    $ file='A (pathological) file name w/ blanks, parenthesis, etc.pdf'
    $ printf -v file '%q' "$file"

    Unlike bash's, my (I.e., ksh's) printf has no -v, just BTW.

    Isn't -v just a shorthand for:

    var=$(printf ...)

    Effectively (maybe) yes, but I wouldn't bet on it, since the
    subshell context or command composition may have side effects
    that direct variable setting wouldn't have. (No coffee yet,
    so I'm doing hard at the moment to think about samples, but I
    think it's not bad to have a simple -v assignment available.)
    Given that 'printf' is a builtin and $(...) of that builtin
    will [in ksh] not create an extra process it's also not slow.


    ?

    So, you would just use that instead.

    That's how we can do it with shells not supporting printf -v.
    (The wrapper I used is effectively also just a printf, doing
    var=$(quote ...), but consistently for all arguments not only
    for those that need it in shell contexts.)


    What about %q? Does ksh have that? That also might be bash-specific -
    but I don't know for sure...

    The %q is also in Ksh's printf. Beyond the ANSI specifiers, Ksh
    supports %b %B %H %P %R %T %Z and %({csv|html|ere|pattern|url})q .
    A couple of these are quite noteworthy - I don't know what bash
    supports -, e.g. pattern conversions, CSV, HTML, URL conversions,
    time formatting. There's also format modifiers to consider width
    for characters instead of bytes/octets, and new ANSI escapes for
    Unicode characters, scaling factors (1000 and 1024 based), digit
    grouping, and a lot more.

    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)