Using '*'in exec command on UNIX 
Author Message
 Using '*'in exec command on UNIX

Hi,

I want to delete all the files in a directory on UNIX. I tried to use
the "exec" command but I keep getting the error "No such file or
directory". How can I do this ?

I tried 3 variations:

set filename "/home/fieldp/temp/*.tcl"
catch {exec rm $filename} catch_out
puts $catch_out

set cmd "exec rm /home/fieldp/temp/*.tcl"
puts "[split $cmd]"
eval [split ${cmd}]

exec rm "/home/fieldp/temp/*.tcl"

I tried a similar thing in Perl with no problems. eg:

`rm /home/fieldp/temp/*.tcl`;

What is Tcl or 'exec' doing with the character '*' ?

With thanks,
Peter.



Fri, 18 Oct 2002 03:00:00 GMT  
 Using '*'in exec command on UNIX

Quote:

> Hi,

> I want to delete all the files in a directory on UNIX. I tried to use
> the "exec" command but I keep getting the error "No such file or
> directory". How can I do this ?

> I tried 3 variations:

> set filename "/home/fieldp/temp/*.tcl"
> catch {exec rm $filename} catch_out
> puts $catch_out

> set cmd "exec rm /home/fieldp/temp/*.tcl"
> puts "[split $cmd]"
> eval [split ${cmd}]

> exec rm "/home/fieldp/temp/*.tcl"

> I tried a similar thing in Perl with no problems. eg:

> `rm /home/fieldp/temp/*.tcl`;

> What is Tcl or 'exec' doing with the character '*' ?

Nothing (and that's the point). The '*' (it's expansion) is handled by
your shell. In Tcl, you can use the glob-command for that:

eval exec rm [glob *]

(note that you have to use the eval command in order to 'flatten' the
list returned by glob into individual command line args).

HOWEVER: If you wan't to delete files, use file delete: It not only
saves you from doing an ugly exec, but is also platfrom independent.

------------------------------------------------------------------------
Andrej Vckovski                          andrej.vckovski at netcetera.ch
Netcetera AG, 8040 Zuerich    phone +41 1 247 79 05  fax +41 1 247 70 75



Fri, 18 Oct 2002 03:00:00 GMT  
 Using '*'in exec command on UNIX

Quote:


> > I want to delete all the files in a directory on UNIX.
   ...
> > set filename "/home/fieldp/temp/*.tcl"

 [excellent explanation elided]

But instead of:

Quote:
> eval exec rm [glob *]

I would suggest using either pure Tcl (like this)

  foreach filename [glob -nocomplain *] {file delete $filename}

or rely on the underlying UNIX operating system if you're not interested
in portability (like this)

  exec sh -c "rm *"

Of couse, the REAL problem here is that you're trying to delete *.tcl.
Try removing *.pl, and see if you have better luck. :-)

Bob
--

Mayo Foundation                                           (507) 284-2702
Rochester MN, 55905 USA   http://www.mayo.edu/sppdg/sppdg_home_page.html



Fri, 18 Oct 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. : Background execution stops at 'exec' (UNIX)

2. 'exec' command

3. Help with 'exec'command

4. Tcl7.6 'exec' command

5. WINDOW: 'exec' command returns immediately

6. Help: problem with 'exec' command

7. corresponding tcl command for UNIX 'touch'

8. Front end to unix command 'find'

9. Using 'exec' in subroutines

10. Probelm with using UNIX source command with exec

11. tcl exec command and ISE's Eiffel

 

 
Powered by phpBB® Forum Software