之前我只有在 bash 使用過,今天突然間想試看看 tclsh。我發現…… 對,可以使用同樣的手法。下面就是一個測試的例子:
tclsh - << "EOF"
puts "HELLO World"
EOF
再來是另外一個簡單的測試:
tclsh - << "EOF"
puts [expr 99 + 1]
EOF
這樣就可以在命令列執行簡單的 tclsh 命令。
tclsh - << "EOF"
puts "HELLO World"
EOF
tclsh - << "EOF"
puts [expr 99 + 1]
EOF
#!/usr/bin/env tclsh
puts -nonewline "Please input a number: "
flush stdout
gets stdin var
if { [ regexp {^[0-9]+$} $var ] == 0} {
puts "It is not a number!!!"
exit
}
if { [expr $var % 2] == 0} {
puts "It is even!!!"
} else {
puts "It is odd!!!"
}
#!/usr/bin/env tclsh
for {set nx 0} {$nx < 10} {incr nx} {
for {set ny 0} {$ny < 10} {incr ny} {
puts "[format "%d x %d = %2d" $nx $ny [expr $nx * $ny]]"
}
}
#!/usr/bin/env tclsh
set x [list 1 2 3 4 5 6 7 8 9]
set y [list 1 2 3 4 5 6 7 8 9]
foreach nx $x {
foreach ny $y {
set z [::tcl::mathop::* $nx $ny]
puts "[format "%d x %d = %2d" $nx $ny $z]"
}
}
#!/usr/bin/env tclsh
namespace path {::tcl::mathop ::tcl::mathfunc}
proc mul {x y} {
puts [format "%d x %d = %2d" $x $y [* $x $y]]
if {$y < 9} {
mul $x [+ $y 1]
} else {
if {$x < 9} {
mul [+ $x 1] 1
} else {
return
}
}
}
mul 1 1
sudo zypper install qemu-arm
wget https://download.opensuse.org/ports/aarch64/factory/images/openSUSE-Tumbleweed-ARM-JeOS-efi.aarch64-Current.raw.xz
unxz openSUSE-Tumbleweed-ARM-JeOS-efi.aarch64-Current.raw.xz
wget https://releases.linaro.org/components/kernel/uefi-linaro/16.02/release/qemu64/QEMU_EFI.img.gz
gzip -d QEMU_EFI.img.gz
qemu-system-aarch64 -m 2048 -cpu cortex-a57 -smp 2 -M virt -bios QEMU_EFI.img -serial stdio -device virtio-net-device,netdev=hostnet0,id=net0,mac=52:54:00:09:a4:37 -netdev user,id=hostnet0 -drive if=none,format=raw,file=openSUSE-Tumbleweed-ARM-JeOS-efi.aarch64-Current.raw,id=hd0 -device virtio-blk-device,drive=hd0
package require xqilla
xqilla db
set exprs [db prepare {days-from-duration(xs:dateTime('2019-06-14T00:00:00') - current-dateTime())}]
set result [$exprs execute]
set diff 0
while {[$result next]} {
set diff [$result string_value]
}
puts "The answer is $diff."
$result close
$exprs close
db close
package require tdbc::sqlite3
tdbc::sqlite3::connection create db :memory:
set statement [db prepare {SELECT CAST ((JulianDay('2019-06-14') - JulianDay('now')) as Integer) as d}]
set diff 0
$statement foreach row {
if {[catch {set diff [dict get $row d]}]} {
set diff 0
}
}
puts "The answer is $diff."
$statement close
db close