Flow control in ksh

今天我需要快速学习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的哟~

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值