Term::ShellUI 是CPAN上的一個模組,
用來建立像shell一樣的文字模式人機介面。
多了一層介面,
當然意味著可以隨需要增強或限縮可以做的事情,
也可以紀錄使用者在連線過程中,
所操作的每一個按鍵與時間戳記,
所謂"凡走過必留下痕跡"需求下,
這算是基本必要的功能。
另外,也可以再建立另外一組應用層使用者,
一方面可以
減少系統使用者數量 ,
同時也能夠
簡化作業權限 的管理。
類似的想法,也能夠用web的方式來實現,
端看實作環境與作業特性來選擇。
由使用文件範例稍做修改如下:
use Term::ShellUI;
my $term = new Term::ShellUI(
commands => {
"cd" => {
desc => "Change to directory DIR",
maxargs => 1,
args => sub { shift->complete_onlydirs(@_); },
proc => sub { chdir($_[0] || $ENV{HOME} || $ENV{LOGDIR}); },
},
"chdir" => { alias => 'cd' },
"lsl" => {
desc => "List files in working directory",
maxargs => 0,
proc => sub { system('ls -l'); },
},
"pwd" => {
desc => "Print the current working directory",
maxargs => 0,
proc => sub { system('pwd'); },
},
"help" => {
desc => "Print helpful information",
args => sub { shift->help_args(undef, @_); },
method => sub { shift->help_call(undef, @_); }
},
"exit" => { alias => 'quit' },
"quit" => {
desc => "結束作業", maxargs => 0,
method => sub { shift->exit_requested(1); },
}},
history_file => '~/.shellui-history',
);
print 'Using '.$term->{term}->ReadLine."\n";
$term->prompt('OP-Shell: ');
$term->run();
這個模組的基本用法整理如下:
建立一個command set,這個command set就是使用者可以執行的指令。
command set 基本上是一個以hash為主的資料結構,詳細參見文件說明。
同一個指令可以有別名。
command set可以遞迴,也就是可以有"次指令 sub command"的功能。
支援指令歷程command history,按上下鍵選擇。
支援中文 (要安裝Term::ReadLine::Gnu)
執行影片如下:
經由自動產生的help畫面,
知道可以執行的指令有哪些,
中文顯示沒有問題,
cd切換目錄,經由pwd指令確認沒有問題,
lsl也正常,但是ls如同預期不能執行,
最後,雖然指示quit離開,但因為程式設定exit是quit的別名,
所以打exit或quit都可以離開。
《影片使用
recordmydesktop 錄製,再使用vlc轉成mp4》