没有合适的资源?快使用搜索试试~ 我知道了~
首页掌握bash脚本:简明入门与实践指南
掌握bash脚本:简明入门与实践指南
需积分: 10 2 下载量 123 浏览量
更新于2024-07-29
收藏 1.57MB PDF 举报
"《Bash袖珍参考手册》是一本专门为在Unix Shell环境下的Bash编程和初学者设计的实用指南。作者Arnold Robbins以其丰富的经验和深入理解,为读者提供了简洁而全面的bash命令行工具介绍。本书旨在帮助用户快速上手Bash,无论是对基础命令的掌握,还是对脚本编写和高级功能的探索,都能在这本小巧的参考手册中找到答案。 Bash(Bourne Again SHell)是Unix/Linux系统中最常用的shell之一,它继承了Bourne Shell的传统,并在此基础上进行了扩展。学习者可以通过本书了解到Bash的工作原理、语法结构、变量管理、条件语句、循环控制、函数定义以及流控制等核心概念。此外,书中还包含了许多实用技巧和最佳实践,如文件和目录操作、路径处理、输入输出重定向、错误处理等。 书中内容不仅适合系统管理员和开发者日常使用,也适用于希望提升Shell技能的编程爱好者。对于那些初次接触Bash的新手,这本袖珍手册提供了详尽的入门教程,通过实例演示和清晰步骤,让学习过程变得更加直观易懂。同时,对于有经验的用户,它则可以作为一个随身携带的参考工具,方便在遇到问题时查阅。 版权信息表明,此书享有2010年的版权,并强调了O'Reilly Media的商标权,同时也提到了相关的贸易标志。此外,书中还包括了编辑、生产编辑、校对人员和封面/内部设计师等制作团队的信息,以及印刷历史和在线版本购买渠道。 《Bash袖珍参考手册》是一本实用且便于携带的资源,无论是在学习阶段还是日常工作中,都能提供有价值的帮助,是每一位在Bash环境中工作的专业人士必备的参考资料。"
资源详情
资源推荐
Bash supports the POSIX [[=c=]] notation for matching char-
acters that have the same weight, and [[.c.]] for specifying
collating sequences. In addition, character classes, of the form
[[:class:]], allow you to match the following classes of
characters.
Class Characters matched Class Characters matched
alnum Alphanumeric
characters
print Printable characters
alpha Alphabetic
characters
punct Punctuation characters
blank Space or Tab space Whitespace characters
cntrl Control characters upper Uppercase characters
digit Decimal digits word [[:word:]] is the same as
[[:alnum:]_] (not in POSIX)
graph Nonspace
characters
xdigit Hexadecimal digits
lower Lowercase
characters
Examples
$ ls new* List new and new.1
$ cat ch? Match ch9 but not ch10
$ vi [D-R]* Match files beginning with D through R
$ pr !(*.o|core) | lp Print files non-object and non-core files
CAUTION
On modern systems, ranges such as [D-R] are not port-
able; the system’s locale may include more than just the
uppercase letters from D to R in the range.
Brace Expansion
Bash has
long supported brace expansion, based on a similar
feature from the C shell. Unlike filename metacharacters, brace
8 | The Bash Shell
expansion is purely textual; the words created by brace expan-
sion do not have to match existing files. There are two forms:
pre{X,Y[,Z…]}post
Expands to preXpost, preYpost, and so on.
pre{start..end[..incr]}post
Here, start, end, and incr are all integers. The shell expands
them to the full range between start and end, increasing
by incr if supplied. Bash ignores leading zeros on incr,
always treating it as a decimal value.
The prefix and postfix texts are not required for either form.
For numeric expansion, start or end or both may be prefixed
with one or more leading zeros. The results of expansion are
padded with zeros to the maximum of the widths of start and
end. The value of incr is treated as a plain integer, as returned
by the C library strtol(3) routine. (Thus a leading zero on incr
causes it to be treated as an octal value.)
Brace expansions may be nested, and the results are not sorted.
Brace expansion is performed before other expansions, and the
opening and closing braces must not be quoted for Bash to
recognize them. To avoid conflict with parameter expansion,
${ cannot start a brace expansion.
Examples
# Expand textually; no sorting
$ echo hi{DDD,BBB,CCC,AAA}there
hiDDDthere hiBBBthere hiCCCthere hiAAAthere
# Expand, then match ch1, ch2, app1, app2
$ ls {ch,app}?
# Expands to mv info info.old
$ mv info{,.old}
# Simple numeric expansion
$ echo 1 to 10 is {1..10}
1 to 10 is 1 2 3 4 5 6 7 8 9 10
Syntax | 9
# Numeric expansion with increment
$ echo 1 to 10 by 2 is {1..10..2}
1 to 10 by 2 is 1 3 5 7 9
# Numeric expansion with zero padding
$ echo 1 to 10 with zeros is {01..10}
1 to 10 with zeros is 01 02 03 04 05 06 07 08 09 10
Quoting
Quoting disables a character’s special meaning and allows it to
be used literally. The following table displays characters that
have special meaning.
Character Meaning
; Command separator.
& Background execution.
() Command grouping.
| Pipe.
< > & Redirection symbols.
* ? [ ] ~ + - @ ! Filename metacharacters.
" ' \ Used in quoting other characters.
` Command substitution.
$ Variable substitution (or command or
arithmetic substitution).
# Start a comment that continues to the end of the
line.
space tab newline Word separators.
These characters can be used for quoting:
" "
Everything between " and " is taken literally, except for
the following characters that keep their special meaning:
10 | The Bash Shell
$
Variable (or command and arithmetic) substitution
will occur.
`
Command substitution will occur.
"
This marks the end of the double quoted string.
' '
Everything between ' and ' is taken literally, except for
another '. You cannot embed another ' within such a
quoted string.
\
The character following a \ is taken literally. Use within
" " to escape ", $, and `. Often used to escape itself, spaces,
or newlines.
$" "
Just like " ", except that locale translation is done.
$' '
Similar to ' ', but the quoted text is processed for the
following escape sequences.
Sequence Value Sequence Value
\a Alert \t Tab
\b Backspace \v Vertical tab
\cX Control character
X
\nnn Octal value nnn
\e Escape \xnn Hexadecimal value nn
\E Escape \’ Single quote
\f Form feed \" Double quote
\n Newline \\ Backslash
\r Carriage return
Syntax | 11
Examples
$ echo 'Single quotes "protect" double quotes'
Single quotes "protect" double quotes
$ echo "Well, isn’t that \"special\"?"
Well, isn’t that "special"?
$ echo "You have `ls | wc -l` files in `pwd`"
You have 43 files in /home/bob
$ echo "The value of \$x is $x"
The value of $x is 100
Command Forms
cmd & Execute cmd in background.
cmd1 ; cmd2 Command sequence; execute multiple cmds
on the same line.
{ cmd1 ; cmd2 ; } Execute commands as a group in the current
shell.
(cmd1 ; cmd2) Execute commands as a group in a subshell.
cmd1 | cmd2 Pipe; use output from cmd1 as input to cmd2.
cmd1 `cmd2` Command substitution; use cmd2 output as
arguments to cmd1.
cmd1 $(cmd2) POSIX shell command substitution; nesting is
allowed.
cmd $((expression)) POSIX shell arithmetic substitution. Use the
result of expression as argument to cmd.
cmd1 && cmd2 AND; execute cmd1 and then (if cmd1 suc-
ceeds) cmd2. This is a “short circuit” opera-
tion: cmd2 is never executed if cmd1 fails.
cmd1 || cmd2 OR; execute either cmd1 or (if cmd1 fails)
cmd2. This is a “short circuit” operation;
cmd2 is never executed if cmd1 succeeds.
! cmd NOT; execute cmd, and produce a zero exit
status if cmd exits with a nonzero status.
Otherwise, produce a nonzero status when
cmd exits with a zero status.
12 | The Bash Shell
剩余131页未读,继续阅读
shuttlecock
- 粉丝: 3
- 资源: 45
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 解决本地连接丢失无法上网的问题
- BIOS报警声音解析:故障原因与解决方法
- 广义均值移动跟踪算法在视频目标跟踪中的应用研究
- C++Builder快捷键大全:高效编程的秘密武器
- 网页制作入门:常用代码详解
- TX2440A开发板网络远程监控系统移植教程:易搭建与通用解决方案
- WebLogic10虚拟内存配置详解与优化技巧
- C#网络编程深度解析:Socket基础与应用
- 掌握Struts1:Java MVC轻量级框架详解
- 20个必备CSS代码段提升Web开发效率
- CSS样式大全:字体、文本、列表样式详解
- Proteus元件库大全:从基础到高级组件
- 74HC08芯片:高速CMOS四输入与门详细资料
- C#获取当前路径的多种方法详解
- 修复MySQL乱码问题:设置字符集为GB2312
- C语言的诞生与演进:从汇编到系统编程的革命
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功