Command Languages

Introduction

The lecture on utilities looked at the general characteristics of utilities supplied with an O/S, and the most common of the Unix versions of these.

The most important utility is the command processor, which usually is an interpreter for a command language of some kind (procedural, graphical, etc). Command languages are distinguished by the level of object they operate on: files and processes. They also have the typical constructs of programming languages: variables, conditionals and loops.
Languages and the levels they work at
These are


Generic Command Language Operations

Contents

Manage processes
Execute commands
Variables
Loops and conditionals
Command languages and their shells have the following capabilities

Manage processes

The O/S is ultimately responsible for process management. The shell provides a simple interface to this. It allows
Process creation
In a command line environment, the shell usually provides a prompt at which you can type a command. In a GUI environment, it provides menus and supplies icons. A GUI will also usually provide a link between file types and the program that will handle them, so that selecting a file will start the corresponding program.
Process destruction
In a command line environment, a process may be killed by keyboard actions or by special programs. In a GUI environment it may be killed by window manager actions.
Running processes
Usually a process will start running immediately its program is ``selected''. There may be means of scheduling it to run at later times, to restart a stopped process, etc.
Process suspension
There will be a means of switching processor control away from a process till the user decides to resume it

Execute commands

Most shells have a set of builtin commands. The Unix shells maintain ``current working directory'' in each shell. So the cd command must run as an internal command so that the shell can keep track of the value. The exit command, to terminate execution of the shell, must also be internal.

If a command is not an internal one, then it is expected to be run from a program on disk. The shell will have a means of searching for this (using the PATH variable in Unix and DOS). Once found, a new process will be started for this.

Variables

Command languages typically have untyped variables. They usually do not have to be declared. i.e. make it simple to use them.

Loops and conditionals

To qualify as a language, command languages must have some form of conditional and loop (or recursion or goto's). This in turn requires a Boolean mechanism. An appropriate one for process control languages is to use the termination status of a process.

A process is started and eventually terminates. It may consider that it has performed its task successfully, so its ``boolean'' value is true. If it failed, its ``boolean'' value is false. MSDOS uses errorlevel for this, Unix uses exit codes.


Unix Command Language - bash

History

Family tree
From http://www.ibm.com/developerworks/aix/library/au-speakingunix_commandline/

Process management

Process creation

At the command line prompt, a process may be started by typing the name of the command (and any command line arguments) and pressing return.

If a command line finishes with an ampersand `&' then it will be run asynchronously. Without this, it will run synchronously.

The shell command interpreter reads the user input and then starts a process using the fork and exec system calls.

A GUI command shell will start processes by the user clicking on icons. It starts processes by using the exec or system system calls.

Process information

The command ps shows information about running processes

The command top shows the top process users in a display that is refreshed every few seconds.

Process destruction

A process may be terminated by sending it an interrupt of some kind.

Process control

A running process may be suspended by sending it a stop interrupt and restarted by sending it a continue interrupt. From the command prompt, these operations may be done by

For any process, if you know its process id (from ps)

Cron jobs

The daemon cron will execute commands on a regular basis. It uses configuration files per user stored in /var/spool/cron/crontabs. The configuration tables are manipulated by the user command crontab

The crontab configuration file is described in section of the manual man 5 crontab. The file has lines of siz fields

              field          allowed values
              -----          --------------
              minute         0-59
              hour           0-23
              day of month   1-31
              month          1-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sun, or use names)
	      command        command plus arguments

A typical file might be

       # run five minutes after midnight, every day
       5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
       # run at 2:15pm on the first of every month -- output mailed to paul
       15 14 1 * *     $HOME/bin/monthly
       # run at 10 pm on weekdays, annoy Joe
       0 22 * * 1-5    mail -s "It’s 10pm" joe%Joe,%%Where are your kids?%
       23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
       5 4 * * sun     echo "run at 5 after 4 every sunday"

The command at can be used to run commands once at a later time.

Exit codes

Codes

A command that terminates can either have accomplished its task or failed to do so. e.g. rm file can succeed at removing the file or can fail (due to access permissions, file not existing, etc).

The exit code is set to the value zero if the command succeeds, non-zero otherwise. You don't normally see the code.

  rm file
  echo $?
The shell variable '?' holds the value of the exit code of the last command executed.

Sequencing

rm file && echo file successfully removed
rm file || echo file not removed

Exit

A shell can execute the command exit to terminate. It can be given an exit code e.g.

$ bash
$ exit 2
$ echo $?

Shell scripts

A shell script is a file containing a sequence of commands. It doesn't need a special filename extension. The first line says which shell to run

#!/bin/bash

echo Welcome to a shell script
cd
echo Home directory has files
ls

If the script is called e.g. my_script invoke it under a new shell process by

bash my_script
or change its mode to executable and run it directly
echo Change its mode once
chmod a+x my_script
my_script
my_script

To run the script within the current shell, use '.':

. my_script
(This is usually used to include shell functions into a shell script - see later)
Jan Newmarch (http://jan.newmarch.name) <jan@newmarch.name>

Copyright © Jan Newmarch
Last modified: Wed Mar 7 20:25:45 EST 2001