Mr. Black
Mr. Black

Reputation: 12122

How to count command line arguments in expect script?

This is my simple expect script:

#!/usr/local/bin/expect
puts "Argu 1 : [lindex $argv 0]"

Run
---
expect example.expt Testing

Output
------
Argu 1: Tesing

I need to count the arguments, which are passed by command line. Like this:

expect example.expt 1 2 3 4

I need the script for the following output
Total: 4 arguments
Arg1: 1
Arg2: 2
Arg3: 3
Arg4: 4

How can I do this? Thanks!

Upvotes: 7

Views: 11967

Answers (2)

Vivek
Vivek

Reputation: 3613

Late but will be useful i guess.

This prints the number of command line arguments and the arguments list as well.

#!/usr/bin/expect -f

puts "No of Command Line Arguments : [llength $argv]"

puts "Arguments List "
set argsCount [llength $argv];
set i 0
while {$i < $argsCount } {
    puts [lindex $argv $i]
    set i [expr $i+1];
}

More details about expect scripting can be found here

Upvotes: 1

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263077

expect uses Tcl as its scripting language. You're looking for the llength command:

puts "Total: [llength $argv] argument(s)"

Upvotes: 5

Related Questions