我想用python生成的参数调用c程序display_output
,但我不知道如何制定语法。我试过这个
./display_output (python -c "print 'A' * 20")
但我有
bash: syntax error near unexpected token `python'
我想这符合我最初的问题,可以帮助我解决这个问题。尝试将python cmd行输出作为bash命令运行的唯一方法是将| bash
附加到命令中。然而,有更好的方法吗?
(python -c "print 'ls'") | bash
我显然不知道如何绕过Bash,但我确信有一种更合适的方法可以做到这一点。
当bash在命令所在的位置看到一个左括号时,它将启动一个子shell来运行所包含的命令。你目前拥有它们的地方不是命令可以去的地方。你想要的是命令替换
./display_output $(python -c "print 'A' * 20")
# ...............^
如果生成的任何参数都包含空白(显然这个玩具示例不是这样的
要在bash中生成一个包含20个"a"的字符串,您需要编写:
a20=$(printf "%20s" "") # generate a string of 20 spaces
# or, the less readable but more efficient: printf -v a20 "%20s" ""
a20=${a20// /A} # replace all spaces with A's
最后一行是shell参数扩展中的模式替换