简体   繁体   English

将 python 脚本 output 存储到 linux 中的变量并运行 wget 命令

[英]Storing a python script output to a variable in linux and run a wget command

So I made a python script that retrieves a url link and returns it as an output. Just to keep it simple, the content of the python script would be just:所以我制作了一个 python 脚本来检索 url 链接并将其作为 output 返回。为了简单起见,python 脚本的内容将只是:

print("https://www.testweb.com/file=ejfeafjaiaefjaof")

For example, if i execute the python script in my terminal:例如,如果我在终端中执行 python 脚本:

python retrieve_url.py

it outputs:它输出:

https://www.testweb.com/file=ejfeafjaiaefjaof

So I then would like to utilize the "wget" command in linux with the url that is returned by my python script, so that i could do something like:所以我想利用 linux 中的“wget”命令和我的 python 脚本返回的 url,这样我就可以做类似的事情:

wget https://www.testweb.com/file=ejfeafjaiaefjaof

But instead, is it possible to store the python output url in a variable and execute a linux script in one go?但是,是否可以将 python output url 存储在一个变量中,并在一个 go 中执行 linux 脚本? I was thinking something like creating an executable script, run.sh我在想创建一个可执行脚本, run.sh

python retrieve_url.py
wget **some argument to store the python output**

and just use./run.sh.并使用./run.sh。

To save the output of your python script like this像这样保存 python 脚本的 output

test=$(python test.py)

echo $test
"https://www.testweb.com/file=ejfeafjaiaefjaof"

wget just needs to read the URL from stdin, using -i- flag: wget只需要使用-i-标志从标准输入读取 URL:

python retrieve_url.py | wget -i-

Output: Output:

--2021-01-14 16:15:17--  https://example.org/
Resolving example.org (example.org)... 93.184.216.34
Connecting to example.org (example.org)|93.184.216.34|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1256 (1,2K) [text/html]
Saving to: ‘index.html’

index.html                                        100%[============================================================================================================>]   1,23K  --.-KB/s    in 0s

2021-01-14 16:15:17 (20,0 MB/s) - ‘index.html’ saved [1256/1256]

FINISHED --2021-01-14 16:15:17--
Total wall clock time: 0,6s
Downloaded: 1 files, 1,2K in 0s (20,0 MB/s)

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

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