技术问询:如何将终端浓缩程序输出转为字符串?以及如何用Python脚本输出的版本号替换wget命令URL中的固定版本?
Hey there! Let's break down your two questions clearly:
1. Convert Terminal Concentrate Program Output to a String
How you do this depends on whether you're working in a shell environment or a Python script:
In a Shell (e.g., Bash):Use command substitution syntax
$(...)to capture the program's output as a string. You can store it in a variable or use it directly:# Capture output into a variable tcp_output=$(terminal-concentrate-program) # Use the string directly (e.g., print it) echo "Program output: $tcp_output"The older backtick syntax
`terminal-concentrate-program`works too, but$(...)is preferred because it supports nesting and is more readable.In a Python Script:Use the
subprocessmodule to run the terminal program and capture its output:import subprocess # Capture output and decode it to a string (assuming UTF-8 encoding) tcp_output = subprocess.check_output(["terminal-concentrate-program"], text=True).strip() print(f"Program output: {tcp_output}")The
text=Trueflag automatically decodes the byte stream to a string, andstrip()removes any extra newline characters from the output.
2. Insert Python Script's Version Number into the wget URL
Your original attempt used +, which is Python's string concatenation syntax—shell doesn't work that way! The right approach is to use shell command substitution to plug in the version number from your test.py script directly. Here's the one-line solution:
wget "https://chromedriver.storage.googleapis.com/$(python3 test.py)/chromedriver_linux64.zip"
The $(python3 test.py) part will run your script first, grab its output (make sure your script only prints the version number with no extra newlines or text!), and insert that value into the URL. Wrapping the entire URL in double quotes prevents issues if the version number ever has special characters (though that's unlikely for version strings).
If your test.py outputs a trailing newline, you can strip it with:
wget "https://chromedriver.storage.googleapis.com/$(python3 test.py | tr -d '\n')/chromedriver_linux64.zip"
内容的提问来源于stack exchange,提问作者Omer

