Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get exit staus of command in tcl

Status
Not open for further replies.

ramana35

Technical User
Apr 23, 2012
14
HK
Hi,

Could somebody help me in getting exit staus of command in tcl.

if it is shell script we can use echo $? right, how can i do that in tcl ?


thanks in advance.


-Kamma
 
Hi

You mean, when you [tt]exec[/tt] an external command ?

Just [tt]catch[/tt] what it returns and extract it from its optionsVarName parameter :
Code:
[b]catch[/b] [teal]{[/teal][b]exec[/b] external_command[teal]}[/teal] result option
[b]puts[/b] [teal][[/teal]dict get [navy]$option[/navy] [teal]-[/teal]code[teal]][/teal]


Feherke.
 
Thanks for quick response.

Actually i would like to check if a user is existed or not.

so i can execute id username

can you describe me what puts [dict get $option -code] do ?
how can i exit if id username return error(means user not existed)

 
Hi

Kamma said:
can you describe me what puts [dict get $option -code] do ?
Do a [tt]puts [navy]$option[/navy][/tt] to see the entire list populated by [tt]catch[/tt].

[tt]dict get[/tt] just searches a value in the odd elements in a list and if found, returns the matching even value :
Code:
[b]set[/b] whatever [green][i]"one two three four five six"[/i][/green]
[b]puts[/b] [teal][[/teal]dict get [navy]$whatever[/navy] three[teal]][/teal]
[gray]# outputs four[/gray]
Kamma said:
how can i exit if id username return error(means user not existed)
[tt]id[/tt] returns non-zero exit code when no such user exists. It not depends on logged in status.


Feherke.
 
thanks, but


bash-2.05$ cat test.tcl
#!/usr/local/bin/tclsh

set var [lindex $argv 0]
puts $var

if { [catch {exec /usr/bin/groups $var} option] } {
puts [dict get $option -code]
}

bash-2.05$ test.tcl rk
rk
invalid command name "dict"
while executing
"dict get $option -code"
(file "./test.tcl" line 7)


Can you tell me how to get rid of this ?

 
dictionaries were introduced in 8.5 (I think). What version of Tcl are you using?

Anyway what do you get if you just "puts $option"?

_________________
Bob Rashkin
 
Hi

Good point Bob.

Kamma, optionsVarName is [tt]catch[/tt]'s 3[sup]rd[/sup] parameter, not 2[sup]nd[/sup].

The following workaround should work with older Tcl version too. Note that it reproduces strictly the extracting part, has absolutely no error checking :
Code:
[b]if[/b] [teal]{[/teal] [teal][[/teal][b]catch[/b] [teal]{[/teal][b]exec[/b] [teal]/[/teal]usr[teal]/[/teal]bin[teal]/[/teal]groups [navy]$var[/navy][teal]}[/teal] bla option[teal]][/teal] [teal]}[/teal] [teal]{[/teal]
  [b]puts[/b] [teal][[/teal][b]lindex[/b] [navy]$option[/navy] [teal][[/teal][b]lsearch[/b] [navy]$option[/navy] [teal]-[/teal]code[teal]]+[/teal][purple]1[/purple][teal]][/teal]
[teal]}[/teal]

Feherke.
 
below one worked for me.

if { ![catch {exec groups $user_or_scoped_pr} result] } {
puts "user exist"
{ else {

puts "user not exist"
exit
}

Anyways thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top