Conventional UNIX shell is an interpreter and shell script is a macro-expansion based language. Actually, shell script can be any kind of language. For example, scsh use 'scheme-48' as its control language. I remember BASIC is designed to be the OS control language as well. At least, people should remember Apple BASIC on Apple II. Of course, we should remember a terrific language, FORTH. Unfortunately, all the language I mentioned, BASIC, Scheme, FORTH are better languages than shells we used in UNIX environment. Although conventional UNIX shells like sh, ksh, bash, csh and tcsh are not perfect enough, they bind all UNIX software tools as the power tools with UNIX IPC mechanism.
Input/Ouput Redirection:
|
|
|
input from infile |
|
|
output to outfile |
|
|
append to outfile |
|
|
write to noclobber outfile |
|
|
append to noclobber outfile |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
close stdin (fd:0) |
|
|
close stdout (fd:1) |
|
|
close stderr (fd:2) |
|
|
Write to file descriptor 4 |
|
|
csh | sh,bash | |
if ( ... ) then
... else ... endif |
if [ ... ] ; then
... else ... fi |
|
foreach x in ( ... )
... end |
for x ... ; do
... done |
|
while ( ... )
... end |
while ... ; do
... done |
|
until ... ; do
... done |
||
if [ $VAR
!= "Hello World" ] ;
then
echo $VAR
fi
Because 'sh' is a macro expantion language. If $VAR is not exist, the code will be expanded as
if [ != "Hello World"
] ; then
echo $VAR
fi
And the shell will complain `!=' is not a unary operator and return an error. If we use double quote to quote $VAR, "$VAR" , then the code will be expanded as
if [ "" !=
"Hello World" ] ;
then
echo $VAR
fi
Therefore, to write a safe code to operate string, we should remember
to add double quotes for variable.