Ubuntu 中如何读取命令行

Ubuntu 中如何读取命令行

当我在做一个项目时,我开始了解如何使用 ncurses 和 GNU 的 readline 库读取命令行。但是我在 Ubuntu (16.04) 中找不到。我很好奇Ubuntu如何在用户输入时处理命令?例如:它如何检测向上/向下箭头被按下,如何检测 Tab 等?

答案1

Ubuntu操作系统不读取命令行。有些程序读取命令行。例如,bash是一个命令解释器(也称为)读取命令行。当 shell 是交互式的并从终端读取命令行时,它使用 GNUreadline图书馆。

$ sudo apt-get -y install libreadline6-dev readline-doc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libtinfo-dev
The following NEW packages will be installed:
  libreadline6-dev libtinfo-dev readline-doc
0 upgraded, 3 newly installed, 0 to remove and 1 not upgraded.
Need to get 299 kB of archives.
After this operation, 1,233 kB of additional disk space will be used.
...

$ sudo dpkg-query -l '*readline*'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
un  libreadline-co <none>       <none>       (no description available)
un  libreadline-gp <none>       <none>       (no description available)
un  libreadline4   <none>       <none>       (no description available)
ii  libreadline5:a 5.2+dfsg-3bu amd64        GNU readline and history librarie
un  libreadline5-d <none>       <none>       (no description available)
ii  libreadline6:a 6.3-8ubuntu2 amd64        GNU readline and history librarie
ii  libreadline6-d 6.3-8ubuntu2 amd64        GNU readline and history librarie
un  libterm-readli <none>       <none>       (no description available)
un  libterm-readli <none>       <none>       (no description available)
un  php-readline   <none>       <none>       (no description available)
ii  php7.0-readlin 7.0.13-0ubun amd64        readline module for PHP
ii  readline-commo 6.3-8ubuntu2 all          GNU readline and history librarie
ii  readline-doc   6.3-8ubuntu2 all          GNU readline and history librarie
un  tcl-tclreadlin <none>       <none>       (no description available)


$ cat trl.c
#include <stdio.h>
#include <stdlib.h>

#include <readline/readline.h>

int main(void)

{
  char * line = readline("Enter some text: ");
  if (line) {
    printf("You have entered \"%s\"\n", line);
  }
  return EXIT_SUCCESS;
}

$ gcc -Wall trl.c -o trl -lreadline

$ ./trl
Enter some text: Some text to be read by readline()
You have entered "Some text to be read by readline()"

相关内容