test.sh:
#!/bin/sh
hello="HELLO"
who="WORLD"
text="$hello $who"
test.py:
import pathlib
content = pathlib.Path('test.sh').read_text()
print(content)
I want to get "HELLO WORLD" in my script py
You can add echo $text
on your 'test.sh' file and then capture the output of the file by using:
import subprocess
subprocess.run(['./test.sh'], capture_output=True)
You can use regular expressions, as content
is a string variable:
import pathlib
import re
content = pathlib.Path('test.sh').read_text()
print(re.search('text="(.+?)"', content))
Output:
<re.Match object; span=(37, 55), match='text="$hello $who"'>
To get the exact text
value, you can slice the string:
import pathlib
import re
content = pathlib.Path('test.sh').read_text()
v = re.search('text="(.+?)"', content)
print(v[0][6:-1])
Resulting in:
$hello $who
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.