Advanced Bash-Scripting Guide 第九章至结束

2010年12月24日 星期五

/abs-guide/declareref.html

1、
declare -i n # 用 declare 声明了的整数不用 let 或 expr 都可以进行一个算术运算 n=6/3 #n=2

2、
疑问:
foo(){ FOO="bar";echo $FOO;};foo #{后面必须跟一个空格,不然会出错,为什么?

3、
foo(){ FOO="bar1";};bar(){ foo;echo $FOO;};bar #函数foo的变量FOO可以被调用它的函数bar所引用
疑问:
foo(){ declare FOO="bar1";};bar(){ foo;echo $FOO;};bar #在命令行仍可以打印出$FOO,但是放在脚本文件中执行就不行了,为什么?

/abs-guide/randomvar.html

1、
Bash 内部函数 $RANDOM 的范围:带符号16位整数 0~32767 二进制表示 $((2#0111111111111111))(15个1,第16位0应该是符号位表示非负数

所以后面的例子中只右移14位,得到00或01两种结果:
$RANDOM
let "number >>= 14")

2、
Example 9-12. Picking a random card from a deck

Suites="Clubs
Diamonds
Hearts
Spades" 字符串以换行符分开为多个元素

suite=($Suites) #数组的形式(a b c),这里分隔符应该可以换成上面的换行符,但为什么上面不用空格分开而用换行符呢?

num_suites=${#suite[*]} # 计算数组元素个数

2010年12月25日 星期六
时间关系,尽快完成浏览 Advanced Bash-scripting Guide 6.2,知道 Bash 脚本能做什么,有哪些关键的用法,到时用时能找到在什么地方。
/abs-guide/parameter-substitution.html

1、
${parameter-default}, ${parameter:-default}
If parameter not set, use default.

2、
${#var}
String length (number of characters in $var). For an array, ${#array} is the length of the first element in the array.
Note
Exceptions:
${#*} and ${#@} give the number of positional parameters.
For an array, ${#array[*]} and ${#array[@]} give the number of elements in the array.

3、
${var#Pattern}, ${var##Pattern}
${var#Pattern} Remove from $var the shortest part of $Pattern that matches the front end of $var.
${var##Pattern} Remove from $var the longest part of $Pattern that matches the front end of $var.

/abs-guide/loops.html

1、
cat $filename | # Supply input from a file.
while read line # As long as there is another line to read ...
do
...
done

/abs-guide/nestedloops.html

/abs-guide/loopcontrol.html
Commands affecting loop behavior
break, continue

1、
A plain break terminates only the innermost loop in which it is embedded, but a break N breaks out of N levels of loop.

2、
A continue N terminates all remaining iterations at its loop level and continues with the next iteration at the loop, N levels above.

/abs-guide/testbranch.html
1、
select

The select construct, adopted from the Korn Shell, is yet another tool for building menus.

select variable [in list]
do
command...
break
done

/abs-guide/commandsub.html
Chapter 12. Command Substitution

1、sThe classic form of command substitution uses backquotes (`...`). Commands within backquotes (backticks) generate command-line text.
script_name=`basename $0`

2、
The $(...) form has superseded backticks for command substitution.
output=$(sed -n /"$1"/p $file) # From "grp.sh" example.

/abs-guide/arithexp.html
Chapter 13. Arithmetic Expansion
Arithmetic expansion provides a powerful tool for performing (integer) arithmetic operations in scripts. Translating a string into a numerical expression is relatively straightforward using backticks, double parentheses, or let.

1、z=`expr $z + 3`
2、z=$(($z+3))
3、let "z += 3"

/abs-guide/part4.html
Part 4. Commands
各种命令的简介

/abs-guide/part5.html
Part 5. Advanced Topics
包括:正则表达式、重定向、子脚本、函数等一些较高级的内容。

其中一些关注的东西:
1、/abs-guide/assortedtips.html Shell 脚本的图形化界面
35.7.2. Widgets
Example 35-20. Widgets invoked from a shell script
dialog.sh: Using 'gdialog' widgets.

2、/abs-guide/securityissues.html
35.8.2. Hiding Shell Script Source 把 Bash 脚本编译成二进制文件,隐藏源码
http://www.datsi.fi.upm.es/~frosal/sources/

3、/abs-guide/winscript.html
35.10. Shell Scripting Under Windows 可以把 Bash 脚本移植进 Windows 下执行
The Cygwin package from Cygnus and the MKS utilities from Mortice Kern Associates add shell scripting capabilities to Windows.

/abs-guide/aboutauthor.html
The author claims no credentials or special qualifications, [1] other than a compulsion to write. [2] This book is somewhat of a departure from his other major work, HOW-2 Meet Women: The Shy Man's Guide to Relationships. He has also written the Software-Building HOWTO. Of late, he has been trying his (heavy) hand at short fiction.
从这里得知,作者称除了写作的冲动,没有文凭和专业资格证。而且他的主要工作不是做这个的。应该是个业余作品吧?

而且难得的是:作者从2000年开始,接连十多年的更新令人钦佩。所以,就算不是专业人士,一样可以做点有用的东西的。要向作者学习。

/abs-guide/copyright.html
这本书的3.9.1 版已经被翻译成了中文, http://www.linuxsir.org/bbs/thread256887.html http://www.linuxsir.org/main/doc/abs/abs-3.9.1_cn.tar.bz2

这本书可以很方便的作为工具书来查询:
/abs-guide/refcards.html
Appendix B. Reference Cards
The following reference cards provide a useful summary of certain scripting concepts. The foregoing text treats these matters in more depth, as well as giving usage examples.

/abs-guide/xrefindex.html
Index
This index / glossary / quick-reference lists many of the important topics covered in the text. Terms are arranged in approximate ASCII sorting order, modified as necessary for enhanced clarity.

/abs-guide/part4.html
Part 4. Commands
Mastering the commands on your Linux machine is an indispensable prelude to writing effective shell scripts.