【问题标题】:How to use a variable directory name in an expect shell如何在期望 shell 中使用变量目录名称
【发布时间】:2016-11-15 15:46:17
【问题描述】:

我想调用一个变量来在期望的 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)`

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

【问题讨论】:

  • 设置公钥授权,你就不需要expect了。
  • @chepner 这对我来说是不切实际的,因为我计划在 50 多台计算机上使用它。那将需要我设置每个人。但是我相信这确实对较小规模的设备有用。如果我错了,请纠正我:)

标签: bash macos shell variables expect


【解决方案1】:

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

set username [exec id -un]

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

【讨论】:

  • 或者直接使用$env(USER)
  • Windows 没有env(USER),而是env(USERNAME)。这也是我以前更喜欢tcl_platform(user)的原因
猜你喜欢
  • 2021-09-18
  • 1970-01-01
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多