我多次尝试从文件内容创建 Tcl 变量,但总是失败。它可以是什么?
I made several attempts to create Tcl variables from file content, always failed. What can it be?
我做了一个基本例子的大纲。为了简化我在时间上面临的场景!
为了反映发生在我身上的事情,让我们首先遵循逻辑推理。
让我们来看看演练:
1 - 我创建父目录:
$ mkdir -p /tmp/tmp.AbiGaIl
2 - 然后我创建子目录 Children:
$ mkdir -p /tmp/tmp.AbiGaIl/A
$ mkdir -p /tmp/tmp.AbiGaIl/B
$ mkdir -p /tmp/tmp.AbiGaIl/C
3 - 现在,我将用虚拟文件填充子目录:
$ 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 - 无论如何,我们可以检查一下是否一切正常。
$ cd /tmp/tmp.AbiGaIl/
$ pwd
$ ls *
5 - 已验证并确认,是时候开始运行脚本了。
$ 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." _
准备好了! bourn shell 操作 [sh
] 成功。
现在,从同样的推理开始。让我们从 Tcl 语言开始 [tclsh
].
这一步只能按照上一步(bash)的流程进行,以免创建文件等功能。让我们重用以前做过的事情。关注:
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..
所以让我们创建一个变量来到达那里。
% set view [cd $text]
Return: 无法将工作目录更改为 "A": 没有那个文件或目录
太麻烦了!
如果出现这个未知错误,我该如何去那里检查目录内容!
% puts [glob -nocomplain -type f -directory $view -tails *.png]
如果您在 glob
中直接插入直接目录名称而不通过包含该目录名称的文件提供的这个变量。这有效,请参阅:
% puts [glob -nocomplain -type f -directory A -tails *.png]
#Return: 3.png 2.png 1.png
但这不是我想要的。我真的需要这个子目录名称为 (s) 的文件。
每次按下小部件button
时都会使用此文件,因此根据与名称对应的字母表中的字母触发“exec echo "A" > /tmp/.directory
”command
每个组,因此可以访问。 .
如果有人能向我解释为什么这在访问时出错。请务必回复甚至发表评论。非常欢迎您的帮助!
首先,我建议不要在脚本中使用 cd
,因为它会改变文件名的解释。相反,在长运行处使用完全限定的文件名要简单得多。
其次,cd
除了空字符串之外,永远不会returns任何东西。不要使用空字符串作为文件名,它会使事情变得混乱(不仅仅是 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 应用程序,这很重要:您没有任何类似控制初始目录的东西。)
该建议也适用于(使用不同的语法)您可能用于此任务的所有其他编程语言。
我做了一个基本例子的大纲。为了简化我在时间上面临的场景!
为了反映发生在我身上的事情,让我们首先遵循逻辑推理。
让我们来看看演练:
1 - 我创建父目录:
$ mkdir -p /tmp/tmp.AbiGaIl
2 - 然后我创建子目录 Children:
$ mkdir -p /tmp/tmp.AbiGaIl/A
$ mkdir -p /tmp/tmp.AbiGaIl/B
$ mkdir -p /tmp/tmp.AbiGaIl/C
3 - 现在,我将用虚拟文件填充子目录:
$ 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 - 无论如何,我们可以检查一下是否一切正常。
$ cd /tmp/tmp.AbiGaIl/
$ pwd
$ ls *
5 - 已验证并确认,是时候开始运行脚本了。
$ 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." _
准备好了! bourn shell 操作 [sh
] 成功。
现在,从同样的推理开始。让我们从 Tcl 语言开始 [tclsh
].
这一步只能按照上一步(bash)的流程进行,以免创建文件等功能。让我们重用以前做过的事情。关注:
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..
所以让我们创建一个变量来到达那里。
% set view [cd $text]
Return: 无法将工作目录更改为 "A": 没有那个文件或目录
太麻烦了!
如果出现这个未知错误,我该如何去那里检查目录内容!
% puts [glob -nocomplain -type f -directory $view -tails *.png]
如果您在 glob
中直接插入直接目录名称而不通过包含该目录名称的文件提供的这个变量。这有效,请参阅:
% puts [glob -nocomplain -type f -directory A -tails *.png]
#Return: 3.png 2.png 1.png
但这不是我想要的。我真的需要这个子目录名称为 (s) 的文件。
每次按下小部件button
时都会使用此文件,因此根据与名称对应的字母表中的字母触发“exec echo "A" > /tmp/.directory
”command
每个组,因此可以访问。 .
如果有人能向我解释为什么这在访问时出错。请务必回复甚至发表评论。非常欢迎您的帮助!
首先,我建议不要在脚本中使用 cd
,因为它会改变文件名的解释。相反,在长运行处使用完全限定的文件名要简单得多。
其次,cd
除了空字符串之外,永远不会returns任何东西。不要使用空字符串作为文件名,它会使事情变得混乱(不仅仅是 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 应用程序,这很重要:您没有任何类似控制初始目录的东西。)
该建议也适用于(使用不同的语法)您可能用于此任务的所有其他编程语言。