加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

理解shell(第五章)

发布时间:2020-12-15 16:50:53 所属栏目:安全 来源:网络整理
导读: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

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/rootanaconda-ks.cfg Documents install.log.syslog Pictures TemplatesDesktop Downloads Music Public Videos[root@localhost etc]# 但上述并不是进程列表,将上述命令加到一个()里,这样就shell仅会生成一个子shell来执行这些命令,如下:[root@localhost ~]# (pwd;ls;cd /etc;pwd) /rootanaconda-ks.cfg Documents install.log.syslog Pictures TemplatesDesktop Downloads Music Public Videos/etc还有一种创建进程列表的方法,它不会创建子shell进程,将命令放置于{}中,{}内首尾用空格隔开,命令后务必加(;),如下:[root@localhost ~]# { pwd;ls;cd /etc;pwd; }/rootanaconda-ks.cfg Documents install.log.syslog Pictures TemplatesDesktop Downloads Music Public Videos/etc要知道是否生成了子进程可以使用$BASH_SUBSHELL环境变量的值来查看,为0时表示没有生成子进程,为1时表示生成了子进程[root@localhost ~]# (pwd;ls;cd /etc;echo $BASH_SUBSHELL)/rootanaconda-ks.cfg Documents install.log.syslog Pictures TemplatesDesktop Downloads Music Public Videos1[root@localhost ~]# { pwd;ls;cd /etc;echo $BASH_SUBSHELL; }/rootanaconda-ks.cfg Documents install.log.syslog Pictures TemplatesDesktop Downloads Music Public Videos0使用子进程的开销比较大,因为终端控制着子进程的I/O4. 后台模式用终端启动的进程一般会在前台运行,通过下列方式可以将命令切至后台运行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 cdcd is a shell builtin[root@localhost etc]# type htophtop is /usr/bin/htop由上可以看出cd是一个内建命令,htop是一个外部命令,可以通过type -a来查看更详细的信息,通过which命令来看到命令所在的路径。有的命令存在外部和内建两种方式,例如pwd:[root@localhost etc]# type -a pwdpwd is a shell builtinpwd 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文件。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读