例如,我一直试图在我的脚本文件中说“目前有 10 个人在线”。
如果没有下一行的“目前在线人员”部分,我似乎永远无法使命令工作。
目前,我有
w='who | wc -l'
echo "There are $w people online at the moment"
Run Code Online (Sandbox Code Playgroud)
但是,我总是以输出结束
There are who | wc -l users online at the moment
Run Code Online (Sandbox Code Playgroud)
你如何让命令在中间工作?我一直在尝试查看和复制示例,但它似乎对我的命令替换问题没有帮助。
Ant*_*hon 20
你想要的输出
who | wc -l
Run Code Online (Sandbox Code Playgroud)
分配给w
,而不是那个字符串,这是由于它周围的引号而得到的。您应该使用命令替换$(...)
:
w=$(who | wc -l)
echo "There are $w people online at the moment"
Run Code Online (Sandbox Code Playgroud)
(您也可以使用反引号,但不能轻易嵌套它们)。
小智 5
你应该使用反引号来执行命令
w=`who | wc -l`
echo "There are $w people online at the moment"