「more」コマンドを作成する
「out-host」Cmdletを利用してUNIXシェルの「more」相当の処理を作成してみます。
「more」の挙動
「more」コマンドは以下の2系統の情報をページング表示します。
- パイプで渡した情報
- 引数として渡したファイルの内容
パイプで渡した情報のページング表示(UNIX)
ls | more
引数として渡したファイルの内容のページング表示(UNIX)
more ./process.txt
「out-host」を利用して「more」を実現する
以下を「profile.msh」に記述しておきます。
function more { $input | out-host -paging if($args[0] -ne $null) { get-content $args[0] | out-host -paging } }
使い方は以下です。
パイプで渡した情報のページング表示(MSH)
MSH C:\> get-childitem | more Directory: FileSystem::C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 2003/08/06 9:50 0 AUTOEXEC.BAT -a--- 2003/08/06 9:50 0 CONFIG.SYS d---- 2005/05/04 22:26 App (中略) <SPACE> next page; <CR> next line; Q quit
引数として渡したファイルの内容のページング表示(MSH)
MSH C:\> more process.txt Handles NPM(K) PM(K) WS(K) VS(M) CPU(s) Id ProcessName ------- ------ ----- ----- ----- ------ -- ----------- 786 48 14776 22764 183 14.80 468 CCAPP 319 7 4404 2876 62 2.57 2292 CCEVTMGR (中略) <SPACE> next page; <CR> next line; Q quit
補足
- パイプと引数を同時に渡すと、両方処理してしまう点が課題です
- 今回、$inputをnullチェックして分岐しようと思ったのですが、$inputは$nullにならないようです
- 「more xxx.txt」のようなパイプがない状態でも$nullになりません。
- 「$input.gettype().fullname」で$inputの型を調べると「System.Array+SZArrayEnumerator」でした。
- 「System.Array+SZArrayEnumerator」について調べる必要がありそうです。