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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCL exec error on proc 1

Status
Not open for further replies.

hce

Programmer
Sep 11, 2012
3
AU
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
 
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
}

 
Hi,
Instead of using
Code:
exec $my_cmd
try rather to use command pipeline
Code:
open "[COLOR=red]|[/color]$my_cmd"

You need probably something like this:
Code:
[COLOR=#804040][b]proc[/b][/color] run_command_exception {my_cmd} {
[COLOR=#0000ff]  # try to open and catch the error[/color]
  [COLOR=#804040][b]set[/b][/color] rc [[COLOR=#804040][b]catch[/b][/color] {[COLOR=#804040][b]set[/b][/color] f [[COLOR=#804040][b]open[/b][/color] [COLOR=#ff00ff]"|[/color][COLOR=#008080]$my_cmd[/color][COLOR=#ff00ff]"[/color] [COLOR=#ff00ff]"r"[/color]]} res]
  
[COLOR=#0000ff]  # if the command was succesfull then process command output[/color]
  [COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$rc[/color] == [COLOR=#ff00ff]0[/color]} {
    [COLOR=#804040][b]while[/b][/color] {[[COLOR=#804040][b]gets[/b][/color] [COLOR=#008080]$f[/color] line] != -[COLOR=#ff00ff]1[/color]} {
[COLOR=#0000ff]      # print line[/color]
      [COLOR=#804040][b]puts[/b][/color] [COLOR=#008080]$line[/color]
    }
[COLOR=#0000ff]    # try to close and catch the error[/color]
    [COLOR=#804040][b]set[/b][/color] rc [[COLOR=#804040][b]catch[/b][/color] {[COLOR=#804040][b]close[/b][/color] [COLOR=#008080]$f[/color]} res]
  }

[COLOR=#0000ff]  # if an error occured then print it[/color]
  [COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$rc[/color] != [COLOR=#ff00ff]0[/color]} {
    [COLOR=#804040][b]puts[/b][/color] stderr [COLOR=#ff00ff]"* [/color][COLOR=#008080]$res[/color][COLOR=#ff00ff]"[/color] 
  }

[COLOR=#0000ff]  # return code[/color]
  [COLOR=#804040][b]return[/b][/color] [COLOR=#008080]$rc[/color]
}

[COLOR=#0000ff]# *** main ***[/color]
[COLOR=#804040][b]set[/b][/color] my_cmd [COLOR=#ff00ff]"cmd.exe /c DIR C:[/color][COLOR=#6a5acd]\\\"[/color][COLOR=#ff00ff]Program Files[/color][COLOR=#6a5acd]\"\\[/color][COLOR=#ff00ff]G* /B"[/color]
[COLOR=#804040][b]set[/b][/color] my_cmd [COLOR=#ff00ff]"no_such_command"[/color]
[COLOR=#804040][b]set[/b][/color] my_cmd [COLOR=#ff00ff]"ls -la"[/color]
[COLOR=#804040][b]set[/b][/color] my_cmd [COLOR=#ff00ff]"make --version"[/color]
[COLOR=#804040][b]set[/b][/color] my_cmd [COLOR=#ff00ff]"make --kk"[/color]

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"* Now executing the Command '[/color][COLOR=#008080]$my_cmd[/color][COLOR=#ff00ff]':[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]
[COLOR=#804040][b]set[/b][/color] res [run_command_exception [COLOR=#008080]$my_cmd[/color]]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"* Command Result: [/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]res = '[/color][COLOR=#008080]$res[/color][COLOR=#ff00ff]'"[/color]

I tried it with several commands and it seems to work.
 
Thank you very much mikrom, it indeed works.

Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top