Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31508
  • 博文数量: 24
  • 博客积分: 1080
  • 博客等级: 少尉
  • 技术积分: 280
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-26 18:11
文章分类

全部博文(24)

文章存档

2011年(1)

2009年(23)

我的朋友
最近访客

分类: LINUX

2009-05-31 12:01:58

第一章 shell 简介

-什么是shell
-存取权限和安全
-shell简单脚本
-shell特性

1.1  什么是shell
 
  -shell是核心程序(kernel)之外的指令解析器,是一个程序,同时是一种命令语言和程序设计语言。
 -shell的类型ash、bash、ksh、csh、tcsh
  -/etc/shells
  -/echo $SHELL
 -程序在shell中运行
 -shell中可运行子shell

#ls
#cat /etc/shells
#echo $SHELL 显示当前的shell
#/bin/csh 进入到csh
#exit  退出当前shell
#ls  两个tab键,帮助功能,补全以ls开头的命令
#help   显示shell命令
1.2  存取权限与安全
 -文件和名录的权限(-rwxr--r--)
 -setuid(suid/guid)(chmod u+s,g+s file)
 -chown和chgrp(chown user file/chgrp group file)
 -umask (umask nnn)
 -符号链接(ln [-s] source_path target_path)

#ls -l  显示文件权限及空间
#ls -lh  注意h的区别
  -、d、l、b(块文件)、c(字符文件)、p(管道文件)、s(socket文件 如:ls -l /tmp/.X11-unix/XO)

 chmod [who] operator [permission] filename
 - who (u,g,o,a)
 - operator (+,-,=)
 - permission (r,w,x,s,t)
#mkdir testfile
#chmod u=rwx,g+w,o+r testfile
#ls -l
#chmod u+s testfile  s位涉及安全问题
#chmod g+x,o+x testfile  这个文件所属的组或其它用户会获得属主的权限
#chown root.antiy testfile         s位:其它用户会拥有root的权限
#ls -l
#chmod g+s testfile  这个文件所属的组或其它用户会获得属组的权限
#ls -l
#chmod o+t testfile  运行时放到swap里
#ls -l
#ls -l /bin | grep '^...s'
s位有安全隐患,但为什么还需要s位呢?

 chmod mode filename
 - mode
  r 4 w 2 x 1
 chmod 644 filename
 chmod 740 filename
#chmod 4744 filename
#chmod 6744 filename
#chmod 7744 filename

 chown和chgrp
 注意-R的作用,子目录也随之更改
#chown root testfile
#ls -l
#chown -R root testfile
#ls -l testfile
#chgrp antiy testfile
#chown root.antiy testfile -R
#ls -l

 umask 缺省权限
#umask
022
#touch filename
#ls -l filename
#mkdir direactory
#ls -lda direactory
 自己找一下umask的值与目录和文件的规律
#umask 000
#umask
000
#touch filename1
#ls -l filename1
#mkdir direactory1
ls -lda direactory1
#umask 022
 -注意安全性问题
 /etc/profile ($HOME/.profile
 $HOME/.bash_profile)
 -umask
#cat /etc/profile | grep "umask"
 
***********************************************************************************************************
 -符号链接
  -硬链接
  -软连接
  -ln [-s] source_path  target_path

 -shell教本
  -使用shell脚本的原因
   功能强大,节约时间。
  -shell脚本基本元素
   #!/bin/bash
   -第一行
   #
   -表示注释
   变量 
   流程控制结构
例子:
helloworld.sh
#!/bin/bash
#输出一个hell world的shell
printchar="hello world"
echo $printchar
echo $HOME
#chmod u+x helloworld.sh
#./helloworld.sh
 
  shell特性
  别名,命令替换,后台处理,变量,管道,重定向,
  模式匹配,特殊字符

 别名:
 -alias
 -alias ll='ls -alh'
#alias
#alias ll='ls -alh'
#alias ll='ls -l --color=tty'
#cat $HOME/.bashrc 每个用户自己定义的别名
 命令替换:
 myfile的内容: 
 parm
 findfile
#cat myfile
#ls `cat myfile` -al

 后台处理:
 -什么是后台?
 -一个终端可以同时运行多个程序
 -nohup command &
 
#nohup tar -czf enerco.tar.gz enerco &
#job -l  可以查看后台正在运行的程序

 变量:
 
 管道:
 -把一个命令的输出连接到另一个命令的输入
 -ls | sort
#ls -l | sort
#ls | sort
 
 重定向(< >):
 -与管道相关,可以改变程序运行的输入来源和输出地点
 -sort  -sort myfile_sort.txt

#vi myfile.txt
#sort < myfile.txt
#sort myfile_sort.txt 
 
 模式匹配:
 -显示以txt为扩展名的文件或显示以a开头的文件,这种能力就称为模式匹配
 -正则表达式
 
 特殊字符
 -双引号("):用来使shell无法认出空格、制表符和其他大多数特殊字符,如
"David Medinets"表示一个值,而不是2个。同样"David < Medinets"表示一个值。
 -单引号('):用来使shell无法认出所有特殊字符。
 -反引号(`):用来替换命令。
 -反斜杠(\):用来使shell无法认出其后特殊字符,使其后的字符失去了特殊的含义,
如:David\ Medinets
#touch David\ Medinets
#ls David\ Medinets
 
 -分号(;):如许在一行上放多个命令。
 -&:命令后台执行。
 -括号():创建成组命令。
 -大括号{}:创建命令块。
 -竖杠(|):管道表示符。
 -< >&:表示重定向。
 -* ? [] ! :表示模式匹配。
 -$:变量名的开头。
 -#:表示注释(第一行除外)。
 -空格,制表符,换行符:当作空白。
阅读(395) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~