- Shell commands (commands to be executed by the operating
- system) can be freely mixed with other code.
- NOTE: the syntax and shell usage is identical on all plat
- forms, including Win32. To avoid portability problems on Win32,
- it is recommended that you avoid the use of the native shell in
- terpreter cmd.
LIB = $(dir lib)
println(The contents of the $(LIB) directory is:)
ls $(LIB)
- BASIC COMMANDS
- The syntax of shell commands is similar to the syntax used
- by the Unix shell bash. In general, a command is a pipeline. A
- basic command is part of a pipeline. It is specified with the
- name of an executable and some arguments. Here are some examples.
ls
ls -AF .
echo Hello world
- The command is found using the current search path in the
- variable PATH[], which should define an array of directories con
- taining executables.
- A command may also be prefixed by environment variable
- definitions.
# Prints "Hello world"
env X="Hello world" Y=2 printenv X
# Pass the include path to the Visual C++
env include="c:Program Filescrosoft SDKinclude" cl
foo.cpp
- GLOBBING
- Commands may contain wildcard patterns. A pattern speci
- fies a set of files through a limited kind of regular expression.
- Patterns are expanded before the function is executed.
# List all files with a .c suffix
ls *.c
# List all files with a single character prefix, and .c
suffix
ls ?.c
# Rename the file hello.ml to foo.ml
mv {hello,foo}.ml
- BACKGROUND JOBS
- The command may also be placed in the background by plac
- ing an ampersand after the command. Control returns to the shell
- without waiting for the job to complete. The job continues to run
- in the background.
gcc -o hugeprogram *.c &
- FILE REDIRECTION
- Input and output can be redirected to files by using the
- <, >, and >& directives after the command.
# Write to the "foo" file
echo Hello world > foo
# Redirect input from the foo file
cat < foo
# Redirect standard output and errors to the foo file
gcc -o boo *.c >& foo
- PIPELINES
- Pipelines are sequences of commands, where the output from
- each command is sent to the next. Pipelines are defined with the
- | and |& syntax. With | the output is redirected, but errors are
- not. With |& both output and errors are redirected.
# Send the output of the ls command to the printer
ls *.c | lpr
# Send output and errors to jyh as email
gcc -o hugefile *.c |& mail jyh
- CONDITIONAL EXECUTION
- Commands may also be composed though conditional evalua
- tion using the || and && syntax. Every command has an integer ex
- it code, which may be zero or some other integer. A command is
- said to succeed if its exit code is zero. The expression command1
- && command2 executes command2 only if command1 succeeds. The ex
- pression command1 || command2 executes command2 only if command1
- fails.
# Display the x/y file if possible
cd x && cat y
# Run foo.exe, or print an error message
(test -x foo.exe && foo.exe) || echo "foo.exe is not
executable"
- GROUPING
- Parenthesis are used for grouping in a pipeline or condi
- tional command. In the following expression, the test function is
- used to test whether the foo.exe file is executable. If it is,
- the foo.exe file is executed. If the file is not executable (or
- if the foo.exe command fails), the message "foo.exe is not exe
- cutable" is printed.
# Run foo.exe, or print an error message
(test -x foo.exe && foo.exe) || echo "foo.exe is not
executable"
- WHAT IS A SHELL COMMAND?
- Syntactially, shell commands are any line that is not one
- of the following:
- * A variable definition of the form VAR=string
- * A function call f(...) or method call o.f(...)
- * A rule definition containing a colon string: ...
- * A special command, including the following:
* if ...
* switch ...
* match ...
* section ...
* return ...
- Commands may also be builtin (aliases). See the documenta
- tion for the Shell object, defined in Pervasives for more infor
- mation.