简体   繁体   中英

Exit from lftp after local shell command execution

I need to quit the lftp automatically after some local shell commands are executed. Eg I need to find some files and exit.

lftp -e "!find . -maxdepth 3 -name \"index.*\" -type f;exit" sftp://user:pass@mysite.com:22

When this command is executed, it keeps me inside the lftp environment so I need to send extra "bye" command to leave the app. But I need to perform it automatically upon shell command execution.

I tried

lftp -e "!find . -maxdepth 3 -name \"index.*\" -type f;exit;bye" sftp://user:pass@mysite.com:22

but it doesn't work (seems "bye" is executed in local shell context rather than lftp shell).

It there any chance to exit from local shell mode back to lftp command mode and then perform "bye" within the same session?

Note that what you're trying won't have a useful effect -- the local shell is local to where you're running lftp , so you're running find on the same machine as the client, not the server. There's thus no reason to run find inside lftp as opposed to outside of it.

Getting past that, though, and answering the literal question -- you can split your commands across multiple lines; $'\\n' is a literal for a newline, or newlines can be literally added to a single-line string. Thus:

lftp -c '
open sftp://user:pass@mysite.com:22
!find . -maxdepth 3 -name "index.*" -type f
' </dev/null

There's no need for the exit or bye as using -c rather than -e causes the connection to be closed and lftp to automatically exit after all commands are run. Using </dev/null also ensures that even if you did use -e , attempts to read further commands from stdin would return an EOF (and thus likewise indicate an exit).

I've also observed that, somehow, after executing a local command, lftp will run a local version of the next command, even if for that second command 'local' was not specified. Normally this reverts back to sending commands to the remote site the third time around, however, when at times I walk away from the terminal and come back and issue a third command much later, the new command and all subsequent ones will also apply to local, like if the connection had been lost -or like it never existed- and in this situation unless I reconnect to the site a command such as 'bye' is just not possible.

What I do to work around this is to define a bookmark early on in the connection process that I can reuse later and make sure is open prior to issuing 'bye' - which as you said, should close the connection / the process / the application and/or window.

So initially, issue something like 'bookmark save remote'. And just prior to leaving, issue something like 'open remote' followed by 'bye', and that should work.

NB: Give your bookmarks unique names instead of 'remote' if you wish to connect to multiple servers and plan to do concurrent work, as all sessions will most likely share the same set of lftp bookmarks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM