按下ls -l *.py并回车,Shell都为我们做了什么?

系统
shell 是如何理解和解释这些命令的?屏幕的背后都做些什么?比如说,当我们执行 ls -l *.py 的时候,shell 都做了哪些事情?了解了这些,可以更好的使用 Unix 类操作系统,今天我们就来一探究竟。

[[437059]]

你是否想过,当你在 shell 上执行一个命令时,unix 的 shell 到底做了哪些事情?shell 是如何理解和解释这些命令的?屏幕的背后都做些什么?比如说,当我们执行 ls -l *.py 的时候,shell 都做了哪些事情?了解了这些,可以更好的使用 Unix 类操作系统,今天我们就来一探究竟。

0、什么是 shell

shell 通常是一个命令行界面,它将操作系统的服务暴露给人类使用或其他程序。在 shell 启动后,shell 通常会通过显示提示来等待用户的输入。下图描述了基本的 UNIX 和 Windows shell 提示。

所以 shell 会提示用户输入命令。现在是用户输入命令的时候了。那么 shell 是如何获取用户输入的命令并进行解释的呢?为了理解这一点,让我们将它们分为 4 个步骤,分别是:

  • 获取并解析用户输入
  • 识别命令及命令的参数
  • 查找命令
  • 执行命令

现在详细展开:

1、获取并解析用户输入

比如说,在 shell 上输入了 ls -l *.py 并回车,shell 内部会调用一个叫 getline()「声明在#include 中,下同」 的函数来读取用户输入的命令,用户输入的命令字符串作为标准输入流,一旦按下回车,表示一行结束,getline() 就会将输入的字符串存储到缓冲区中。

  1. ssize_t getline(char **restrict lineptr, size_t *restrict n, FILE *restrict stream); 

函数参数说明:

  • lineptr: 缓冲区
  • n: 缓冲区大小
  • stream: 流,这里就是标准输入流

现在让我们看一下代码:

  1. char *input_buffer; 
  2.  
  3. size_t b_size; 
  4.  
  5. b_size = 32; // size of the buffer 
  6.  
  7. input_buffer = malloc(sizeof(char) * b_size); // the buffer to store the user input 
  8.  
  9. getline(&input_buffer, &b_size, stdin); // gets the line and stores it in input_buffer 

一旦用户按下回车,就会调用 getline() ,将用户输入的字符串或命令将存储在 input_buffer 中。所以现在 shell 已经获取了用户输入,那么下一步是什么?

2、识别命令及命令的参数

现在 shell 已经知道你输入了字符串是 'ls -l *.py' 但是,还需要知道这里面哪个是命令,哪个是命令的参数,谁来做这个事情呢?那就是函数 strtok()「#include 」。

strtok() 将一个字符串标记为分隔符,在这个例子中分隔符是一个空格。所以一个空格告诉 strtok() 它是一个词的结尾。因此 input_buffer 中的第一个标记或单词是命令 (ls),其余的单词或标记(-l 和 *.py)是命令的参数。因此,一旦 shell 标记了字符串,它就会将它们存储在一个变量中,以便以后使用。

  1. char *strtok(char *restrict str, const char *restrict delim); 

参数说明:

  • str: 要标记的字符串
  • delim: 分隔符

函数 strtok() 接受字符串和分隔符作为参数,返回一个指向标记字符串的指针。具体的执行代码如下所示:

  1. char *input_buffer, *args, *delim_args, *command_argv[50]; 
  2. int i; 
  3.  
  4. i = 0; 
  5. delim_args = " \t\r\n\v\f"; // the delimeters 
  6. args = strtok(input_buffer, delim_args); // stores the token inside args 
  7. while (args) 
  8.  command_argv[i] = args; // stores the token in command_argv 
  9.  args = strtok(NULL, delim_args); 
  10.  i++; 
  11. command_argv[i] = NULL; // sets the last entity of command_argv to NULL 

command_argv[i] = NULL; // sets the last entity of command_argv to NULL

command_argv 保存了命令字符串,其内容如下:

  1. command_argv[0] = "ls" 
  2.  
  3. command_argv[1] = "-l" 
  4.  
  5. command_argv[2] = "*.py" 
  6.  
  7. command_argv[3] = NULL 

好了,command_argv[0] 是命令,其他的都是它的参数,最后一个是 NULL,表示命令的结束。命令字符串已经拆解完毕了,下一步就是查找命令。

3、查找命令

第二步已经知道,用户要执行的命令就是 ls,那么去哪里查找这个命令呢?shell 回去环境变量 PATH 中去查找,PATH 这个环境变量就是存储可执行命令的位置的。

不过,一个 PATH 存储的路径可不止一个:

如何在这么多路径中高效的查找到 ls 命令呢?这就需要 access() 「#include 」 函数:

  1. int access(const char *pathname, int mode); 

参数及返回值说明:

  • pathname: 文件/可执行文件的路径
  • mode: 模式,我们使用 X_OK 来检查文件是否存在
  • 返回值:如果文件存在,返回 0,否则返回 -1
  1.  char *path_buff, *path_dup, *paths, *path_env_name, *path[50]; 
  2.  int i; 
  3.  
  4.  i = 0; 
  5.  path_env_name = "PATH"
  6.  path_buff = getenv(path_env_name); /* get the variable of PATH environment */ 
  7.  path_dup = _strdup(path_buff); /* this function is found below */ 
  8.  paths = strtok(path_dup, ":"); /* tokenizes it */ 
  9.  while (paths) 
  10.  { 
  11.   path[i] = paths; 
  12.   paths = strtok(NULL":"); 
  13.   i++; 
  14.  } 
  15.  path[i] = NULL; /* terminates it with NULL */ 
  16.  
  17. /** 
  18. * _strdup - duplicates a string 
  19. * @from: the string to be duplicated 
  20. Return: ponter to the duplicated string 
  21. */ 
  22. char *_strdup(char *from
  23.  int i, len; 
  24.  char *dup_str; 
  25.  
  26.  len = _strlen(from) + 1; 
  27.  dup_str = malloc(sizeof(int) * len); 
  28.  i = 0; 
  29.  
  30.  while (*(from + i) != '\0'
  31.  { 
  32.   *(dup_str + i) = *(from + i); 
  33.   i++; 
  34.  } 
  35.  *(dup_str + i) = '\0'
  36.  
  37.  return (dup_str); 

上面代码中的 path 数组存储所有 PATH 位置并且以 NULL 终止。因此,可以将每个 PATH 位置与命令连接起来,并使用 access() 函数执行存在性检查:

  1.  char *command_file, *command_path, *path[50]; 
  2.  int i; 
  3.  
  4.  i = 0; 
  5.  command_path = malloc(sizeof(char) * 50); 
  6.  while (path[i] != NULL
  7.  { 
  8.   _strcat(path[i], command_file, command_path); /* this function is found below */ 
  9.   stat_f = access(command_path, X_OK); /* and checks if it exists */ 
  10.   if (stat_f == 0) 
  11.    return (command_path); /* returns the concatenated string if found */ 
  12.  
  13.   i++; 
  14.  } 
  15.  return NULL; /* otherwise returns NULL */ 
  16.  
  17. /** 
  18. * _strcat - concatenates two strings and saves it to a blank string 
  19. * @path: the path string 
  20. * @command: the command 
  21. * @command_path: the string to store the concatenation 
  22. Return: Always void 
  23. */ 
  24. void _strcat(char *path, char *command, char *command_path) 
  25.  int i, j; 
  26.  
  27.  i = 0; 
  28.  j = 0; 
  29.  
  30.  while (*(path + i) != '\0'
  31.  { 
  32.   *(command_path + i) = *(path + i); 
  33.   i++; 
  34.  } 
  35.  *(command_path + i) = '/'
  36.  i++; 
  37.  
  38.  while (*(command + j) != '\0'
  39.  { 
  40.   *(command_path + i) = *(command + j); 
  41.   i++; 
  42.   j++; 
  43.  } 
  44.  *(command_path + i) = '\0'

一旦找到命令,就会返回命令的完整路径,否则就返回 NULL,然后 shell 会显示命令不存在的错误。

现在假如命令找到了,然后呢?

4、执行命令

命令一旦找到,就是执行它的时候了,问题是怎么执行呢?

执行命令,需要借助函数 execve()「#include 」中:

  1. int execve(const char *pathname, char *const argv[], 
  2.  
  3. char *const envp[]); 

参数说明:

  • pathname: 可执行文件的完整路径
  • argv: 命令的参数
  • envp: 环境变量列表

execve() 会执行找到的命令,返回一个整数表示执行结果。

但是现在如果 shell 只是运行 execve(),就会出现问题。execve() 调用后不返回标准输出的信息,这是不好的,因为用户需要执行的结果。所以为了解决这个问题,shell 在子进程中执行命令。因此,一旦在子进程内执行完成,父进程就会收到信号并且程序流继续。所以为了执行命令,shell 使用 fork() 创建了一个子进程。(fork 声明在#include 中)

  1. pid_t fork(void); 

fork() 通过复制调用进程来创建一个新进程。新进程称为子进程。调用进程称为父进程。fork() 在父进程中返回子进程的进程 ID,在子进程中返回 0:

  1.  char *command, *command_argv[50], **env; 
  2.  pid_t child_pid; 
  3.  int status; 
  4.  
  5.  get_each_command_argv(command_argv, input_buffer); /* this function is found below */ 
  6.  child_pid = fork(); 
  7.  if (child_pid == -1) 
  8.   return (0); 
  9.  
  10.  if (child_pid == 0) 
  11.  { 
  12.   if (execve(command, command_argv, env) == -1) 
  13.    return (0); 
  14.  } 
  15.  else 
  16.   wait(&status); 
  17.  
  18. /** 
  19. * get_each_command_argv - stores all the arguments \ 
  20. *             of the input command to the list 
  21. * @command_argv: the command argument list 
  22. * @input_buffer: the input buffer 
  23. Return: Always void 
  24. */ 
  25. void get_each_command_argv(char **command_argv, char *input_buffer) 
  26.  char *args, *delim_args; 
  27.  int i; 
  28.  
  29.  delim_args = " \t\r\n\v\f"
  30.  args = strtok(input_buffer, delim_args); 
  31.  
  32.  i = 0; 
  33.  while (args) 
  34.  { 
  35.   command_argv[i] = args; 
  36.   args = strtok(NULL, delim_args); 
  37.   i++; 
  38.  } 
  39.  command_argv[i] = NULL

shell 使用 wait()(函数声明在#include ) 在程序流继续之前等待子进程的状态变化,并再次为用户显示提示。

  1. pid_t wait(int *wstatus); 

wstatus:是一个指向整数的指针,可以用来标识子进程是如何终止的。

shell 在子进程内执行命令,然后 wait() 等待子进程完成。所以这样用户就可以得到命令的结果,并且可以在 shell 显示其提示后输入另一个命令。

所以最后当子进程完成时显示 ls -l *.py 的结果,并且由于我们已经等待子进程结束,这意味着给出了命令的结果。所以现在 shell 可以再次显示它的提示以再次等待用户输入。这将继续循环,除非用户键入 exit。

 

责任编辑:武晓燕 来源: Python七号
相关推荐

2011-04-19 10:04:25

NeopPIshell网站后门

2020-09-01 11:40:01

HTTPJavaTCP

2010-03-12 14:28:52

三层交换技术

2020-08-13 08:04:31

配置跨域框架

2020-10-09 08:59:55

输入网址解密

2015-08-04 09:33:24

Coding.net

2011-03-31 09:20:45

URLDNSWeb应用程序

2020-12-15 06:57:24

java服务器

2016-01-29 10:05:13

酒店服务业大数据大数据分析

2022-12-07 07:33:54

Java启动类项目

2014-07-17 15:52:00

Android L

2021-05-07 11:25:29

项目网关流量

2021-03-15 08:40:42

Vue组件函数

2011-11-29 09:10:11

Hadoop

2020-09-13 09:22:51

Linuxls命令

2021-02-25 10:02:32

开机键Linux内存

2020-04-01 17:39:57

天翼云新基建

2022-03-11 21:28:31

部署开发服务器

2016-11-17 15:35:51

RxJava操作Subscriber

2015-09-16 15:01:30

PHP内核
点赞
收藏

51CTO技术栈公众号