2009-08-08 21 views
0

対話型端末のbashコマンドからの入出力を解析して画面に到達する方法はありますか?私はおそらく.bashrcで何かを考えていましたが、私はbashを使うのが初めてです。例えば端末の出力/入力を解析する方法は? (.bashrc?)

  • 私は '卵'
  • "と 'バー' のすべてのインスタンスを置き換えるスクリプトを渡される "LS /ホーム/ fooの/バー/"
  • を入力LS /ホーム/ fooの/卵/」出力は置き換えるスクリプト
  • スクリプトの出力に送り返されます
  • 画面
に送信されますが実行されます

答えて

2

はい。ここに私が自分の使い方で書いた何かが、ファイルパスを要求する古いコマンドライン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"; 
     } 
    } 
} 
+0

矢印キーや履歴などのスクリプトを使用する方法はありますか? – Annan

+0

私が知っているわけではありません。これらはシェル組み込み関数であり、シェルが対話的に実行されていると想定しています(この場合はそうではありません)。これ以上の機能を望むなら、あなたは自分のシェルを書くことを本当に見ています。それはそれほど難しくありません。例えば、 http://linuxgazette.net/111/ramankutty.html –

関連する問題