ScrappyDev ScrappyDev - 3 months ago 13
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.

Error

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


Script:

#!/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

Answer

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 "fgrep_host.sh 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.

Comments