今天我需要快速学习ksh并且达到可以干活儿的状态。 第一个目标是学习这个:
[ ! -d $codeHome ]
1,[] 和[[]]
2,-d是什么
3,&
Here are some points I think really really important,下面的内容全部在Chapter 5: Flow Control
:
1, Exit Status and Return (P137).
Every Unix command, whether it comes from source code in C, some other language,
or a shell script/function, returns an integer code to its calling process — the
shell in this case—when it finishes. This is called the exit status. 0 is usually the
“OK” exit status, while anything else (1 to 255) usually denotes an error.* The way
ksh handles exit statuses for built-in commands is described in more detail later in
this section.
$? Exit status of last command.
这么用:
ls ./ test.ksh
if [ "$?"="0" ];then
...
else
For ksh93, the exit status values for built-in commands and several exceptional
cases have been regularized as follows:
Value Meaning
1–125 Command exited with failure
2 Invalid usage, with usage message (built-in commands)--This is really common in ksh.
126 Command found, but file is not executable
127 Command not found
128–255 External command exited with failure
≥ 256 Command died with a signal; subtract 256 to get signal number
2,[ ! -d $codeHome ] 中的-d (P149) Very simple.
The other kind of operator that can be used in conditional expressions checks a
file for certain properties.
Operator True if ...
-e file file exists
-d file file is a directory
-f file file is a regular file (i.e., not a directory or other special type of file)
-L file file is a symbolic link
-r file You have read permission on file
-s file file exists and is not empty
-w file You have write permission on file
-x file You have execute permission on file or directory search permission if it
is a directory
-O file You own file (the effective UID matches that of file)
-G file Your effective group ID is the same as that of file
file1 -nt file2 file1 is newer than file2a
file1 -ot file2 file1 is older than file2
file1 -ef file2 file1 and file2 ar e the same file
3,[] and [[]]
[[ condition ]] is actually a statement just like any other, except that the only
thing it does is retur n an exit status that tells whether condition is true. Thus it fits
within the if construct’s syntax of if statements.
还是不是很明白这个。这样看if后面可以跟
[]: return a boolean value?
[[]]
built-in command
Both [[]] and build-in command can return exit status.
这样也可以:
if grep $word1 $filename > /dev/null || grep $word2 $filename > /dev/null
then
: # do nothing
else
print "$word1 and $word2 are both absent from $filename."
fi
不可以像一般language那样用什么if ture, if 0/1的哟~