INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

get exit staus of command in tcl

get exit staus of command in tcl

(OP)
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

RE: get exit staus of command in tcl

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/

RE: get exit staus of command in tcl

(OP)
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)

 

RE: get exit staus of command in tcl

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/

RE: get exit staus of command in tcl

(OP)
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 ?

 

RE: get exit staus of command in tcl

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

RE: get exit staus of command in tcl

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/

RE: get exit staus of command in tcl

(OP)
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.
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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!

Resources

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