我想在Linux系统上用C编写一个简单,愚蠢的X终端模拟器.
起初,我只是想我必须打开一个shell并显示它的输出.我检查了xterm和rxvt代码,看起来有点复杂.
首先,我必须用openpty打开一个伪终端.所以我看一下手册页,看看openpty填充了2个文件描述符,即master和slave.xterm和rxvt代码都很混乱,因为这些特殊文件的系统依赖性.
我理解termios的东西:它只是关于终端转义码的一堆信息.我真正没有得到的是:我想对主/从文件描述符做什么?
打开终端,登录,在shell上执行"ls"的示例程序将是非常棒的.
(英语不是我的母语,原谅我最终的错误)
编辑:这是我提出的示例代码:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <pty.h>
#include <utmp.h>
#include <ctype.h>
void
safe_print (char* s)
{
while(*s) {
if(*s == '\n')
putchar("\n");
else if(iscntrl(*s))
printf("\\e(%d)", *s);
else
putchar(*s);
s++;
}
}
int
main (int argc, char** argv)
{
char buf[BUFSIZ] = {0};
int master;
int ret = forkpty(&master, NULL, NULL, NULL);
if(ret == -1)
puts("no fork"), exit(0);
if(!ret) {
execl("/bin/sh", "sh", NULL);
exit(0);
}
sleep(1); /* let the shell run …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用docker build适当的命令自动创建开发Docker镜像Dockerfile.我需要在RUN命令中运行的脚本之一要求用户单击并阅读其许可协议.因此有两个问题:
RUNa 中所有命令的输出在哪里Dockerfile?docker build命令卡住了,要求用户在无限循环中输入.有谁知道如何从adb shell运行命令并保留在shell会话中?我想要实现的是在adb shell中设置别名.
我试过以下但没有成功
adb shell <<< "ls"
Run Code Online (Sandbox Code Playgroud)
执行此命令后确实保留在shell中,但无法接收任何进一步命令的输出.
我也尝试过以下方法:
adb shell <<EOF
ls
EOF
Run Code Online (Sandbox Code Playgroud)
结果相同.
我有一个有自己提示的程序
example_program>
Run Code Online (Sandbox Code Playgroud)
我需要通过这个程序运行一系列命令
example_program> command_A
example_program> command B
Please enter input: [input_here]
example_program> command C
Run Code Online (Sandbox Code Playgroud)
我可以通过 shell 脚本中的以下行发送命令 A、B、C:
(echo "command_C" && cat) | (echo "command_B" && cat) | (echo "command_A" && cat ) | example_program
Run Code Online (Sandbox Code Playgroud)
如何输入所需的输入并在命令 B ([input_here]) 后提示输入?
我无权发送或期待。
我有一个脚本,旨在接受来自stdin的输入,然后提示用户输入更多内容.这是一个人为的例子来说明我的意思:
import sys
# Get input from stdin
input_nums = [int(n.strip()) for n in sys.stdin]
# Prompt user
mult = int(raw_input("Enter a number by which to multiply your input: "))
for num in input_nums:
print num*mult
Run Code Online (Sandbox Code Playgroud)
当我从stdin传入数据时,python将stdin解释为在它到达之前关闭raw_input,它给出了EOFError: EOF when reading a line:
[user]$ cat nums.txt
2
3
4
5
[user]$ cat nums.txt | python sample.py
Enter a number by which to multiply your input: Traceback (most recent call last):
File "sample.py", line 6, in <module>
mult …Run Code Online (Sandbox Code Playgroud) 我想将数据通过管道传输到交互式命令中,并将交互式命令的输出作为另一个命令的输入接收。
例如,我希望能够执行以下操作:
echo "Zaphod" | hello.sh | goodbye.sh
Run Code Online (Sandbox Code Playgroud)
并有输出:
再见,赞福德
这是我对此的初步破解,但我错过了一些东西 ;-) 我实际上希望 hello.sh 从事物列表中进行选择。
你好.sh
echo Please supply your name
read NAME
echo "HELLO $NAME"
Run Code Online (Sandbox Code Playgroud)
再见.sh
MSG=$*
if [ -z "$1" ]
then
MSG=$(cat /dev/stdin)
fi
echo "BYE $MSG"
Run Code Online (Sandbox Code Playgroud)
编辑:通过“从事物列表中选择”,我想我是在暗示我的真实用例,它从标准输出中获取任何东西,让我选择一个选项,然后将其传递给其他东西的标准输入...例如:
ls /tmp | select_from_list | xargs cat
将允许我列出 /tmp/ 中的文件,交互选择一个,然后 cat 文件的内容。
所以我的“select_from_list”脚本实际上是这样的:
#!/bin/bash
prompt="Please select an option:"
options=( $* )
if [ -z "$1" ]
then
options=$(cat /dev/stdin)
fi
PS3="$prompt "
select opt in "${options[@]}" "Quit" ; do …Run Code Online (Sandbox Code Playgroud) 我也读过有关此问题的先前问题。fflush(stdin) 在这种情况下对我不起作用。我希望我的程序从管道标准输入读取并从中间的键盘输入继续。
int main()
{
int userin = 3;
read_input();
userin = print_menu();
userin = parse_input(userin);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想从文件中读取数据,该文件作为管道标准传递给程序,例如
int read_input(){
char line[200];
char word[MAX_STRING+1];
int line_number = 0;
while(fgets(line, sizeof(line), stdin) != NULL ){
//do something
printf("%s",line);
line_number++;
}
}
Run Code Online (Sandbox Code Playgroud)
现在 read_input 必须完成从管道输入的读取。“print_menu”必须继续从键盘读取。
int print_menu()
{
int userinput;
char c;
char num[4];
while((c=getchar()) != '\n' && c != EOF && c != '\r');
printf("\n1. Choice 1 \n");
printf("2. Choice 2\n");
printf("3. Exit\n");
printf("Enter your choice (1-3): "); …Run Code Online (Sandbox Code Playgroud) 有没有办法在调用时将字符串"推"到程序的stdin流?
这样我们才能有效果
echo "something" | ./my_program
Run Code Online (Sandbox Code Playgroud)
但是,不是在读取EOF之后"something",my_program将从原始标准输入(例如,键盘)读取其进一步输入.
示例:假设我们要启动一个bash shell,但我们要在其中做的第一件事就是调用date.echo date | bash不会做这个工作,因为shell会在运行后终止date.