03linuxshell专题讲座王保明.docx

上传人:b****5 文档编号:4445542 上传时间:2022-12-01 格式:DOCX 页数:56 大小:496.13KB
下载 相关 举报
03linuxshell专题讲座王保明.docx_第1页
第1页 / 共56页
03linuxshell专题讲座王保明.docx_第2页
第2页 / 共56页
03linuxshell专题讲座王保明.docx_第3页
第3页 / 共56页
03linuxshell专题讲座王保明.docx_第4页
第4页 / 共56页
03linuxshell专题讲座王保明.docx_第5页
第5页 / 共56页
点击查看更多>>
下载资源
资源描述

03linuxshell专题讲座王保明.docx

《03linuxshell专题讲座王保明.docx》由会员分享,可在线阅读,更多相关《03linuxshell专题讲座王保明.docx(56页珍藏版)》请在冰豆网上搜索。

03linuxshell专题讲座王保明.docx

03linuxshell专题讲座王保明

LinuxShell应用编程专题讲座

writtenby王保明

第一部分shell编程基础

1shell简介

什么是shell

存取权限和安全

shell简单脚本

shell特性

1.1什么是shell

●shell是核心程序kernel之外的指令解析器,是一个程序,同时是一种命令语言和程序设计语言。

✧shell是命令解析器,用户输入命令,它去解析。

●shell类型ash、bash、ksh、csh、tcsh

✧cat/etc/shells看系统下的shell

✧echo$SHELL看当前用户运行的shell

●程序在shell中运行

✧ls命令执行过程分析

●shell中可以运行子shell

✧/bin/csh退出子shell

●linux下默认的shell是bash

✧bash特点,快速(上下键);tab键盘自动补齐;自动帮助功能help

在shell下执行help命令,可以查看shell提供的命令

[test@localhost~]$help

GNUbash,version3.2.25

(1)-release(x86_64-redhat-linux-gnu)

Theseshellcommandsaredefinedinternally.Type`help'toseethislist.

Type`helpname'tofindoutmoreaboutthefunction`name'.

Use`infobash'tofindoutmoreabouttheshellingeneral.

Use`man-k'or`info'tofindoutmoreaboutcommandsnotinthislist.

Astar(*)nexttoanamemeansthatthecommandisdisabled.

JOB_SPEC[&]((expression))

.filename[arguments]:

[arg...][[expression]]

alias[-p][name[=value]...]bg[job_spec...]

bind[-lpvsPVS][-mkeymap][-ffibreak[n]

builtin[shell-builtin[arg...]]caller[EXPR]

caseWORDin[PATTERN[|PATTERN].cd[-L|-P][dir]

command[-pVv]command[arg...]compgen[-abcdefgjksuv][-ooption

complete[-abcdefgjksuv][-pr][-ocontinue[n]

declare[-afFirtx][-p][name[=valdirs[-clpv][+N][-N]

disown[-h][-ar][jobspec...]echo[-neE][arg...]

enable[-pnds][-a][-ffilename]eval[arg...]

exec[-cl][-aname]file[redirecexit[n]

export[-nf][name[=value]...]orfalse

fc[-eename][-nlr][first][lastfg[job_spec]

forNAME[inWORDS...;]doCOMMAfor((exp1;exp2;exp3));doCOM

functionNAME{COMMANDS;}orNAgetoptsoptstringname[arg]

hash[-lr][-ppathname][-dt][nahelp[-s][pattern...]

history[-c][-doffset][n]orhiifCOMMANDS;thenCOMMANDS;[elif

jobs[-lnprs][jobspec...]orjobkill[-ssigspec|-nsignum|-si

letarg[arg...]localname[=value]...

logoutpopd[+N|-N][-n]

printf[-vvar]format[arguments]pushd[dir|+N|-N][-n]

pwd[-LP]read[-ers][-ufd][-ttimeout][

readonly[-af][name[=value]...]return[n]

selectNAME[inWORDS...;]doCOset[--abefhkmnptuvxBCHP][-oopti

shift[n]shopt[-pqsu][-olong-option]opt

sourcefilename[arguments]suspend[-f]

test[expr]time[-p]PIPELINE

timestrap[-lp][argsignal_spec...]

truetype[-afptP]name[name...]

typeset[-afFirtx][-p]name[=valuulimit[-SHacdfilmnpqstuvx][limit

umask[-p][-S][mode]unalias[-a]name[name...]

unset[-f][-v][name...]untilCOMMANDS;doCOMMANDS;done

variables-Somevariablenamesanwait[n]

whileCOMMANDS;doCOMMANDS;done{COMMANDS;}

1.2存取权限与安全

简介:

文件和目录的权限(-rwxr—r--)

setuid(suid/guid)(chmodu+sg+sfile)

chown和chgrp(chownuserfile/chgrpgroupfile)

umask(umasknnn)(文件创建时的缺省权限位)

●文件和目录的权限(-rwxr--r--)

✧linux下安全解决方案很多,现在讨论文件和目录的访问权限

✧练习ls–lh

文件的权限硬链接数用户名用户组文件大小、最近修改时间文件名称

drwxr-xr-x2testtest4096Jun2308:

07cppsocket1

drwxr-xr-x8testtest4096Jun2308:

08cppsocket2

drwxr-xr-x9testtest4096Jun1812:

32gcc

-rw-r--r--1testtest59827Jun2308:

11gcc_mk_gdb.tar.gz

drwxr-xr-x7testtest4096Jun2308:

09mk

drwxr-xr-x4testtest4096Jun2309:

47mygcc

drwxr-xr-x5testtest4096Jun2311:

13mymk

-rw-r--r--1rootroot63Nov92013oraInst.loc

-rw-rw-r--1oracleoinstall732Nov92013oratab

drwxr-xr-x2rootroot4096Nov92013pam.d

drwxr-xr-x3rootroot4096Nov82013racoon

lrwxrwxrwx1rootroot7Nov82013rc->rc.d/rc

drwxr-xr-x10rootroot4096Nov82013rc.d

硬链接数?

d-目录–一般文件–l快捷方式–c字符设备–b块设备s-socket设备文件–p管道文件

-rw-r--r--1rootroot5Jun2312:

39scim-bridge-0.3.0.lockfile-0@localhost:

0.0

-rw-r--r--1oracleoinstall5Jun1816:

13scim-bridge-0.3.0.lockfile-501@localhost:

0.0

srwxr-xr-x1rootroot0Jun2305:

39scim-bridge-0.3.0.socket-0@localhost:

0.0

srwxr-xr-x1oracleoinstall0Jun1807:

13scim-bridge-0.3.0.socket-501@localhost:

0.0

srw-------1oracleoinstall0Jun1807:

13scim-helper-manager-socket-oracle

srw-------1rootroot0Jun2305:

39scim-helper-manager-socket-root

srw-------1oracleoinstall0Jun1807:

13scim-panel-socket:

0-oracle

srw-------1rootroot0Jun2305:

39scim-panel-socket:

0-root

srw-------1oracleoinstall0Jun1807:

13scim-socket-frontend-oracle

srw-------1rootroot0Jun2305:

39scim-socket-frontend-root

drwx------2rootroot4.0KJun2305:

39ssh-grWlnX5288

drwx------2oracleoinstall4.0KJun1807:

13virtual-oracle.26qtL1

drwx------2rootroot4.0KJun2305:

39virtual-root.selw4P

[test@localhost~]$ls-l/tmp/.X11-unix/X0

srwxrwxrwx1rootroot0Jun2220:

07/tmp/.X11-unix/X0

这里s为socket文件

[test@localhost~]$

●chmod改变文件或目录的权限位

manchmod或者infochmod

NAME

chmod–changefileaccesspermissions改变文件或者目录的权限位

SYNOPSIS

chmod[OPTION]…MODE[,MODE]…FILE…

chmod[OPTION]…OCTAL-MODEFILE…

chmod[OPTION]…–reference=RFILEFILE…

DESCRIPTION

chmod语法格式

chmod[who]operator[permission]filename

who(u,g,o,a)

operator(+,-=)

permission(r,w,x,s,t)

chmod有两种用法(Theformatofasymbolicmode)

1、数字方式

用户用户组其他全部增加去掉=读写执行s(具有超级用户)t(执行文件在

缓存)eg:

chmodu=rwx,g+w,o+rmyfile

注意:

S位存在的意义:

写一个程序,被执行是,临时拥有超级用户权限,执行完毕以后,有恢复普通身份,例如,写一个数据库脚本,这个数据库脚本具有超级管理员身份运行,运行完毕以后,恢复普通身份,避免破坏系统。

查看带有s位的应用程序,命令ls–l/bin|grep‘^…s’

[root@localhost01]#

[root@localhost01]#ls-l/bin|grep'^...s'

-rwsr-xr-x1rootroot61424Jul32009mount

-rwsr-xr-x1rootroot37312Apr242009ping

-rwsr-xr-x1rootroot32736Apr242009ping6

-rwsr-xr-x1rootroot28336Jul132009su

-rwsr-xr-x1rootroot41224Jul32009umount

[root@localhost01]#

[root@localhost01]#

2、chmod数字方式chmodmodefilechmod775file

rwx数字代表421

3、chownroot.testmyfile改变myfile的用户名、用户组

●setuid(suid/guid)(chmodu+s,g+sfile)

●chown和chgrp(chownusefile/chgrpgroupfile)

✧chown改变文件、目录所在的用户和用户组

✧chgrp改变文件、目录所在的用户和用户组

✧chown[-R]ownermyfile

✧chownowner.groupmyfile

✧chown.groupmyfile//注意有个点

✧chgrp[-R]groupmyfile

●umask(umasknnn)

✧熟悉了修改文件的权限位和用户组后,思考:

创建文件时,如何默认指定权限位那?

✧先运行umask命令,看看结果如何?

✧umask命令,用来指定,用户创建文件、目录,默认的权限位

✧问题抛出在0022umsk值下,文件默认权限位是644目录默认权限位是755

✧[root@localhostmytest]#umask

✧0022

✧[root@localhostmytest]#ls-lt

✧total4

✧drwxr-xr-x2rootroot4096Jun2321:

41dir

✧-rw-r--r--1rootroot0Jun2321:

41myfile.c

✧实验:

当umask为022,创建文件,权限位默认是:

644

当umask为077,创建目录,权限位默认是755

做实验,测试!

✧umask的秘密

✧umask文件目录

✧067

✧156

✧245

✧334

✧423

✧512

✧601

✧700

✧规律如果文件权限位=7-umask-1目录权限位=7-umask

用户用户组其他用户

755

rwxr-xr-x

022====》755

8421

rwxr-xr-x

✧ls–lda命令只查看目录

✧umask的配置/etc/profile($HOME/.profile$HONME/.bash_profile)

●文件的符合链接(ln[-s]source_pathtarget_path)

✧硬连接

✧软连接

[test@localhostshell]$ls

my.tar.gz

[test@localhostshell]$lnmy.tar.gzmy2.tar.gz建立一个硬连接

[test@localhostshell]$ln-smy.tar.gzkjfs建立一个软连接

[test@localhostshell]$ls-lt

total128

lrwxrwxrwx1testtest9Jun2322:

32kjfs->my.tar.gz

-rw-r--r--2testtest59827Jun2322:

30my.tar.gz

-rw-r--r--2testtest59827Jun2322:

30my2.tar.gz

[test@localhostshell]$

1.3shell脚本

●使用shell脚本的原因

✧功能强大

✧节约时间

●shell脚本基本元素

#!

/bin/bash

#shell脚本

mytext="helloworld"

echo$mytext;

注意初学者易犯错误:

1)第一行#不是注释其他#都是注释;

2)echo$mytextA;写错现象;

3)写错现象#!

bin/bash

4)脚本如果没有执行权限chmod11101hello.sh,如何办

5)chmodu+x01hello.shchmodu-x01hello.sh

●shell脚本运行方式

./01shell.sh$PATH

1.4shell特性

简介:

别名、管道、命令替换、重定向、后台处理、模式匹配、变量、特殊字符

●别名

⏹alias查看本用户下的alias配置

[test@localhost~]$alias

aliasl.='ls-d.*--color=tty'

aliasll='ls-l--color=tty'

aliasls='ls--color=tty'

aliasvi='vim'

aliaswhich='alias|/usr/bin/which--tty-only--read-alias--show-dot--show-tilde'

[test@localhost~]$

⏹自定义别名:

aliasll=‘ls-alh’

⏹aliasll=‘ls–I–color=tty’

⏹cat$HOME/.bashrc在这个用户下配置着alias命名的配置

●命令替换

⏹ls`catmyfile`–alh通过单反引号,把’catmyfile’的内容输出传给ls命名

⏹注意反单引号,在键盘的~符号的下方。

也就是在键盘左上角ESC键的下边

●后台处理

⏹什么是后台nohupcommond&

⏹一个终端可以同时运行多个程序

◆nohuptar–cvf111.tar.gz&可以同时运行多个大文件压缩

◆jobs–l可以查看后台正在运行的命令

●管道

⏹把一个命令的输出作为另外一个命令的输入

◆ls-l|sort;ls|sort

●重定向<输入>输出

⏹与管道相关,可以改变程序运行的输入来源和输出地点

⏹sort

⏹sortmyfile+sort.txt

●模式匹配

⏹显示以txt为扩展的文件或者显示以a开头的文件,这种能力叫做模式匹配

⏹正则表达式

●特殊字符

⏹双引号(“”):

用来使shell无法认出空格、制表符和其他大多数特殊字符,比如:

建立一个带空格的文件touch“wangbaoming”。

⏹单引号(‘):

用来使shell无法认出所有特殊字符。

⏹反引号(`):

用来替换命令

⏹反斜杠(\):

用来使shell无法认出的特殊字符,使其后的字符失去了特殊的含义,转义字符。

eg:

创建带空格文件名touchmy\file

⏹分号(;):

允许在一行上放多个命令。

mv1.txt2.txt;mv2.txt3.txt;

⏹&:

命令后台执行

⏹括号():

创建成组的命令

⏹大括号{}:

创建命令块。

⏹竖杠(|):

管道标示符

⏹<>&:

重定向表示符

⏹*?

[]!

表示模式匹配

⏹$:

变量名的开头

⏹#:

表示注释(第一行除外)

⏹空格、制表符、换行符:

当做空白

2变量和运算符

本地变量影响变量的命令

环境变量引号

变量替换运算符

位置变量表达式变量

标准shell变量运算符的优先级

特殊变量

2.1变量

简介:

什么是shell变量?

本地变量,环境变量,变量替换(显示变量),位置变量,标准变量,特殊变量,影响变量的命令。

●什么是shell的变量?

⏹为使用shell编程更加有效,系统提供了一些shell变量。

shell变量可以保存注诸如路径名、文件名、或者数字这样的变量。

从这一点上看,shell编程中,变量至关重要。

⏹eg:

echo$myVar1

●本地变量

⏹本地变量在用户现有的shell生命期的脚本中使用。

⏹variablename=value

◆定义本地变量:

MYVAR=”test”注意=左右不要有空格

◆使用本地变量$MYVARecho$MYVARorecho${MYVAR}

⏹set显示本地所有的变量

⏹readonlyvariablename

◆readonlymyvar1=”test2”;myvar1=”test2”;bash会报错误

[wbm@wmblinux64~]$readonly

declare-rBASHOPTS="checkwinsize:

cmdhist:

expand_aliases:

extquote:

force_fignore:

hostcomplete:

interactive_comments:

login_shell:

progcomp:

promptvars:

sourcepath"

declare-irBASHPID=""

declare-arBASH_VERSINFO='([0]="4"[1]="1"[2]="2"[3]="1"[4]="release"[5]="x86_64-redhat-linux-gnu")'

declare-irEUID="500"

declare-rMYYY="test2"

declare-irPPID="7140"

declare-rSHELLOPTS="braceexpand:

emacs:

hashall:

histexpand:

history:

interactive-comments:

monitor"

declare-irUID="500"

⏹实验

◆[wbm@wmblinux64myshell]$set|grep"M

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1