简体   繁体   English

将ftp命令输出存储在变量中

[英]store ftp command output in a variable

I am using bash a script to connect to an FTP server for deleting a file. 我正在使用bash脚本连接到FTP服务器以删除文件。 I would like to store the output message and code of the delete command executed on the FTP server into a variable of my script. 我想将在FTP服务器上执行的输出消息和delete命令的代码存储到脚本的变量中。

How could I do this ? 我该怎么办?

Here is my snippet : 这是我的片段:

...
function delete_on_ftp{
  ftp -i -n $ftp_host $ftp_port <<EOF
quote USER $ftp_login
quote PASS $ftp_pass
delete $1
quit
EOF
}
output_cmd=$(delete_on_ftp $myfile)
...

By the way I do above I only get the message, no way to get the returned code. 通过上面的方式,我只会收到消息,而无法获取返回的代码。 Is there another way allowing to get the code and the message, in 1 or 2 variables ? 是否有另一种方式可以通过1或2个变量获取代码和消息?

Thanks, Cheers 谢谢,干杯

I just tested the following curl command, which make your task easy. 我刚刚测试了以下curl命令,这使您的任务变得容易。

curl --ftp-ssl -vX "DELE oldfile.pdf" ftp://$user:$pass@$server/public_html/downloads/

Please do not forget the slash at the end of your directory, it is necessary. 请不要忘记目录末尾的斜杠,这是必要的。

curl: (19) RETR response: 550

550 oldfile.pdf: No such file or directory 550 oldfile.pdf:没有这样的文件或目录

curl: (19) RETR response: 250

250 DELE command successful 250 DELE命令成功

curl is available at http://curl.haxx.se/ . curl可从http://curl.haxx.se/获得

Other answers on this question should provide you what you want. 关于这个问题的其他答案应该可以为您提供所需的信息。

However, if you are keen on specifically using ftp command, you can use expect command for the same... 但是,如果您热衷于使用ftp命令,则可以将expect命令用于相同的...
Note, that this is not the best way to achieve what you are trying. 请注意,这不是实现您正在尝试的最佳方法。

expect -c "log_user 0;
           spawn ftp -i -n $ftp_host $ftp_port;
           expect \"<add ftp login username: prompt details here>\"
           send \"quote USER $ftp_login\r\n\"
           expect \"<add ftp login password: prompt details here>\"
           send \"quote PASS $ftp_pass\r\n\"
           expect \"<add ftp shell prompt details here>\"
           log_user 1; send \"delete $1\r\n\"
           log_user 0;
           expect \"<add ftp shell prompt details here>\"
           send \"quit\r\n\";
           interact"

You may need to add some more lines in the above for the login & shell prompts returned by the ftp command. 您可能需要在上面为ftp命令返回的登录名和shell提示添加更多行。

One of the ways to get FTP to act automatically is to use a Netrc file. 使FTP自动执行操作的一种方法是使用Netrc文件。 By default, FTP will use $HOME/.netrc , but you can override that via the -N parameter. 默认情况下,FTP将使用$HOME/.netrc ,但是您可以通过-N参数覆盖它。 The format of a netrc file is fairly straight forward. netrc文件的格式非常简单。 A line is either a Macrodef or a line that contains login information. 一行是Macrodef或包含登录信息的行。 Here's an example below: 下面是一个示例:

Netrc File Netrc文件

mysystem login renard password swordfish
another login renard password 123456
default login renard password foofighter

macdef init
binary
cd foo
get bar
delete bar
quit

macdef fubar
...

The three first lines are the logins for various systems. 前三行是各种系统的登录名。 The default is a login for any system which you don't define a particular login for. default值为您未为其定义特定登录名的任何系统的登录名。 The lines that start with marcodef are macros you define to do a series of steps for you. marcodef开头的行是您定义的宏,可以为您执行一系列步骤。 The init macro automatically runs right after login. init宏会在登录后立即自动运行。 If the last line is quit , it will quit out of FTP for you. 如果最后一行quit ,它将为您退出FTP。 There should be a blank line to end the macro, (although most systems will take an End of the File as the end of the macrodef too). 结束宏应该有一个空行(尽管大多数系统也将文件的结尾也作为宏定义的结尾)。

You can create a Netrc file on the fly, enter your FTP command in that, and then, run your FTP command with that Netrc file: 您可以动态创建一个Netrc文件,在其中输入FTP命令,然后使用该Netrc文件运行FTP命令:

cat > $netrc_file <<<EOF
$ftp_host login $ftp_login password $ftp_password
macdef init
delete $my_file
quit
EOF

ftp -N $netrc_file

You can capture the output via STDOUT, or in a variable and then parse that for what you need: 您可以通过STDOUT或在变量中捕获输出,然后根据需要解析输出:

ftp -N $netrc_file | tee $ftp_output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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