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

TCL exec error on proc

TCL exec error on proc

(OP)
Hi,

I can run [catch {exec cmake --version} result] without problems, but I cannot run it from following function in a tcl script:

proc RunCommand {myCommand} {
if {[catch {exec $myCommand} result]} {
puts stderr $result
return TCL_ERROR
}
}

RunCommand {cmake --version}

couldn't execute "cmake --version": no such file or directory

what I am missing here? Appreciate helps.

Thank you.

Kind regards

RE: TCL exec error on proc

(OP)
Actually it is a problem to use variable for a command. The question is really how to set a variable for shell commands. For example, the following will always generate an error:

set myshellcommand "cmake --version"

if {[catch {exec $myshellcommand}] result} {
return TCL_ERROR
}

RE: TCL exec error on proc

Hi,
Instead of using

CODE

exec $my_cmd 
try rather to use command pipeline

CODE

open "|$my_cmd" 

You need probably something like this:

CODE

proc run_command_exception {my_cmd} {
  # try to open and catch the error
  set rc [catch {set f [open "|$my_cmd" "r"]} res]
  
  # if the command was succesfull then process command output
  if {$rc == 0} {
    while {[gets $f line] != -1} {
      # print line
      puts $line
    }
    # try to close and catch the error
    set rc [catch {close $f} res]
  }

  # if an error occured then print it
  if {$rc != 0} {
    puts stderr "* $res" 
  }

  # return code
  return $rc
}

# *** main ***
set my_cmd "cmd.exe /c DIR C:\\\"Program Files\"\\G* /B"
set my_cmd "no_such_command"
set my_cmd "ls -la"
set my_cmd "make --version"
set my_cmd "make --kk"

puts "* Now executing the Command '$my_cmd':\n"
set res [run_command_exception $my_cmd]
puts "* Command Result: \$res = '$res'" 

I tried it with several commands and it seems to work.

RE: TCL exec error on proc

(OP)
Thank you very much mikrom, it indeed works.

Cheers.

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