Shell 2

本文深入探讨了Shell编程的基本规则,包括如何正确使用变量、处理路径名扩展、执行安全的文件操作、查询用户信息及正则表达式的学习。通过实际代码示例,指导开发者遵循最佳实践,提高Shell脚本的效率与安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写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

4. 查询用户 who w finger group 等命令  :  http://www.linuxsir.org/main/?q=node/105

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值