Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This site is awesome!...Things I have been trying to figure out for weeks, I got the answer in hours!..."

Geography

Where in the world do Tek-Tips members come from?

get exit staus of command in tcl

ramana35 (TechnicalUser)
25 Apr 12 6:21
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
feherke (Programmer)
25 Apr 12 6:40
Hi

You mean, when you exec an external command ?

Just catch what it returns and extract it from its optionsVarName parameter :

CODE --> Tcl

catch {exec external_command} result option
puts [dict get $option -code]
 

Feherke.
http://feherke.github.com/

ramana35 (TechnicalUser)
25 Apr 12 6:59
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)

 
feherke (Programmer)
25 Apr 12 7:18
Hi

Quote (Kamma):

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

dict get just searches a value in the odd elements in a list and if found, returns the matching even value :

CODE --> Tcl

set whatever "one two three four five six"
puts [dict get $whatever three]
# outputs four

Quote (Kamma):

how can i exit if id username return error(means user not existed)
id returns non-zero exit code when no such user exists. It not depends on logged in status.
 

Feherke.
http://feherke.github.com/

ramana35 (TechnicalUser)
25 Apr 12 7:57
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 ?

 
Bong (Programmer)
25 Apr 12 9:24
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

feherke (Programmer)
25 Apr 12 9:34
Hi

Good point Bob.

Kamma, optionsVarName is catch's 3rd parameter, not 2nd.

The following workaround should work with older Tcl version too. Note that it reproduces strictly the extracting part, has absolutely no error checking :

CODE --> Tcl ( fragment )

if { [catch {exec /usr/bin/groups $var} bla option] } {
  puts [lindex $option [lsearch $option -code]+1]
}

Feherke.
http://feherke.github.com/

ramana35 (TechnicalUser)
25 Apr 12 9:54
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.
 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close