第一章:shell基础
●umask --查看当前用户建立文件或文件夹时的默认权限
eg:
[test@szbirdora 1]$umask
0002
[test@szbirdora 1]$ls -lh
-rw-rw-r-- test test myfile
drwxrwxr-x test test 1
上面的例子中咱们看到由test默认建立的文件myfile和文件夹1的权限分别为664,775.而经过umask查到的默认权限为002.因此能够推断出umask的计算算法为:
umask file directory
0 6 7
1 5 6
2 4 5
3 3 4
4 2 3
5 1 2
6 0 1
7 0 0
●链接ln
硬链接 ln sourcefile targetfile 链接后的target文件大小和source文件同样
软链接 ln -s sourcefile targetfile 相似于windows的快捷方式
●shell script 基本结构
#!/bin/bash --------bash shell开头必须部分
# description --------注释部分(无关紧要,为了阅读方便最好加以说明)
variable name=value ---------变量部分,声明变量,赋值
control segment ---------流程控制结构,如判断、循环、顺序
eg.
helloworld.sh
#! /bin/bash
# This is a helloworld shell script
printchar = "hello world"
echo $printchar
[test@szbirdora 1]$sh helloworld.sh
hello world
●shell 特性
①别名 alias eg. alias ll = “ls -l”
②管道 a |b 将a命令的输出做为b命令的输入 eg. ls |sort 将ls列举的项排序
③命令替换 a `b` 将b命令的输出做为a命令的输入 eg. ls `cat myfile` 列举出cat myfile的输出项
④后台运行 nohup command& 可经过jobs -l查看后台运行的脚本
⑤重定向 >,< 能够改变程序运行的输出来源和输入来源
⑥变量 能够用$varname 来调用变量
⑦特殊字符
`用来替换命令
\用来使shell没法认出其后的特殊字符,使其失去特殊含义
;容许一行放多个命令
() 建立成组的命令 ??
{} 建立命令块 ??
第二章:变量和运算符
●本地变量:在用户如今的shell生命期的脚本中使用。设置变量:various_name=value.可用set 来查看。用readonly可使变量只读。
●环境变量:用于当前用户下全部用户进程(不限于如今的shell)。
设置变量:export various_name=value。用env查看。
用readonly可使变量只读。
●变量替换
echo ${variable name} 显示实际值到variable name
echo ${variable name:+value} 若是设置了variable name,则显示其值,不然为空
echo ${variable name:?value} 若是未设置variable name,则显现用户定义错误信息value
echo ${variable name:-value} 若是未设置,则显示其值
echo ${variable name:=value} 若是未设置,则设置其值,并显示
●清除变量 unset variable name
●位置变量
位置变量表示$0,$1,$2...$9
$0 ----脚本名字
$1 ----根据参数位置表示参数1
eg.
#! /bin/bash
#parm.sh
echo "This is script name : $0"
echo "This is parameter 1: $1"
echo "This is parameter 2: $2"
[test@szbirdora 1]$sh parm.sh a b
This is script name : parm.sh
This is parameter 1: a
This is parameter 2: b
●向系统中传递位置变量
#!/bin/bash
#parm.sh
find /u01/test/1 -name $1 -print
[test@szbirdora 1]$ sh parm.sh myfile
/u01/test/1/myfile
●标准变量 bash默认创建了一些标准环境变量,可在/etc/profile中定义
EXINIT
HOME
IFS
LOGNAME --当前登陆用户名
MAIL
MAILPATH
PATH
TERM --终端信息
TZ --时区
PS1 --登陆提示,如[test@szbirdora 1]$
[test@szbirdora 1]$ echo $PS1
[\u@\h \W]\$ --\u -user --\h -host --\W -document
PS2 --一命令多行,换行提示,如>
PWD --当前目录
MAILCHECK --每隔多少秒检查是否有新邮件
[test@szbirdora 1]$ echo $MAILCHECK
60
SHELL
MANPATH --帮助文档位置
TERMINFO --终端信息
●特殊变量
$# 传递到脚本的参数个数
$* 以一个单字符串显示全部向脚本传递的参数,与位置变量不一样,参数可超过9个
$$ 脚本运行的当前进程ID号
$! 后台运行的最后一个进程的进程ID号
$@ 传递到脚本的参数列表,并在引号中返回每一个参数
$- 显示shell使用的当前选项,与set命令功能相同
$? 显示最后命令的退出状态,0表示没有错误,其余表示有错误
eg.
#!/bin/bash
#parm
echo "this is shellname: $0"
echo "this is parm1 : $1"
echo "this is parm2 : $2"
echo "show parm number : $#"
echo "show parm list : $*"
echo "show process id: $$"
echo "show precomm stat: $?"
[test@szbirdora 1]$ sh parm.sh a b
this is shellname: parm.sh
this is parm1 : a
this is parm2 : b
show parm number : 2
show parm list : a b
show process id: 24544
show precomm stat: 0
●影响变量的命令
declare 设置或显示变量
-f 只显示函数名
-r 建立只读变量
-x 建立转出变量
-i 建立整数变量
使用+替代-,能够颠倒选项的含义
export
-p 显示所有全局变量
shift[n] 移动位置变量,调整位置变量,使$3赋予$2,使$2赋予$1 n 前移n
typeset 和declare同义
注意:双引号不能解析$,\,`三个字符,因此在双引号中能够引用变量、转义字符、替换变量
单引号能够解析,因此单引号中引用变量等无效
[test@szbirdora 1]$ echo "$test"
test
[test@szbirdora 1]$ echo '$test'
$test
算法
●运算符类型
⒈按位运算符
~ 取反
<< 左移运算符
>> 右移运算符
& 与
| 或
^ 异或
$[ ] 表示形式告诉shell对方括号中表达式求值 $[a+b]
2.逻辑运算符
&&
||
>,<,=,!=
3.赋值运算符
let variablename1 +=variablename1+ varablename2shell