| |||||||||||
Procedure with variable number of arguments Posted by kinshuk_mishra (kinshuk_mishra), 25 March 2003 Hi,I have a query regarding procedure. The problem: ----------------- I want to make a procedure which can telnet to some linux machine and execute set of commands on that machine. The exact number of commands which may be required to execute is not constant and can change as per the requirement. I now want to know that is there any way in which I can create a procedure that can take variable number of arguments. I know that it is possible as i read about the same in "practical programming in TCL " i could see the example but I think either my edition is very old or i am not able to grasp it. It will be great if some body can help me out with an example. I am sending my procedure....here i am only abe to give one $cmd or only fixed number of $cmd's but how can i make this as a variable number.... proc telnet_exec {ip login password prpasswd cmd} { set telnet_prompt [spawn telnet $ip] set cmd1 "echo $?" set expPrompt "root@chandra /root#" expect "login: " set id $spawn_id send -i $id "$login\r" expect -i $id "Password: " send -i $id "$password\r" send -i $id "su -\r" expect -i $id "Password: " send -i $id "$prpasswd\r" expect -i $id $expPrompt send -i $id "$cmd\r" send -i $id "$cmd1\r" expect -i $id "0" { puts stdout "test case passed" } else { puts stdout "test case failed" } send -i $id "exit" } Posted by admin (Graham Ellis), 25 March 2003 From our training notes - hope this helpsTcl gives an error and halts if you run a command with the wrong number of parameters. Yet, some commands can take a variable number of parameters. If you want to write your own command (proc) that takes a variable number of parameters, you can do so. If a parameter is itself a list of two parameters, the first is taken as a variable name and the second as a default value if that parameter is not specified. Thus: proc findsize {width {height 0}} { takes one or two parameters. The first, which must be specified, is placed into the variable width. If a second parameter is specified, it's placed into the variable height. If there is no second parameter, the proc carries on running with height set to 0. You can also specify a variable, or unlimited, number of parameters on the end of a proc definition by adding the word "args" to the parameter list. All remaining input variables are placed into a list called args if you so specify. Code:
And running: [graham@chapatti folder]$ vardemo Test 1 - 5 items over limit Test 2 - 1 items over limit [graham@chapatti folder]$ Posted by kinshuk_mishra (kinshuk_mishra), 26 March 2003 Thank You very much....This works for me.... This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |