Welcome
Welcome to <strong>The Linux And Unix Menagerie</strong>.

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

TCL Script Trouble

Any tcl coding or Expect scripting questions and answers

TCL Script Trouble

Postby MrLinux on Tue Nov 06, 2007 10:32 pm

I'm having trouble with a tcl script I'm writing. :?

What I need to do is to be able to determine the length of an input string and check that against my input limitation. I'm stuck on two things - plucking the arguments to my script off the command line (everything after the command is a filename) and then figuring out how long (in characters) the argument names are. Any help would be greatly appreciated.

Thanks, and good luck on your forum! :D
MrLinux
 
Posts: 1
Joined: Tue Nov 06, 2007 3:45 pm

Postby laum on Tue Nov 06, 2007 11:48 pm

Hey there,

Thanks for the good luck. I can always use it :)

For the first part - dealing with your command line arguments, there are three built-in variables that you can access to help you with that:
$argc <-- This variables value will tell you how many arguments were passed to your script
$argv <-- This variable stores a list of your command line arguments (to be used for your second part)
$argv0 <-- This variable stores the command name, and not the arguments, just in case you need that.

For your second part, figuring out the length of all your individual command line arguments - I'm going to assume by "input limitation" that the arguments can only be a certain number of characters in length and pick an arbitrary length of 5 characters - you can just iterate through the $argv variable like:

foreach entry $argv {
if { [string length $entry] <= 5 } {
"do whatever you would do if the argument length is okay" <-- whatever code you need to execute
} else {
puts "argument $entry is too long - please shorten to 5 characters"
}

and that's pretty much it.

Hope that helps :)

, laum
laum
Site Admin
 
Posts: 46
Joined: Sun Oct 14, 2007 7:04 pm


Return to TCL / Expect

Who is online

Users browsing this forum: No registered users and 0 guests

cron