脚本编写与项目构建全解析
1. 命令选项的长短形式
在日常使用命令时,我们会发现很多命令都有短选项和长选项两种形式。以ls命令为例,以下两个命令是等价的:
[me@linuxbox ~]$ ls -ad [me@linuxbox ~]$ ls --all --directory在命令行输入选项时,为了减少输入量,通常会优先选择短选项;而在编写脚本时,长选项能提高代码的可读性。
2. 命令的缩进与换行
当使用较长的命令时,将命令拆分成多行可以提高可读性。例如下面这个find命令:
[me@linuxbox ~]$ find playground \( -type f -not -perm 0600 -exec chmod 0600 '{}' ';' \) -or \( -type d -not -perm 0700 -exec chmod 0700 '{}' ';' \)乍一看,这个命令很难理解。如果在脚本中写成下面这样,就会容易理解得多:
find playground \ \( \ -type f \ -not -perm 0600 \ -exec chmod 0600 '{}' ';' \ \) \ -or \ \( \ -type d \ -not -perm 0700 \ -exec ch