如何验证 expect shell 脚本中的输入参数数量?

How to validate number of input parameters in an expect shell script?

我正在尝试验证我正在创建的 shell 脚本中的参数数量。
该脚本将使用 expect.

错误

invalid command name "$#"
    while executing
"$# -ne 3 "
    invoked from within
"if [ $# -ne 3 ] then"

脚本:

#!/usr/bin/expect -f
if [ $# -ne 3 ] then
   echo "Wrong number of arguments provided"
   echo "fgrep_host.sh hostname filter_text new_file"
   exit
fi

正如@Dinesh 所说,expect 脚本是 而不是 一个 shell 脚本,它是一个 expect 脚本,它是 Tcl.

想做什么就写什么:

#!/usr/bin/expect -f
if {$argc != 3} {
  puts "Wrong number of arguments provided"
  puts "fgrep_host.sh hostname filter_text new_file"
  exit 1
}

(尽管您不应该添加 .sh 扩展名)

您必须阅读 expectTcl 才能继续。