Bashスクリプトで使用されるコマンドの実行

Bashスクリプトで使用されるコマンドの実行

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.

おすすめ記事