嘗試執行 tcl 程式碼時出現無效操作

嘗試執行 tcl 程式碼時出現無效操作

使用終端,我嘗試執行包含名為 的 tcl 程式碼的腳本myscript.tcl。根據我正在遵循的教程,我將chmod +x myscript.tcl使用 鍵入然後運行程式碼./myscript.tcl。請注意,在 tcl 腳本中,我還#!/home/localadmin/Desktop根據教程添加了腳本。但是,執行後,我收到錯誤Invalid Operation。為什麼會發生這種情況以及如何解決它?

精確誤差:E: Invalid operation ./myscript.tcl

教學連結:https://www.youtube.com/watch?v=U5m_vuBzdZE&list=PL7616FA0112D74AD3&index=2,參考1:10

代碼 :

#variables set to integer will ALWAYS be integer, cannot assign strings
set x 3

set y 4

#$x means the value of x
#whatever happens after expr will take it as a mathematical expression
#whatever inside brackets is executed
set z [expr $x + $y]

#text is option for command label
#the . represents anything coming from main window after execution
label .sum -text "z is $z"

label .myname -text "My NAME is RAND PAUL"

incr z

label .increment -text "incrementing z -> $z"

#pack puts the previous labels on te main window
#without pack, will discard the labels
pack .myname .increment .sum

跟進:

我有另一個腳本 -

#!/usr/bin/tclsh


#Lesson 3 tk/tcl lists


set to_do [list]
lappend to_do "name"
lappend to_do "is"
lappend to_do "bobby"
lappend to_do "jones"

set num [list 0 1 2 3 4 5 6 7 8 9 10 11]
puts $to_do
puts $num
puts "index 0 is [lindex $to_do 0]"
puts "index 3 is [lindex $to_do 3]"
puts "replacing one element..."
puts [lreplace $num 3 3 [list 30 40 50 60]]
puts "replacing a range of elements from index 3 till ndex 6 => other indexees from 4 till 6 will deleted"
puts [lreplace $num 3 6 [list 30 40 50 60]]

puts "inserting one element at index 1 (2nd element that is)..."
puts [linsert $num 1 "new list element"]

產生相同的錯誤:E: Invalid operation ./lesson3.tcl。我shebang在原始帖子中使用了在這個新文件中找到的相同內容並修復了它,但是,在嘗試運行這個新文件時我仍然遇到相同的錯誤。

答案1

我猜你的腳本沒有運行的原因是因為你的腳本shebang完全錯誤。重點shebang是告訴編譯器運行腳本的套件或程序,而#!/home/localadmin/Desktop編譯器不關心任何內容(我猜)

到目前為止,其他一切看起來都是正確的(我再次不熟悉tcl)所以我會嘗試將腳本中的第一行更改為如下所示

myscript.tcl

 #!/usr/bin/tclsh
 my_script.....

假設這不起作用,您可以透過執行以下命令來追蹤阻止腳本運行的任何錯誤(例如語法錯誤等)

tclsh your_script.tcl


看完影片後,本教學不適合您(假設您確實正在執行 ubuntu)如何運行腳本

相關內容