0
有没有办法在交互式终端中的bash命令解析输入和输出到达屏幕之前?我想也许是.bashrc中的一些东西,但我是使用bash的新手。解析终端输出/输入的方法? (.bashrc?)
例如:
- 我键入 “ls /家庭/富/酒吧/”
- 这被通过一个脚本,用于替换 '栏' 的所有实例与 '蛋'
- 过去了“ LS /家庭/富/蛋/”被执行
- 输出被发送回替换脚本
- 脚本的输出发送到屏幕
有没有办法在交互式终端中的bash命令解析输入和输出到达屏幕之前?我想也许是.bashrc中的一些东西,但我是使用bash的新手。解析终端输出/输入的方法? (.bashrc?)
例如:
是的。下面是我自己写的东西,用于包装要求文件路径的旧命令行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";
}
}
}
有没有办法像箭头键和历史记录一样使用脚本? – Annan 2009-08-08 15:21:18
不是我所知道的。这些是shell内建的,并且假设shell是交互式运行的(在这种情况下它不是)。如果你需要比这更多的功能,那么你真的在编写你自己的shell - 这不像听起来那么难。见例如http://linuxgazette.net/111/ramankutty.html – 2009-08-08 18:20:20