如何在 expect shell 中使用变量目录名

How to use a variable directory name in an expect shell

我想在 expect shell 脚本中调用一个变量来查找当前用户 ID:

#!/usr/bin/expect -f 
set pass "password"
set username [id -un]
spawn scp -r root@serveraddress:/var/scpserver /Users/$username/Library/Songs/song.mp3`

不幸的是,这不起作用,因为它给我以下错误

Mac:TestDir Matthew$ ./tester.sh 
invalid command name "id"
while executing "id -un"
invoked from within
"set username [id -un]"
(file "./tester.sh" line 3)`

这背后的想法是我可以 运行 在任何计算机上执行此脚本,而不管分配给该计算机的用户用户名是什么。

id -un是系统命令,不是Tcl命令。您应该使用 exec 来执行任何系统命令。

set username [exec id -un]

除了这种方式,您可以依靠 Tcl 的内置 tcl_platform(user) 变量来获取当前用户名。