1:查看本系统shell的默认交互shell

yusuwudeMacBook-Pro:~ yusuwu$ cat /etc/passwd

nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
root:*:0:0:System Administrator:/var/root:/bin/sh
daemon:*:1:1:System Services:/var/root:/usr/bin/false

2:查看本系统的shell

yusuwudeMacBook-Pro:~ yusuwu$ ls -lF /bin/*sh
-r-xr-xr-x  1 root  wheel   623472  4  7 03:52 /bin/bash*
-rwxr-xr-x  1 root  wheel   529424  4  7 03:52 /bin/csh*
-rwxr-xr-x  1 root  wheel   110848  4  7 03:52 /bin/dash*
-r-xr-xr-x  1 root  wheel  1300256  4  7 03:52 /bin/ksh*
-rwxr-xr-x  1 root  wheel    31440  4  7 03:52 /bin/sh*
-rwxr-xr-x  1 root  wheel   529424  4  7 03:52 /bin/tcsh*
-rwxr-xr-x  1 root  wheel   637632  4  7 03:52 /bin/zsh*

通过上面命令执行结果来看,*sh都是可执行程序,如果我们想切入到其他的shell,可以直接执行命令:

yusuwudeMacBook-Pro:~ yusuwu$ /bin/bash 

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ exit
exit
yusuwudeMacBook-Pro:~ yusuwu$ 

退出exit则可.

 3:shell的父子关系

yusuwudeMacBook-Pro:~ yusuwu$ ps -f
  UID   PID  PPID   C STIME   TTY           TIME CMD
  501  1457  1456   0  6:17下午 ttys000    0:00.02 -bash
yusuwudeMacBook-Pro:~ yusuwu$ /bin/bash 

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
bash-3.2$ ps -f
  UID   PID  PPID   C STIME   TTY           TIME CMD
  501  1457  1456   0  6:17下午 ttys000    0:00.02 -bash
  501  1844  1457   0 11:20下午 ttys000    0:00.01 /bin/bash
bash-3.2$ /bin/dash
$ ps -f
  UID   PID  PPID   C STIME   TTY           TIME CMD
  501  1457  1456   0  6:17下午 ttys000    0:00.02 -bash
  501  1844  1457   0 11:20下午 ttys000    0:00.01 /bin/bash
  501  1846  1844   0 11:20下午 ttys000    0:00.00 /bin/dash
$ exit 
bash-3.2$ exit
exit
yusuwudeMacBook-Pro:~ yusuwu$ 

通过上面代码的执行顺序和父子的PID可以看出.

  3.1:进程列表

  命令列表只需要在命令之间加分号“;”即可,命令列表想成为进程列表,这些命令就必须加在园括号里.

  

yusuwudeMacBook-Pro:~ yusuwu$ pwd;cd /etc/;pwd;cd ;ls;echo $BASH_SUBSHELL 
/Users/yusuwu
/etc
Applications            Movies                YXL.tar
Applications (Parallels)    Music                eclipse-workspace-20200505
Desktop                Parallels            iCloud Drive(归档)
Documents            Pictures            jetbrains-agent
Downloads            Postman                package-lock.json
IdeaProjects            Public                resourceFiles
Library                YXL
0
yusuwudeMacBook-Pro:~ yusuwu$ (pwd;cd /etc/;pwd;cd;ls;echo $BASH_SUBSHELL );
/Users/yusuwu
/etc
Applications            Movies                YXL.tar
Applications (Parallels)    Music                eclipse-workspace-20200505
Desktop                Parallels            iCloud Drive(归档)
Documents            Pictures            jetbrains-agent
Downloads            Postman                package-lock.json
IdeaProjects            Public                resourceFiles
Library                YXL
1
yusuwudeMacBook-Pro:~ yusuwu$ 

住: echo $BASH_SUBSHELL 如果这个命令返回0,就表示没有子sehll,如果返回1或者其他更大的数字,就表示存在子shell.上面执行的代码可以看出被园括号括住的命令列表变成了进程列表,

进程列表会生成一个子shell来执行对应的命令.

  3.2:后台模式:

要想将命令置入后台模式,可以在命令末尾加上字符&;

yusuwudeMacBook-Pro:~ yusuwu$ sleep 10
yusuwudeMacBook-Pro:~ yusuwu$ ps -f
  UID   PID  PPID   C STIME   TTY           TIME CMD
  501  1457  1456   0  6:17下午 ttys000    0:00.06 -bash
yusuwudeMacBook-Pro:~ yusuwu$ sleep 20&
[1] 1871
yusuwudeMacBook-Pro:~ yusuwu$ ps -f
  UID   PID  PPID   C STIME   TTY           TIME CMD
  501  1457  1456   0  6:17下午 ttys000    0:00.07 -bash
  501  1871  1457   0 11:34下午 ttys000    0:00.00 sleep 20
yusuwudeMacBook-Pro:~ yusuwu$ jobs
[1]+  Running                 sleep 20 &
yusuwudeMacBook-Pro:~ yusuwu$ jobs -l
[1]+  1871 Done                    sleep 20

  3.3:将进程列表置入后台:

yusuwudeMacBook-Pro:~ yusuwu$ (sleep 10; echo $BASH_SUBSHELL ; sleep 10)&
[1] 1876

在CLI中运用子shell出创造性方法之一就是将进程列表置入后台模式,这样我们既可以在子shell中进行繁重的处理工作,同时也不会让子shell的I/O受制于终端.

eg:

 

yusuwudeMacBook-Pro:~ yusuwu$ (tar -czf ./YXL.tar.gz ./YXL)&
[1] 1888

当然将进程列表置入后台模式并不是子shell在CLI中仅有的创造性用法.协程就是另一种方法.

  3.4:协程

协程可以同时做两件事,它在后台生成一个子shell,并在这个子shell中执行命令.要进行协程处理,得使用coproc命令,还有要在子shell中执行的命令.

$ coproc sleep 10

[1] 2544

$

除了会创建在shell外,协程基本上就是将命令置入后台模式.

$ jobs

[1]+ Running      coproc COPROC sleep 10 &

$

COPROC是coproc命令给进程起的名字,我们也可以使用命令的扩展语法自己设置这个名字

$ coproc my_job { sleep 10; }

[1] 2570

$

$jobs

[1]+ Running      coproc my_job { sleep 10; } &

$

这里要注意的是:扩展语法写起来必须要确保在第一个花括号"{"和命令之间有一个空格.还必须保证命令以分号";"结尾,另外分号和闭花括号"}"之间也得有一个空格.

4:理解shell的内建命令

  4.1:外部命令和内建命令的区分

yusuwudeMacBook-Pro:bin yusuwu$ type -a ps
ps is /bin/ps
yusuwudeMacBook-Pro:bin yusuwu$ which ps 
/bin/ps
yusuwudeMacBook-Pro:bin yusuwu$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd
yusuwudeMacBook-Pro:bin yusuwu$ type pwd
pwd is a shell builtin
yusuwudeMacBook-Pro:bin yusuwu$ which pwd
/bin/pwd
yusuwudeMacBook-Pro:bin yusuwu$ type -a echo
echo is a shell builtin
echo is /bin/echo
yusuwudeMacBook-Pro:bin yusuwu$ 

外部命令也被称为文件系统命令.是存在于bash shell之外的程序.它们并不是shell程序的一部分,外部命令程序通常位于/bin,/usr/bin,/sbin或/usr/sbin中.

当外部命令执行时会衍生的创建出一个子进程,当进程必须执行衍生操作时,它需要花费时间和精力来设置新子进程的环境,所以说,外部命令多少还是有代价的.

内建命令和外部命令的区别在于前者不需要使用子进程来执行,它们已经和shell编译成了一体,作为shell工具的组成部分存在.不需要借助外部程序文件来运行.

所以内建命令的执行效率更高.

yusuwudeMacBook-Pro:bin yusuwu$ type -a exit
exit is a shell builtin

命令type -a显示出每个命令的两种实现,which命令只显示了外部命令文件.

[1]:history

yusuwudeMacBook-Pro:bin yusuwu$ type -a history
history is a shell builtin
yusuwudeMacBook-Pro:bin yusuwu$ history
...
  111  ls
  112  cd JavaVirtualMachines/
...
 609  type -a history
  610  history

记: !! 加回车:执行刚才执行的一条命令

  !加命令编号: eg:  !112 则会执行history中的112条命令.

  history -a和history -n命令用法

[2]:命令别名 alias

yusuwudeMacBook-Pro:~ yusuwu$ la
-bash: la: command not found
yusuwudeMacBook-Pro:~ yusuwu$ alias la='ls -al'
yusuwudeMacBook-Pro:~ yusuwu$ la
total 721400
drwxr-xr-x+  50 yusuwu  staff       1600  5 16 00:22 .
drwxr-xr-x    6 root    admin        192  4  7 03:45 ..
-r--------    1 yusuwu  staff          9  6 19  2018 .CFUserTextEncoding
-rw-r--r--@   1 yusuwu  staff      24580  5 13 16:26 .DS_Store

 

posted on 2020-05-16 00:58  曾经那个少年  阅读(350)  评论(0编辑  收藏  举报