evilpanda evilpanda - 23 days ago 7
Bash Question

How to use a variable directory name in an expect shell

I want to call a variable to find the current user ID within an expect shell script:

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


Unfortunately this is not working as it gives me the following error

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)`


The idea behind this is that I can run this script on any computer regardless of what the users username is to assigned to that computer.

Answer

id -un is a system command, not a Tcl command. You should use exec to execute any system command.

set username [exec id -un]

Instead of this way, you can rely on the Tcl's inbuilt tcl_platform(user) variable to get the current username.