bash:-c:第0行:从python-os.system内部运行linux cmd时,意外标记“newline”附近出现语法错误

mzsu5hc0  于 2021-06-08  发布在  Kafka
关注(0)|答案(1)|浏览(283)

我想从内部将字符串变量重定向到cmd os.system() 在python中
我有以下错误
bash:-c:第0行:意外标记“newline”附近出现语法错误
我正在使用重定向运算符 <<< 以下是我的建议mentation:-

my_var = "Hello World"
os.system('bash -c "linuxcmd <<< "${my_var}""')

直接重定向字符串可以很好地工作;即

os.system('bash -c "linuxcmd <<< "Hello""')

请帮帮我,因为我真的被困在它。
我在这里运行的linux命令是kafka console producer

nwnhqdif

nwnhqdif1#

my_var = "Hello World"
os.system('bash -c "linuxcmd <<< "{}""'.format(my_var))

或者在python3中,使用f字符串:

os.system(f'bash -c "linuxcmd <<< "{my_var}""')

测试结果如下:

my_var = 'test'
os.system(f'bash -c "echo {my_var}"')

我的Python3在manjaro上运行,效果很好。

相关问题