Reading command line input
In general, Tcl reads the arguments following the script name as a list and creates the following three variables:
argv
: A Tcl list containing all of the arguments in the command lineargc
: A count of the number of arguments in the listsargv0
: The script name.
For example, the following script uses all three variables. This is a simple command in the form
>/bin/sh MktDept.sh name name name
where name
is a person’s name, such as Mary
or Joe
. If you want to use first and last name, surround the name with quotes, for example “Jane Smith”
.
Note: This code sample demonstrates starting ADEdit from a shell script. The subsequent examples use the executable file model.
MktDept.sh
#!/bin/sh # This script takes a list of names and displays it # # \ exec adedit "$0" ${1+"$@"} package require ade_lib if { $argc == 0 } { puts "Command format: $argv0 name name ..." exit 1 } set total $argc puts " The following people are in the marketing department" while {$total > 0} { incr total -1 puts "[lindex $argv $total]" }
The first if statement uses the count, argc
, to determine if any arguments have been entered. If the argc
value is equal to zero, the user did not enter any names and the script displays the command format message. The argc
counter is used again to set the total
count of names entered for the while loop. Inside the loop, the names are drawn from the argv
list.
Another useful command for parsing command line options is getopt
. This command derives from, but is different than, the Tcl getopt
command. The ADEdit getopt
command has the following syntax:
getopt _argv name ?-var?
where:
_argv
is the Tcl list that contains the command line arguments.name
is a label for the associated data.?_var?
is the variable name for the data.
For example, the following script illustrates the use of getopt
to define the user and group variables that will be used later in the script.
This script also demonstrates how to use a procedure, usage
, that prompts the user when she doesn’t enter all of the arguments. usage
first displays the full command syntax and then the missing argument.
Note: The user and password arguments are optional. If the user enters a user name without the password, the bind
program automatically prompts for the password. You do not need to include that prompt in the script.
getopt-example
#!/bin/env adedit
# This script takes a domain name and optionally user name and password
# and binds the user to the specified domain.
# If the user does not specify a user name or password, she is prompted.
#
package require ade_lib
proc usage {msg} {
puts {usage: -d <domain> [-u <user>] [-p <password>]}
puts $msg
exit 1
}
if {[getopt argv -d domain] == 0} {
usage "Missing Domain, ex. centrify.demo"
}
if {[getopt argv -u user] != 0} {
if {[getopt argv -p password]} {
bind $domain $user $password
} else {
bind $domain $user}
} else {
puts "Enter administrator name:"
gets stdin user
bind $domain $user
}
puts "
Binding complete to $domain."