没有合适的资源?快使用搜索试试~ 我知道了~
首页深入学习Linux shell脚本教程
"Advanced Bash-Scripting Guide" 是一本深入探索Linux shell脚本编程艺术的教程,由Mendel Cooper撰写,邮箱地址为<the_grendel.abs@gmail.com>。该书针对没有编程背景的学习者设计,但内容逐步提升至中级到高级水平,旨在教授shell脚本技巧,并鼓励读者通过实践学习。书中包含丰富的实例和详尽的注释,使读者在参与编写过程中加深理解。 作者Mc(可能是Mendel Cooper的缩写)于2009年9月30日进行了最后一次修订,这次被称为'BUFFALOBERRY'版本的更新,表明了作者对内容的持续维护和完善。本书特别适合用作课堂教材,用于介绍编程概念,强调理论学习与实践经验相结合的重要性。 第一章"Shell Programming!" 引入了shell编程的基本概念,让读者从零开始接触这个领域。第二章则从一个名为"Starting Off With a Sha-Bang"的章节开始,这里可能涉及如何使用bash shell脚本语言编写第一个脚本,以及设置执行环境等基础知识。 这本书的特点在于它不仅是学习材料,还是一本手册和参考资料库,提供了大量的练习和示例,帮助读者扎实掌握shell脚本技能。"Advanced Bash-Scripting Guide"是任何想要深入学习Linux shell编程的用户不可或缺的资源,无论是初学者还是希望提升现有技能的专业人士。
资源详情
资源推荐
bash$ cp /home/bozo/current_work/junk/* .
Copy all the "junk" files to $PWD.
.
"dot" character match. When matching characters, as part of a regular expression, a "dot" matches a
single character.
"
partial quoting [double quote]. "STRING" preserves (from interpretation) most of the special
characters within STRING. See Chapter 5.
'
full quoting [single quote]. 'STRING' preserves all special characters within STRING. This is a
stronger form of quoting than "STRING". See Chapter 5.
,
comma operator. The comma operator [15] links together a series of arithmetic operations. All are
evaluated, but only the last one is returned.
let "t2 = ((a = 9, 15 / 3))" # Set "a =
9" and "t2 = 15 / 3"
The comma operator can also concatenate strings.
for file in /{,usr/}bin/*calc
# ^ Find all executable files ending in "calc"
#+ in /bin and /usr/bin directories.
do
if [ -x "$file" ]
then
echo $file
fi
done
# /bin/ipcalc
# /usr/bin/kcalc
# /usr/bin/oidcalc
# /usr/bin/oocalc
# Thank you, Rory Winston, for pointing this out.
,, ,
Lowercase conversion in parameter substitution (added in version 4 of Bash).
\
escape [backslash]. A quoting mechanism for single characters.
\X escapes the character X. This has the effect of "quoting" X, equivalent to 'X'. The \ may be used to
quote " and ', so they are expressed literally.
See Chapter 5 for an in-depth explanation of escaped characters.
/
Filename path separator [forward slash]. Separates the components of a filename (as in
/home/bozo/projects/Makefile).
This is also the division arithmetic operator.
`
command substitution. The `command` construct makes available the output of command for
assignment to a variable. This is also known as backquotes or backticks.
Advanced Bash-Scripting Guide
Chapter 3. Special Characters 10
:
null command [colon]. This is the shell equivalent of a "NOP" (no op, a do-nothing operation). It
may be considered a synonym for the shell builtin true. The ":" command is itself a Bash builtin, and
its exit status is true (0).
:
echo $? # 0
Endless loop:
while :
do
operation-1
operation-2
...
operation-n
done
# Same as:
# while true
# do
# ...
# done
Placeholder in if/then test:
if condition
then : # Do nothing and branch ahead
else # Or else ...
take-some-action
fi
Provide a placeholder where a binary operation is expected, see Example 8-2 and default parameters.
: ${username=`whoami`}
# ${username=`whoami`} Gives an error without the leading :
# unless "username" is a command or builtin...
Provide a placeholder where a command is expected in a here document. See Example 18-10.
Evaluate string of variables using parameter substitution (as in Example 9-16).
: ${HOSTNAME?} ${USER?} ${MAIL?}
# Prints error message
#+ if one or more of essential environmental variables not set.
Variable expansion / substring replacement.
In combination with the > redirection operator, truncates a file to zero length, without changing its
permissions. If the file did not previously exist, creates it.
: > data.xxx # File "data.xxx" now empty.
# Same effect as cat /dev/null >data.xxx
# However, this does not fork a new process, since ":" is a builtin.
See also Example 15-15.
In combination with the >> redirection operator, has no effect on a pre-existing target file (: >>
target_file). If the file did not previously exist, creates it.
Advanced Bash-Scripting Guide
Chapter 3. Special Characters 11
This applies to regular files, not pipes, symlinks, and certain special files.
May be used to begin a comment line, although this is not recommended. Using # for a comment
turns off error checking for the remainder of that line, so almost anything may appear in a comment.
However, this is not the case with :.
: This is a comment that generates an error, ( if [ $x -eq 3] ).
The ":" also serves as a field separator, in /etc/passwd, and in the $PATH variable.
bash$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/sbin:/usr/sbin:/usr/games
!
reverse (or negate) the sense of a test or exit status [bang]. The ! operator inverts the exit status of
the command to which it is applied (see Example 6-2). It also inverts the meaning of a test operator.
This can, for example, change the sense of equal ( = ) to not-equal ( != ). The ! operator is a Bash
keyword.
In a different context, the ! also appears in indirect variable references.
In yet another context, from the command line, the ! invokes the Bash history mechanism (see
Appendix K). Note that within a script, the history mechanism is disabled.
*
wild card [asterisk]. The * character serves as a "wild card" for filename expansion in globbing. By
itself, it matches every filename in a given directory.
bash$ echo *
abs-book.sgml add-drive.sh agram.sh alias.sh
The * also represents any number (or zero) characters in a regular expression.
*
arithmetic operator. In the context of arithmetic operations, the * denotes multiplication.
** A double asterisk can represent the exponentiation operator or extended file-match globbing.
?
test operator. Within certain expressions, the ? indicates a test for a condition.
In a double-parentheses construct, the ? can serve as an element of a C-style trinary operator, ?:.
(( var0 = var1<98?9:21 ))
# ^ ^
# if [ "$var1" -lt 98 ]
# then
# var0=9
# else
# var0=21
# fi
In a parameter substitution expression, the ? tests whether a variable has been set.
?
wild card. The ? character serves as a single-character "wild card" for filename expansion in
globbing, as well as representing one character in an extended regular expression.
Advanced Bash-Scripting Guide
Chapter 3. Special Characters 12
$
Variable substitution (contents of a variable).
var1=5
var2=23skidoo
echo $var1 # 5
echo $var2 # 23skidoo
A $ prefixing a variable name indicates the value the variable holds.
$
end-of-line. In a regular expression, a "$" addresses the end of a line of text.
${}
Parameter substitution.
$*, $@
positional parameters.
$?
exit status variable. The $? variable holds the exit status of a command, a function, or of the script
itself.
$$
process ID variable. The $$ variable holds the process ID [16] of the script in which it appears.
()
command group.
(a=hello; echo $a)
A listing of commands within parentheses starts a subshell.
Variables inside parentheses, within the subshell, are not visible to the rest of the
script. The parent process, the script, cannot read variables created in the child
process, the subshell.
a=123
( a=321; )
echo "a = $a" # a = 123
# "a" within parentheses acts like a local variable.
array initialization.
Array=(element1 element2 element3)
{xxx,yyy,zzz,...}
Brace expansion.
echo \"{These,words,are,quoted}\" # " prefix and suffix
# "These" "words" "are" "quoted"
cat {file1,file2,file3} > combined_file
# Concatenates the files file1, file2, and file3 into combined_file.
cp file22.{txt,backup}
# Copies "file22.txt" to "file22.backup"
Advanced Bash-Scripting Guide
Chapter 3. Special Characters 13
A command may act upon a comma-separated list of file specs within braces. [17] Filename
expansion (globbing) applies to the file specs between the braces.
No spaces allowed within the braces unless the spaces are quoted or escaped.
echo {file1,file2}\ :{\ A," B",' C'}
file1 : A file1 : B file1 : C file2 : A file2 : B file2 :
C
{a..z}
Extended Brace expansion.
echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z
# Echoes characters between a and z.
echo {0..3} # 0 1 2 3
# Echoes characters between 0 and 3.
The {a..z} extended brace expansion construction is a feature introduced in version 3 of Bash.
{}
Block of code [curly brackets]. Also referred to as an inline group, this construct, in effect, creates
an anonymous function (a function without a name). However, unlike in a "standard" function, the
variables inside a code block remain visible to the remainder of the script.
bash$ { local a;
a=123; }
bash: local: can only be used in a
function
a=123
{ a=321; }
echo "a = $a" # a = 321 (value inside code block)
# Thanks, S.C.
The code block enclosed in braces may have I/O redirected to and from it.
Example 3-1. Code blocks and I/O redirection
#!/bin/bash
# Reading lines in /etc/fstab.
File=/etc/fstab
{
read line1
read line2
} < $File
echo "First line in $File is:"
echo "$line1"
echo
echo "Second line in $File is:"
echo "$line2"
Advanced Bash-Scripting Guide
Chapter 3. Special Characters 14
剩余863页未读,继续阅读
showyouall
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 计算机人脸表情动画技术发展综述
- 关系数据库的关键字搜索技术综述:模型、架构与未来趋势
- 迭代自适应逆滤波在语音情感识别中的应用
- 概念知识树在旅游领域智能分析中的应用
- 构建is-a层次与OWL本体集成:理论与算法
- 基于语义元的相似度计算方法研究:改进与有效性验证
- 网格梯度多密度聚类算法:去噪与高效聚类
- 网格服务工作流动态调度算法PGSWA研究
- 突发事件连锁反应网络模型与应急预警分析
- BA网络上的病毒营销与网站推广仿真研究
- 离散HSMM故障预测模型:有效提升系统状态预测
- 煤矿安全评价:信息融合与可拓理论的应用
- 多维度Petri网工作流模型MD_WFN:统一建模与应用研究
- 面向过程追踪的知识安全描述方法
- 基于收益的软件过程资源调度优化策略
- 多核环境下基于数据流Java的Web服务器优化实现提升性能
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功