写Shell的一般规则:
1. 开头指定shell的编译器 ,如:#!/bin/bash
2. 引用字符串变量时应该加上"" , 如: string="Hellokitty" ; echo "${Hellokitty}"
具体的原因原来涉及到Bash解析器的解析原理:
Pathname Expansion
After word splitting, unless the -f option has been set, bash scans each word for
the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern,
and replaced with an alphabetically sorted list of file names matching the pattern. If no matching file names are found, and the shell option nullglob is dis-abled, the
word is left unchanged. If the nullglob option is set, and no matches are found, the word is removed. If the failglob shell option is set, and no matches are found, an error message is printed and the command is not executed. If the shell option nocaseglob is
enabled, the match is performed without regard to the case of alpha- betic characters. Note that when using range expressions like [a-z] (see below), letters of the other case may be included, depending on the setting of LC_COLLATE.
3. 删除某个目录下面的文件应该做匹配,如果路径是个变量,那么请先判断变量是否为空:
[ -n "${FILE_PATH}" ] && rm -r ${FILEPATH}/abc*.sh
getent passwd username >/dev/null || useradd -c "username" -s /bin/bash -g usergroup -r username 2>> /dev/null
5. 正则表达式学习帖子:《半个小时学会正则表达式》
http://deerchao.net/tutorials/regex/regex.htm
http://deerchao.net/tutorials/regex/regex.htm
本文深入探讨了Shell编程的基本规则,包括如何正确使用变量、处理路径名扩展、执行安全的文件操作、查询用户信息及正则表达式的学习。通过实际代码示例,指导开发者遵循最佳实践,提高Shell脚本的效率与安全性。

1371

被折叠的 条评论
为什么被折叠?



