Linux 解析终端输出/输入的方法?(.bashrc?)
有没有办法在交互终端中解析bash命令到达屏幕之前的输入和输出?我在想可能在.bashrc中有些东西,但我对使用bash还不熟悉 例如:Linux 解析终端输出/输入的方法?(.bashrc?),linux,bash,command-line,terminal,Linux,Bash,Command Line,Terminal,有没有办法在交互终端中解析bash命令到达屏幕之前的输入和输出?我在想可能在.bashrc中有些东西,但我对使用bash还不熟悉 例如: 我输入“ls/home/foo/bar/” 它通过一个脚本传递,该脚本将“bar”的所有实例替换为“eggs” 执行“ls/home/foo/eggs/” 将输出发送回替换脚本 脚本的输出被发送到屏幕 是的。下面是我为自己写的东西,用来包装请求文件路径的旧命令行Fortran程序。它允许返回外壳,例如运行“ls”。这只有一种方式,即截取用户输入,然后将其传递
- 我输入“ls/home/foo/bar/”
- 它通过一个脚本传递,该脚本将“bar”的所有实例替换为“eggs”
- 执行“ls/home/foo/eggs/”
- 将输出发送回替换脚本
- 脚本的输出被发送到屏幕
是的。下面是我为自己写的东西,用来包装请求文件路径的旧命令行Fortran程序。它允许返回外壳,例如运行“ls”。这只有一种方式,即截取用户输入,然后将其传递给程序,但可以得到您想要的大部分内容。你可以根据自己的需要进行调整
#!/usr/bin/perl
# shwrap.pl - Wrap any process for convenient escape to the shell.
# ire_and_curses, September 2006
use strict;
use warnings;
# Check args
my $executable = shift || die "Usage: shwrap.pl executable";
my @escape_chars = ('#'); # Escape to shell with these chars
my $exit = 'exit'; # Exit string for quick termination
open my $exe_fh, "|$executable @ARGV" or die "Cannot pipe to program $executable: $!";
# Set magic buffer autoflush on...
select((select($exe_fh), $| = 1)[0]);
# Accept input until the child process terminates or is terminated...
while ( 1 ) {
chomp(my $input = <STDIN>);
# End if we receive the special exit string...
if ( $input =~ m/$exit/ ) {
close $exe_fh;
print "$0: Terminated child process...\n";
exit;
}
foreach my $char ( @escape_chars ) {
# Escape to the shell if the input starts with an escape character...
if ( my ($command) = $input =~ m/^$char(.*)/ ) {
system $command;
}
# Otherwise pass the input on to the executable...
else {
print $exe_fh "$input\n";
}
}
}
#/usr/bin/perl
#pl-对任何进程进行包装,以方便转义到shell。
#愤怒与诅咒,2006年9月
严格使用;
使用警告;
#检查args
my$executable=shift | | die“用法:shwrap.pl executable”;
我的@escape_chars=('#')#带着这些护身符逃到贝壳里去
我的$exit='exit'#用于快速终止的退出字符串
打开我的$exe_fh,“|$executable@ARGV”或die“无法通过管道连接到程序$executable:$!”;
#将“魔法缓冲区自动刷新”设置为打开。。。
选择((选择($exe_fh),$|=1)[0]);
#接受输入,直到子进程终止或被终止。。。
而(1){
chomp(我的$input=);
#如果我们收到特殊的退出字符串,则结束。。。
如果($input=~m/$exit/){
关闭$exe_fh;
打印“$0:终止的子进程…\n”;
出口
}
foreach我的$char(@escape_chars){
#如果输入以转义字符开头,则转义到shell。。。
如果(my($command)=$input=~m/^$char(.*)/){
系统$命令;
}
#否则,将输入传递到可执行文件。。。
否则{
打印$exe\U fh“$input\n”;
}
}
}
有没有办法让箭头键和历史记录等内容与脚本一起使用?我不知道。这些是shell内置的,假设shell是以交互方式运行的(本例中不是这样)。如果你想要更多的功能,那么你真的在考虑编写你自己的shell——这并不像听起来那么难。见例。