没有合适的资源?快使用搜索试试~ 我知道了~
Advanced Bash-Scripting Guide 读书笔记
4星 · 超过85%的资源 需积分: 10 31 下载量 189 浏览量
2013-07-17
02:10:58
上传
评论
收藏 765KB PDF 举报
温馨提示
试读
173页
看Advanced Bash-Scripting Guide这本书做的一些笔记,将书中一些比较主要的东西提取出来了,看完这本书之后用来复习还是很有用的。
资源推荐
资源详情
资源评论
08/14/12 Edited by: xuzhezhao Last edited: 09/05/12
Advanced Bash-Scripting Guide
学习笔记
Specia Characters
1. 分号(colon)代表空命令(null command)
• 类似于 shell 内置的 true 功能,执行后的 exit 值是 0,即$?=0。
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).
• Endless loop
while :
do
operation-1
operation-2
...
operation-n
done
# Same as:
while true
do
...
done
• 在 if/then 结构中作为占位符(Placeholder in if/then test)
if condition
then : # 注意“:”前面有空格,不加分号的话会产生语法错误
1
# Do nothing and branch ahead
else
# Or else ...
take-some-action
fi
• 作为命令占位符(Provide a placeholder where a binary operation is
expected)
: ${username=`whoami`}
#可以用分号预留一个命令的位置
# ${username=`whoami`}
# Gives an error without the leading :
#+ unless "username" is a command or builtin...
• Evaluate string of variables using parameter substitution
: ${HOSTNAME?} ${USER?} ${MAIL?}
# Prints error message
#+ if one or more of essential environmental variables not set.
• 配合重定向,将文件变成空文件(truncates a file to zero length)
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.
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.
• 还有一种不建议的用法,用作注释(May be used to begin a comment line,
although this is not recommended)
2
: This is a comment that generates an error, ( if [ $x -eq 3] ).
2. !(bang):取反 reverse (or negate) the sense of a test or exit
status
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.
3. *(asterisk):通配符(wild card)、乘号(multiplication)
在正则表达式(regular expression)中代表 0 个或任意多个前一个字符。
**:取幂运算符,或者是 extended file-match globbing.
4. ?:test operator
In a double-parentheses construct(双括号结构中), the ? can serve as an element
of a C-style trinary operator(三目运算符).
condition?result-if-true:result-if-false
(( 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.
5. $:变量代换 Variable substitution (contents of a variable).
• $var:显示 var 内容
• $:正则表达式的结尾标志。end-of-line. In a regular expression, a "$" addresses the
end of a line of text.
3
• ${ }:参数代换(Parameter substitution).
• $' … ':将 ascii 码转成字符形式,如 echo $'\111'。Quoted string expansion. This
construct expands single or multiple escaped octal or hex values into
ASCII [16] or Unicode characters.
• $*,$@:positional parameters.
• $?:上一命令的返回值。exit status variable. The $? variable holds the exit status of
a command, a function, or of the script
itself.
• $$:script 所在执行环境中的进程 ID,process ID variable.
The $$ variable holds the process ID of the script in which it appears.
6. ():命令组(command group)
(a=hello; echo $a)
括号中的命令是在子 shell 中执行(A listing of commands within parentheses starts a
subshell),子 shell 要的变量在父 shell 中不可见。
数组初始化(array initialization)
Array=(element1 element2 element3)
echo ${Array[0]}
; echo ${Array[1]}; echo ${Array[2]}
7. { }:Brace expansion
echo \"{These,words,are,quoted}\"
# "These" "words" "are" "quoted"
# " prefix and suffix
cat {file1,file2,file3} > combined_file
# Concatenates the files file1, file2, and file3 into combined_file.
cp file22.{txt,backup}
4
# Copies "file22.txt" to "file22.backup"
注意:No spaces allowed within the braces unless the spaces are quoted or escaped.
echo {file1,file2}\ :{\ A," B",' 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.
base64_charset=( {A..Z} {a..z} {0..9} + / = )
# Initializing an array, using extended brace expansion.
# From vladz's "base64.sh" example script.
• { }: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.
a=123
{ a=321; } #大括号内的命令必须要以分号或者换行来结束
echo "a = $a"
# a = 321
(value inside code block)
# Thanks, S.C.
• 重定向 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
5
剩余172页未读,继续阅读
资源评论
- 滨野明日香2014-06-25有很好的辅助作用
xuzhezhaozhao
- 粉丝: 351
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功