我正在尝试在 bash 脚本中运行 tcpdump 命令,但我不断收到此错误:tcpdump: illegal token: "
我的脚本可以总结为:
a='sudo tcpdump "icmp"' echo $a $a
当我运行 print 时echo $a
,sudo tcpdump "icmp"
但是$a
执行时,出现错误。当我复制粘贴打印的消息时,该命令正常运行。
我还尝试将命令放入数组中并通过调用 : 来执行它${array[@]}
,但它没有解决问题。
有人有解决办法吗?
答案1
尝试
eval "$a"
根据man bash
eval [arg ...] The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0.