Advanced Bash-Scripting Guide 第四至五章

2010年12月21日 星期二
/abs-guide/variables.html

/abs-guide/varsubn.html

Assignment may be with an = (as in var1=27), in a read statement, and at the head of a loop (for var2 in 1 2 3).

赋值注意事项:
1)、等号左右无空格
2)、$hello 是 {$hello} 的简化形式
3)、双引号之中的变量保留值中的空格

/abs-guide/varassignment.html
脚本中可以有 a=hello! ;终端命令行不行,解释!为历史命令

/abs-guide/untyped.html
任意字符串在算术运算中当作“0”;不可靠的运算:未初始化的字符串和空字符串在算术运算中当作“0”

/abs-guide/othertypesv.html
疑问: eval 用法
eval [arg ...]
The args are read and concatenated together into a single com‐
mand. This command is then read and executed by the shell, and
its exit status is returned as the value of eval. If there are
no args, or only null arguments, eval returns 0.

seq - print a sequence of numbers

export 命令注意事项:
1)、在脚本中export的变量不能被执行脚本的命令行环境使用
2)、子进程export的变量不能被其父进程使用

After $9, the arguments must be enclosed in brackets, for example, ${10}, ${11}, ${12}.

疑问:
args=$# # Number of args passed.
lastarg=${!args}
# Note: This is an *indirect reference* to $args ...

间接引用的意义及用法:/abs-guide/bashver2.html#VARREFNEW

疑问:
# A better method is parameter substitution:
# ${1:-$DefaultVal}
# See the "Parameter Substition" section
#+ in the "Variables Revisited" chapter.
${1:-$DefaultVal}的用法

/abs-guide/quoting.html
疑问:
在当前目录中有名为 first 的文件,为什么: grep [Ff]irst *.txt 会不正常工作而 grep "[Ff]irst" *.txt 工作正常?

echo "$(ls -l)" 引号可以保护换行符不被吞掉

weirdvars.sh
/abs-guide/quotingvar.html#WEIRDVARS
"" 会转义 $, ` (backquote), and \ (escape)
'' 会转义 '
echo 的结果会以 $IFS 定义的分隔符显示,但如果 echo 的参数是用""包含的,那么""内的分隔符将失去它作为分隔符的意义而不会作用于输出结果的显示。此例中的分隔符是 \ ,与转义字符 \ 是不同的意义。

疑问:
echo -e 启用 \ 的转义,但这与""会转义 \ 是不同的意义。如:echo "a\\b" 和 echo "a\tb" 对比 echo -e "a\\b" 和 echo -e "a\tb" 不是很明白,需要进一步理解

/abs-guide/escapingsection.html
疑问:
1)、
\ 的行为取决于是否出现在转义符后、引号的类型、是否在参数替代中、here 文档中。
Note

The behavior of \ depends on whether it is escaped, strong-quoted, weak-quoted, or appearing within command substitution or a here document.
太绕了,这里的例子把头都弄晕了,以后有需要的时候在详细了解,运用时尽量避开太生僻的用法(还包括那些所谓高技巧的用法),简单、兼容性对我很重要。

2)、
(cd /source/directory && tar cf - . ) | \
(cd /dest/directory && tar xpvf -)
如果不用括号就出错,是否括号的作用类似于运算优先级改变?