Shell学习好网站:GNU 、Bash-Hackers LDP(Advanced Bash-Scripting Guide)
1, set用法
set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
Any arguments remaining after the options are processed are treated as values for the positional parameters and are assigned, in order, to $1, $2, ... $n.
通过set来设置 位置变量 为命令的输出,示例:
$ date
Mon Jan 21 11:19 CST 2002
$ set `date`
$ echo $4
11:19
2, 可以从管道读一个命令的输出吗?
One common pitfall is trying to read output piped from commands, such as:
foo | IFS= read varPOSIX allows any or all commands in a pipeline to be run in subshells, and which command (if any) runs in themain shell varies greatly between implementations — in particular Bash and ksh differ here. The standard idiom for overcoming this problem is to use a here document:
IFS= read var << EOF $(foo) EOF
3,Here doc 和here string
示例1 here doc:
$ cat << EOF > Working dir $PWD > EOF Working dir /home/user
示例2 here-string:
$ tr a-z A-Z <<<"Yes it is a string" YES IT IS A STRINGHere document : http://en.wikipedia.org/wiki/Here_document http://bash.cyberciti.biz/guide/Here_strings
4, source,exec和 shell scrip 差别
exec [-cl] [-a name] [command [arguments]]
If command is specified, it replaces the shell. No new process is created.
If command is not specified, any redirections take effect in the current shell, and the return status is 0. If there is a redirection error, the return status is 1.
即exec命令能够在不启动新进程的前提下,将当前正在运行的程序替换为一个新的程序。
source filename [arguments]
Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.那么exec与source的区别是什么呢?
exec命令在执行时会把当前的shell process关闭,然后换到后面的命令继续执行。http://mindream.wang.blog.163.com/blog/static/2325122220084624318692/
"source FILE"
相同“. FILE
” : include a script file
5, exec重定向
exec <filename command redirects stdin to a file
exec >filename command redirects stdout to a file
n<&- # Close input file descriptor n
n>&- # Close output file descriptor n.
i>&j # Redirects file descriptor i to j.
# All output of file pointed to by i gets sent to file pointed to by j.
[j]<>filename # Open file "filename" for reading and writing, #+ and assign file descriptor "j" to it. # If "filename" does not exist, create it. # If file descriptor "j" is not specified, default to fd 0, stdin.
6,管道与重定向
Each command in a pipeline(管道) is executed as a separate process (i.e., in a subshell).(man bash)
管道的两边都是命令;重定向的一侧是命令,另一侧是文件(或者文件描述符)
管道触发两个子进程执行"|"两边的程序;而重定向是在一个进程内执行。
作为子进程的运行的管道,不能够改变脚本的变量.
例如:
[tonystz@li381-248 ~]$ echo a|read b;echo $b
[tonystz@li381-248 ~]$ echo a|(read b;echo $b)
a
7,Process Substitution进程替换
基本格式:用圆括号扩起来的命令
>(command list) #写文件,以文件的形式 提供command list的输入
<(command list) #读文件,以文件的形式 获得command list的输出
Process substitution is supported on systems that support named pipes (FIFOs) or the /dev/fd method of naming open files. It takes the form of <(list) or >(list). The process list is run with its input or outputconnected to a FIFO or some file in /dev/fd. The name of this file is passed as an argument to the current command as the result of the expansion. If the >(list) form is used, writing to the file will provide input for list. If the <(list) form is used, the file passed as an argument should be read to obtain the output of list.
8,shell中的变量作用域
1) 同一个shell中:默认所有变量包括shell函数中的变量都是全局的。 By default all variables are global;针对同一个shell中的可见性
2) local变量: local can only be used in a function。it causes the variable name to have a visible scope restricted to that function and its children.
3) 导出变量(export变量):从这一刻开始的的所有子shell都可以看到这个变量;针对不同的(子)shell中的可见性
4)导出变量的另一种形式:
variable_name=value command
等价于:
(variable_name=value; export name; command)
9,变量扩展操作Parameter Expansion (man sh)
${parameter:-default} :如果没有或者空,则使用该值;Use Default Values. If parameter is unset or null, the expansion of default is substituted.
${parameter:=default} :如果没有或者空,则赋值;Assign Default Values. If parameter is unset or null, the expansion of default is assigned to parameter.
${parameter:offset:length} :子字符串操作;Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset.
${#parameter} :求字符串长度;The length in characters of the value of parameter is substituted. If parameter is * or @, the value substituted is the number of positional parameters. If parameter is an array name subscripted by * or @, the value substituted is the number of elements in the array.
变量/字符串替换操:分为以下几种。如果Replacement为空,就是删除操作。Pattern前面的/,#,%类似正则表达式里面的 g , ^, $,即 全局匹配、匹配开始、匹配结尾
1)${var/ Pattern/Replacement} : First match of Pattern, within var replaced with Replacement.
2)${var //Pattern/Replacement} : Global replacement. All matches of Pattern, within var replaced with Replacement.
3)${var/ #Pattern/Replacement} : If prefix of var matches Pattern, then substitute Replacement for Pattern.
4) ${var/ %Pattern/Replacement} : If suffix of var matches Pattern, then substitute Replacement for Pattern
10,find命令用法(来自man手册)
基本形式:find [-H] [-L] [-P] [path...] [expression]
注意find的选项只有3个,其余的均为表达式expression,既然是表达式,那么表达式就有运算操作EXPRESSIONS:The expression is made up of options。All options always return true 。表达式:为了区别之前的选项,这里我们叫表达式选项,所有的表达式选项都返回true。
表达式可进行的操作,基本都输布尔运算:
( expr ) : Force precedence.强制优先级
! expr : True if expr is false. 取反操作
expr1 expr2 或者 expr1 -a expr2:与操作
expr1 -o expr2 :或操作
expr1 , expr2 : 逗号操作,和C里面的逗号操作很像。2个表达式都会被计算,然后返回expr2的值
11,总结Shell Expansion
- brace expansion:
- tilde expansion:
- parameter expansion: ${parameter}
- command substitution: $(command) or `command`
- arithmetic expansion: $(( expression ))
- word splitting: Internal Field Separator $IFS 环境变量
- filename expansion:
- process substitution: <(command) or >(command) 有的系统支持;从命令读取或者写入命令
1),Word splitting: 用双引号引用时,不发生word splitting。
The IFS variable holds the characters that Bash sees as word boundaries in this step. The default contains the characters: ”<space><tab><newline>”. These characters are also assumed when IFS isunset. 即使没有这个环境变量也起作用
Word splitting occurs once any of the following expansions are done (and only then!)
Bash will scan the results of these expansions for special IFS characters that mark word boundaries.This is only done on results that are not double-quoted!
2), parameter expansion
The basic form of parameter expansion is ${parameter} , other form is ${parameter:-word}, ${parameter:=word}, ${parameter:?word}, ${parameter:+word}, ${parameter:offset}, ${parameter:offset:length}等等
3), arithmetic expansion 查看详细的运算符 此方法只能计算整数
The format for arithmetic expansion is:
$(( expression )) 或 ((expression)) ;等同于 let
Evaluation is done in fixed-width integers with no check for overflow, though division by0 is trapped and flagged as an error. The operators and their precedence,associativity, and values are the same as inthe C language.
((expression)) :The expression is evaluated according to the rules described below underARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".
13,test, [ and [[的区别
"[ "和test相同,属于命令。[[是改进版,属于关键字。 [[ 不会做Word splitting 和 filename expansion
15,Grouping commands组合命令()和{}、函数
()组合命令在子shell中执行;{}只是简单的组合命令,结尾必须是换行或者分号.
如:( echo "This is a Test1") && { echo "This is a Test2"; }
shell函数:可以想象成带名字的{}命令组合;另外函数调用和一般的命令是一样的;因此有单行函数: fun () { echo "This is a function"; echo; }
16, 测试shell变量为空if [ "x$variable" = "x" ]; then ...
17, shell调试
set -x 打印调试
set -e 假如某个语句返回非真0则退出 Every script you write should include set -e at the top. This tells bash that it should exit the script if any statement returns a non-true return value
打开和关闭选项: using -X and switched off using +X;如打开调试 set -x,关闭调试set +x;打开错误检查set -e,关闭错误检查 set +e
18,shell进程和子shell
变量$$:打印当前shell的PID;如果在()子shell中,还是打印当前的shell PID,而不是子shell的PID;PPID打印父shell进程的ID
subshell:一个独立的进程,可以完成并发任务;
19,SSH自动输入密码登录
#!/bin/sh
pwd='uw+De6qN'
pwdf=$(mktemp)
echo "echo $pwd" > $pwdf
chmod a+x $pwdf
export SSH_ASKPASS=$pwdf
[ "$DISPLAY" ] || export DISPLAY; DISPLAY=
setsid ssh "root"@"10.64.71.8"
rm -rf $pwdf
20,持续更新中……
shell trick site: http://www.etalabs.net/sh_tricks.html
Bash Hackers Wiki Frontpage: http://wiki.bash-hackers.org/start