So I have been using and enjoying /usr/bin/expect for scripts because it can quickly log me into servers since it autocompletes my userid and password. Through some cursory reading, I've come to understand that it is basically a superset of TCL, which I admit knowing nothing about.

I ssh into my server all the time with my MacBook Pro, and I want to take my login script a step further and have it log into the NAS on my internal network one way if I am on my home network (with the RFC-1918 IP space of, and log into it externally a different way if I am not on that network. Basically an if-else condition.

A command to return the current IPv4 host address (at least on macOS) is:

ifconfig en0 | grep 'inet' | awk '$1 == "inet" { print $2 }'

(Replace en0 with the interface being used as necessary; it's the Wi-Fi on my Mac.)

This returns the IPv4 address. I was thinking this command should be called by some function (which TCL apparently calls procedures) and its output returned to an if-else condition that then executes the appropriate set of commands and exits the script.

I've got as far as doing simple if-else but I am not sure how the standard output from commands can be incorporated into the script. Any ideas?


To get the output of a command in Tcl, use exec:

For example, set myvar [exec "date -u"]

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.