tcl - 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:
1º
set folder "/tmp/tmp.AbiGaIl"
#Return : /tmp/tmp.AbiGaIl
2º
% set entry [cd $folder]
3º
% pwd
#Return : tmp.meLBJzexGc
4º
% puts [glob -type d $entry *]
#Return : . C B A
5º
% set file [open "/tmp/.directory"]
#Return : file3
6º
% set text [read $file]
#Return : A
7º
% close $file
8º
% set view "$text"
#Return : A
9º
% 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!
解决方案
首先,我建议不要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 应用程序,这一点至关重要:您无法控制那里的初始目录。)
该建议也适用于(使用不同的语法)您可能用于此任务的所有其他编程语言。
推荐阅读
- django - 为什么 django 无法在 AMP 故事播放器中显示图像和视频
- flutter - 添加图像(资产)时出现颤振错误
- json - 如何使用 Flutter 中的默认方法解析复杂的 JSON?
- amazon-web-services - 尝试运行 CDK 测试时出错:TypeError:将循环结构转换为 JSON,从具有构造函数“CloudFormationStackArtifact”的对象开始
- nlp - 如何使用余弦相似度对文本进行分类?
- geolocation - 在后台某个时间后未触发Android地理围栏?
- angular - Angular HttpParams 设置数值错误
- python - 将 venv 迁移到 Windows 服务器并从资源管理器运行 .py
- javascript - 在像共享存储库一样使用之前优化存储库大小
- architecture - 您应该将 dto 放在 ecb 模式中的哪个位置