您的位置:首页 > 运维架构 > Shell

理解shell(第五章)

2017-06-23 14:47 211 查看
1. shell的类型
[root@localhost ~]# ls -l /bin/*sh
-rwxr-xr-x. 1 root root 938832 Jul 18 2013 /bin/bash
lrwxrwxrwx. 1 root root 4 Oct 27 2014 /bin/csh -> tcsh
-rwxr-xr-x. 1 root root 109672 Oct 17 2012 /bin/dash
lrwxrwxrwx. 1 root root 4 Oct 27 2014 /bin/sh -> bash
-rwxr-xr-x. 1 root root 387328 Feb 22 2013 /bin/tcsh
在centos6中运行上述命名可看到在centos中shell有bash、csh、dash。centos中终端默认启动是一般为bash。
在命令行模式输入bash命令,会生成一个子shell,使用exit命令退出子shell
2. shell的父子关系
shell进程是存在父子关系的,父shell可以通过bash命令创建子shell、子shell还可以通过bash命令再次创建子shell,通过ps -f命令可以看到父子shell进程之间的关系,通过ps --forest 可以查看父子shell之间的 层级关系。当运行一个shell命令时,sehll会创建一个子来执行改命令
3. 进程列表
当我们想一次运行多个命令时可以在命令与命令之间加上(;)这样就shell就会依次执行这些命令。如下:
[root@localhost ~]# pwd;ls;cd /etc
/root
anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
[root@localhost etc]#

但上述并不是进程列表,将上述命令加到一个()里,这样就shell仅会生成一个子shell来执行这些命令,如下:
[root@localhost ~]# (pwd;ls;cd /etc;pwd)
/root
anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
/etc

还有一种创建进程列表的方法,它不会创建子shell进程,将命令放置于{}中,{}内首尾用空格隔开,命令后务必加(;),如下:
[root@localhost ~]# { pwd;ls;cd /etc;pwd; }
/root
anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
/etc

要知道是否生成了子进程可以使用$BASH_SUBSHELL环境变量的值来查看,为0时表示没有生成子进程,为1时表示生成了子进程
[root@localhost ~]# (pwd;ls;cd /etc;echo $BASH_SUBSHELL)
/root
anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
1

[root@localhost ~]# { pwd;ls;cd /etc;echo $BASH_SUBSHELL; }
/root
anaconda-ks.cfg Documents install.log.syslog Pictures Templates
Desktop Downloads Music Public Videos
0

使用子进程的开销比较大,因为终端控制着子进程的I/O

4. 后台模式
用终端启动的进程一般会在前台运行,通过下列方式可以将命令切至后台运行
COMMADN & :在命令后在&,可以将命令运行至后台
CTRL+Z:在命令运行时执行此操作可以将命令转到后台运行

上述方法虽然将命令送至后台运行但是依然与终端相关,如果需要剥离与终端的联系,应使用如下方式:
nohub COMMAND &

日常使用中可以使用后台模式,让命令执行时不受制于终端I/O,提高效率,例如进行文件归档时:
$ (tar -cf Rich.tar /home/rich ; tar -cf My.tar /home/christine)&
[3] 2423

可以通过jobs命令来查看当前运行在后台的进程,或jobs -l来查看更详细的信息,一旦任务完成则会看到Done的提示
还可以通过如下方式来控制作业
fg [[%] JOB_NUM]:把指定的后台作业调回前台;
bg [[%] JOB_NUM]:让送回后台的作业继续运行;
kill [[%] JOB_NUM]:终止指定作业

可以用一个有意思的命令来查看后台作业:
sleep # & :让进程睡眠#秒,此时可以使用jobs命令查看后台作业

协程命令coproc,它可以让命令运行于后台,可以创建一个子shell,然后在子shell中运行指定的命令,例如:
[root@localhost etc]# coproc { sleep 10; }
[1] 24343
[root@localhost etc]# jobs -l
[1]+ 24343 Running coproc COPROC { sleep 10; } &

5. 外部命令和内建命令
可以通过type命令来区别外部命令和内建命令:
[root@localhost etc]# type cd
cd is a shell builtin
[root@localhost etc]# type htop
htop is /usr/bin/htop
由上可以看出cd是一个内建命令,htop是一个外部命令,可以通过type -a来查看更详细的信息,通过which命令来看到命令所在的路径。

有的命令存在外部和内建两种方式,例如pwd:
[root@localhost etc]# type -a pwd
pwd is a shell builtin
pwd is /bin/pwd
[root@localhost etc]# which pwd
/bin/pwd

内建命令在运行时不会创建子进程来运行,比外部命令效率高。

常用的内建命令有:
history:用于管理命令历史,当登录shell时,会读取命令历史文件中的记录,~/.bash_history,当退出shell会将登录shell后执行的命令追加至命令历史文件中。
参数:
-a:将当前shell中执行的命令追加到~/.bash_history中
-d #:删除指定编号的命令历史中的命令
-c:清空命令历史
快捷操作:
!!:调用上一条命令
!#:调用命令历史中第#条命令
! string:调用命令历史中最近一个以string开头的命令

alias/unalias:命令别名管理
alias 别名=命令名(如果包含空格需要在命令名外加引号):创建别名
unalias 别名
使用命令修改只能当前shell有效,如果要永久有小,需要修改~/.bashrc文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  命名 2014