How to create temporary shellscript files and directories through Tcl language?

1.2k views Asked by At

There are numerous ways to do this and the one I present here is one of them, using the “mktemp” tool that is already installed by default in many Linux distros.

Temporary Files and Directories

The "mktemp" tool comes by default in the "GNU coreutils" package and its purpose is solely to create temporary files/directories.

Creating tempfiles and tempdir

Its basic use is quite simple. Calling 'mktemp' from the command line without any parameters will create a file on your disk inside /tmp and whose name will be displayed on the screen. Look:

$ mktemp
/tmp/tmp.JVeOizVlqk

Creating directories is as simple as adding the “-d” parameter to the command line.

$ mktemp -d
/tmp/tmp.sBihXbcmKa

Using in practice

In practice there is no use in the temporary file or directory name being displayed on the screen. It must be stored in a variable that can be accessed at all times that can be read or written during processing.

In the example below there is a small script, useless by the way, but which shows us a suitable step-by-step. Look:

#!/bin/sh
#
#Create a temporary file
TEMP = `mktemp`
#Save all the content of the entered path
ls -l "$1"> $TEMP
#Read temporary file and list only directories
grep ^d $TEMP
sudo rm -f $TEMP

Note that the command "mktemp" was invoked in a subshell (between "- crase) and its output stored in the variable "TEMP".

Then, so that the file can be read or written just use the variable, where there would be the file name, as done in the ls, grep and rm commands.

If you needed to create directories, as already said, the process is the same, just adding a -d to the mktemp command line.

#!/bin/sh
#
TEMP = `mktemp -d`
cd $TEMP
.
.
.
sudo rm -rf $TEMP

If you want to create many temporary files, we use the “-p” parameter that specifies the path where the file should be created.

#!/bin/sh
#
TEMP = `mktemp -d`
cd $TEMP
FILE1 = `mktemp -p $TEMP`
.
.
.
sudo rm -f $FILE1
sudo rm -rf $TEMP

Only that. Your scripts will now be able to use temporary files more professionally.

However, I want to do this with tclsh instead of sh [bourn shell .. but I made a few attempts and nothing worked. Here's an example of what I tried:

 # Create a temporary file
 exec sh -c "TEMP =`mktemp -d`"

 set dir [cd $TEMP]

 # Save all content from the entered path
 exec ls -l "$1"> $TEMP

 set entry [glob -type f $env(dir) *.jpg]

 # Read temporary file and list only directories
 puts $entry

My biggest problem was and is in creating the variable

# Create a temporary file
 exec sh -c "TEMP =`mktemp -d`"

This is not working!

Can someone give me a free sample ?!

1

There are 1 answers

2
Schelte Bron On BEST ANSWER

Creating temporary files can be done using file tempfile. For directories file tempdir will be available in Tcl 8.7.

On Tcl versions before 8.7, you can use file tempfile to obtain a path to a temporary location and then create a directory by that name:

set fd [file tempfile temp]
close $fd
file delete $temp
file mkdir $temp

The file tempfile command also allows you to specify a template, similar to the -p option of mktemp


To answer your updated question, you can do something like:

# Create a temporary file
set temp [exec mktemp]
# Save all content from the entered path
exec ls -l [lindex $argv 0] > $temp
# Read temporary file
set f [open $temp]
set lines [split [read $f] \n]
close $f
# List only directories
puts [join [lsearch -all -inline $lines {d*}] \n]

I ignored your mixing up of directories and regular files and whatever the *.jpg is supposed to be.

Your attempts to create shell variables from inside Tcl and then use those in the next exec command will always fail because those variables are gone when the first subshell terminates. Keep the results in Tcl variables instead, like I did above.

Of course you could more easily find the directories using glob -type d, but I kept the shell command to serve as an example.


With example, the creating of directory temporary It would be like this:

# Create a temporary directory
set dir [exec mktemp -d] ; 

# Now, files insert in directory 
# (In this example I am decompressing a ZIP file and only JPEG format images)
exec unzip -x $env(HOME)/file.zip *.jpg -d $dir ; 

# Look the directory now with this command:
puts [glob -nocomplain -type f -directory $dir -tails *.jpg] ;