ScrappyDev ScrappyDev - 5 months ago 17
Bash Question

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

I'm trying to validate the number of arguments in a shell script I'm creating.

The script will be using 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 " hostname filter_text new_file"


As @Dinesh said, an expect script is not a shell script, it's an expect script, which is Tcl.

To do what you want, you'd write it:

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

(though you shouldn't be adding the .sh extension)

You're going to have to read up on expect and Tcl to continue.