TCL-Expect scripts are an amazingly easy way to script out laborious tasks in the shell when you need to be interactive with the console. Think of them as a "macro" or way to programmaticly step through a process you would run by hand. They are similar to shell scripts but utilize the .tcl extension and a different #! call.
The first step, similar to writing a bash script, is to tell the script what it's executing under. For expect we use the following:
#!/usr/bin/expect
You also must place interact at the end
Variables are very simple, just use set {name} {value}, for example:
set a "apple"
set b "banana"
set c "cantalope"
Referencing variables is done by simply appending $ to the name - so $a would contain apple.
If you want to set a variable by passing in an argument simply use the following:
set user [lindex $argv 0]
set password [lindex $argv 1]
Then, when running the script I could supply the varaible content via:
./myscript.tcl myuser mypassword
You can start a process with the spawn command. For example, ssh or scp are great examples:
set user "myuser"
set server "myserver.com"
spawn ssh "$user\@server"
The above would spawn the ssh process and submit the user and server, so essentially like entering ssh myuser@myserver.com in the console.
The send command allows you to send something to the console. For example, a password:
set password [lindex $argv 0]
set app [lindex $argv 1]
spawn sudo "apt-get install $app"
expect "assword"
send $password
The above would allow you to pass in your sudo password and the name of an application to install from apt, then wait for the password prompt and send it. No assword is not a mispelling, let's look at that expect statement...