-
Notifications
You must be signed in to change notification settings - Fork 0
T1 02
We will get started by opening up a shell or terminal window. Find the Meta
key on your keyboard as the Windows logo on standard PC keyboards, or as the Command
key on Mac keyboards. We will refer to both these keys as the Meta
key for the rest of this guide..
Welcome to the command line. Things will look a little scary here if this is the first time you’ve opened up what is called a command-line interface (CLI) shell. Usually, modern systems will open a graphical user interface (GUI) shell that operates with icons, apps, clicks, swipes, etc. A CLI shell is a text-based or keyboard-only interface to your computer. Throughout the rest of this guide we will simply refer to the CLI shell as the Shell. We will be using the word shell to mean the Windows Command Prompt and *nix CLI shells. The Shell is a simple interface and will open up a new world of commands for you to run on your machine.
On the Internet you may sometimes see the *nix system. This is shorthand meaning Unix and Linux systems. These two systems while often similar in many respects for the shell language, there are difference in the commands available to the two systems. In the early 2000s, Apple decided to adopt the POSIX standard of Unix for their operating system so that their shell or Terminal provides a standard set of commands that you can expect without any additional or special software installed.
On Linux, you open the Shell by opening up the terminal, konsole, shell or prompt (depending on your specific flavor of Linux like Red Hat, Ubuntu, SUSE, Fedora etc.). Each flavor has it's preferred name for the Shell.
On Mac, you open the Shell through the Terminal App by pressing Meta-Space
and typing in Terminal
.
On Windows operating systems, you open up the Shell through the regular command line by pressing Meta-R
and typing in cmd
, or the Power-Shell by pressing Meta-R
and typing in powershell
.
Your computer will know about the commands available to the Shell because there is a global variable called PATH
that stores a list of locations for all the programs that the Shell can use. You may have noticed that in the Windows script example we modified this variable with the setx
command to ensure that Cygwin was added. What this did was add all of the new programs that Cygwin installed so that we can more easily access them in the Shell. On Linux and Mac, these commands are by default installed in a directory ( /usr/bin
or /usr/local/bin
) that is being watched by the Shell so that all new commands are added.
TCL/Expect can be used in conjunction with command-line programs whether they are written in C, MatLab, Python, or any other program already available to you in the Shell.
We will output "Hello from TCL!" and use TCL/Expect to verify this from the terminal.
-
Open up a Shell.
-
Go to your home directory:
$ cd ~ $ ls
-
If your directory is empty create a
Documents
folder:$ mkdir Documents
-
Run the commands:
$ cd Documents $ mkdir Code $ cd Code $ touch hello-world.tcl
You have just changed directories to your
Documents
folder, created a folder namedCode
, changed directories to the new folder, and used thetouch
command to create an empty file namedhello-world.tcl
in the new folderCode
in yourDocuments
directory. -
On Windows, you can open the current directory in Explorer using the command:
$ explorer .
-
On Mac, you can open the current directory in Finder using the command:
$ open .
-
Open the file
hello-world.tcl
in a plain text editor (Notepad, Sublime, Nano, etc.) and copy-paste the following. Again, it is very important to keep the white space exactly as is shown in the code:#!/bin/env expect spawn echo "Hello World from TCL/Expect!" expect { "Hello world." { puts "Success!" exit 0 } default { puts "Error!" exit 1 } }
-
To run this program open up a shell and change directories to the saved file. To do this you should type:
$ cd ~/Documents/Code $ ls
And see the file
hello-world.tcl
. -
Next, run:
$ expect hello-world.tcl
You should see the following displayed on your command line screen:
$ spawn echo Hello World from TCL/Expect!
Hello World from TCL/Expect!
Error!
What is going on with this program? The first line of the program contains what is called a shebang (Sha-Bang). This will tell the Shell and you as the programmer which program it needs to use to execute this script file.
Next, with the spawn
command we tell it to start the echo
program with the argument "Hello World from TCL/Expect!"
At this point, Expect will be monitoring the output of the echo
program because of the expect
command. In the following lines, we tell it how to handle the output. First, if we see the exact text "Hello world."
then print to the screen using the puts
command and exit the program with the value 0.
If it doesn't see the text after a default amount of time, then print that we have an error and exit the program with a value of 1.
What can we do to modify the program to have the following output without changing the code on the line with "Error!"
to "Success!"
(change what Expect is expecting)?
$ spawn echo Hello World from TCL/Expect!
Hello World from TCL/Expect!
Success!
Congrats on modifying your first program and fixing a bug! With the TCL/Expect testing tool, you will be able to develop automated tests for your programs and will jump ahead of many students who don't write tests for their programs.
- Windows Command Prompt tutorial - https://www.digitalcitizen.life/command-prompt-how-use-basic-commands
- *nix Systems Shell tutorial - https://www.tutorialspoint.com/unix/unix-what-is-shell.htm
- Additional Windows Shell CMDs - https://ss64.com/nt/
- TCL/Expect main wiki - http://wiki.tcl.tk/
- Expect tutorials - https://wiki.tcl.tk/11584
- Answer - https://github.com/betsalel-williamson/C-Programming-Tutorial/blob/develop/T1/step-2-command-line-101/hello-world.tcl
Authored by Betsalel (Saul) Williamson [email protected].
For more information about SERC visit sercpitt.weebly.com