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.
These are
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.
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.
Family tree
From http://www.ibm.com/developerworks/aix/library/au-speakingunix_commandline/
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.
The command ps
shows information about running processes
ps
shows info about your processes run from this shell
ps -l
shows long info: who is running it (UID),
what's its id (PID), what is its parent's if (PPID),
how mcuh times has it used, etc
ps -a
show all processes
ps -x
show all processes including those not attached
to a terminal
The command top
shows the top process users in a
display that is refreshed every few seconds.
kill
command
kill
process_id
kill -KILL
process_id
killall
name
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
fg
will continue it in the foreground
bg
will continue it in the background
For any process, if you know its process id (from ps
)
kill -STOP PID
pause
kill -CONT PID
resume
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
crontab -l
lists your crontab entry
crontab -e
edits your crontab entry
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.
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.
command_1 ; command_2
executes both commands
in order
command_1 && command_2
executes the
second command only if the first one succeeds
command_1 || command_2
executes the
second command only if the first one fails
rm file && echo file successfully removed rm file || echo file not removed
A shell can execute the command exit
to terminate.
It can be given an exit code e.g.
$ bash $ exit 2 $ echo $?
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_scriptor 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)