最近在接老项目,是由Python和Shell编写的。而且其中对于逻辑调用处理的部分主要采用的是Shell。早就知道这个难啃,但是想不到其中奇奇怪怪的操作符那么多。今天就来好好梳理一下。
1. 奇怪的操作符
$?是做什么的?
$?is used to find the return value of the last executed command. Try the following in the shell:ls somefile echo $?If
somefileexists (regardless whether it is a file or directory), you will get the return value thrown by thelscommand, which should be0(default “success” return value). If it doesn’t exist, you should get a number other then 0. The exact number depends on the program.
也就是说这个是用来返回上一句的返回值!
$#是做什么的?
是用来返回传入的参数个数的~
如何记忆:# 有number 的意思
2. 一些常用命令的梳理
pwd是做什么的?
Pwd, means “print working directory”.
shift是做什么的?
用来将参数左移,每次销毁最左边的参数并且返回。
3. 一些约定俗成的用法
关闭STDOUT和STDERR,并且输出到log file之中
# Close STDOUT file descriptor exec 1<&- # Close STDERR FD exec 2<&- # Open STDOUT as $LOG_FILE file for read and write. exec 1>>${LOG_FILE} # Redirect STDERR to STDOUT exec 2>&1
4. Shell的逻辑运算符
| 运算符号 | 代表意义 |
|---|---|
| = | 等于 应用于:整型或字符串比较 如果在[] 中,只能是字符串 |
| != | 不等于 应用于:整型或字符串比较 如果在[] 中,只能是字符串 |
| < | 小于 应用于:整型比较 在[] 中,不能使用 表示字符串 |
| > | 大于 应用于:整型比较 在[] 中,不能使用 表示字符串 |
| -eq | 等于 应用于:整型比较 |
| -ne | 不等于 应用于:整型比较 |
| -lt | 小于 应用于:整型比较 |
| -gt | 大于 应用于:整型比较 |
| -le | 小于或等于 应用于:整型比较 |
| -ge | 大于或等于 应用于:整型比较 |
| -a | 双方都成立(and) 逻辑表达式 –a 逻辑表达式 |
| -o | 单方成立(or) 逻辑表达式 –o 逻辑表达式 |
| -z | 空字符串 |
| -n | 非空字符串 |