首页 > 解决方案 > I made several attempts to create Tcl variables from file content, always failed. What can it be?

问题描述

I made an outline of a basic example. To simplify the scenario I am facing on time!

To have a reflection of what is happening to me, let's first follow the line of logical reasoning.

Let's go to the Walkthrough:

1 - I create the parent directory:

$ mkdir -p /tmp/tmp.AbiGaIl

2 - Then I create the subdirectories Children:

$ mkdir -p /tmp/tmp.AbiGaIl/A

$ mkdir -p /tmp/tmp.AbiGaIl/B

$ mkdir -p /tmp/tmp.AbiGaIl/C

3 - Now, I will populate the subdirectories with dummy files:

$ touch /tmp/tmp.AbiGaIl/A/1.png /tmp/tmp.AbiGaIl/A/2.png /tmp/tmp.AbiGaIl/A/3.png

$ touch /tmp/tmp.AbiGaIl/B/1.png /tmp/tmp.AbiGaIl/B/2.png /tmp/tmp.AbiGaIl/B/3.png

$ touch /tmp/tmp.AbiGaIl/C/1.png /tmp/tmp.AbiGaIl/C/2.png /tmp/tmp.AbiGaIl/C/3.png

4 - Anyway, we can check if everything is ok.

$ cd /tmp/tmp.AbiGaIl/

$ pwd

$ ls *

5 - Verified and confirmed, it's time to start working the script.

$ echo "A" > /tmp/.directory

$ DIR=$(cat /tmp/.directory)

$ cd $DIR

$ ls *.*

NOTE - "This last step is the main cause of my headache, not in Bash, but in Tcl." _

Ready! The bourn shell operation [sh] was a success.

Now, starting from this same line of reasoning. Let's start with the Tcl language [tclsh].


In this step, you can only follow the flow from the previous step (bash), so as not to create the file among other features. Just let's reuse something done before. Follow:

set folder "/tmp/tmp.AbiGaIl"
#Return : /tmp/tmp.AbiGaIl

% set entry [cd $folder]

% pwd
#Return : tmp.meLBJzexGc

% puts [glob -type d $entry *]
#Return : . C B A

% set file [open "/tmp/.directory"]
#Return : file3

% set text [read $file]
#Return : A

 % close $file

% set view "$text"
#Return : A

% puts [glob -nocomplain -type f -directory $view -tails *.png]
#Return : ?

You should expect this, but do not enter the directory. I did it on purpose for you to understand or what happens if I create. You will see in the next lines below what happens. Continue..


So let's create a variable to get there.

% set view [cd $text]

Return : couldn't change working directory to "A": no such file or directory

Tai a big problem!

How can I go there and check the directory contents if it is giving me this unknown error!

% puts [glob -nocomplain -type f -directory $view -tails *.png]

If you create a direct insertion of the direct directory name in glob without going through this variable that is fed by a file containing the name of that directory. This works, see:

% puts [glob -nocomplain -type f -directory A -tails *.png]
#Return: 3.png 2.png 1.png

But that's not what I want. I really need this file with the subdirectory name (s).

This file is used every time a widget button is pressed, so an "exec echo "A" > /tmp/.directory" command is triggered according to the letter of the alphabet that corresponds to the name of each group and therefore can be accessed. .

If anyone can explain to me why this is giving error while accessing. Be sure to reply or even comment. Your help will be most welcome!

标签: tcl

解决方案


首先,我建议不要cd在脚本中使用,因为它会改变文件名的解释。相反,从长远来看,在任何地方使用完全限定的文件名要简单得多。

其次,cd永远不要返回空字符串以外的任何内容。不要使用空字符串作为文件名,它会混淆事物(不仅仅是 Tcl!)

set folder "/tmp/tmp.AbiGaIl"

set f [open "/tmp/.directory"]
gets $f dir
close $f

set subfolder [file join $folder $dir]

set files [glob -nocomplain -dir $subfolder -type f *.png]
foreach filename $files {
    puts "found [file tail $filename] in $subfolder :: $filename"
}

如果您只想要特定文件名称的最后一部分,file tail请在需要时使用。但是,当您谈论文件时,请保留完整的文件名,而不仅仅是它的名称:这样做更可靠。(如果您曾经使用 GUI 应用程序,这一点至关重要:您无法控制那里的初始目录。)

该建议适用于(使用不同的语法)您可能用于此任务的所有其他编程语言。


推荐阅读