頭と尾の命令

頭と尾の命令

Unixでこの練習をするにはどうすればよいですか?次のコマンドを作成します。 lshead.bash – 引数で指定されたディレクトリ内の各ファイルの最初の数行をリストします。このコマンドは、ファイルの最初のn行または最後のn行をリストするオプションも許可する必要があります。コマンド例:lshead.bash -head 10 Documentsは、ドキュメントディレクトリ内の各ファイルの最初の10行を一覧表示します。

ベストアンサー1

このコードは基本的なアイデアを提供します。必要に応じていくつかの機能を直接追加できます。

編集する

#!/bin/bash
cur_dir=`pwd`
function head_file()
{
    ls -p $cur_dir | grep -v / | while read file;do
        echo "First $1 lines of the FILE: $file"
        cat $file | head -n+$1 # Displaying the First n lines
        echo "*****************END OF THE FILE: $file**************"
    done
}

function tail_file()
{
    ls -p $cur_dir | grep -v / | while read file;do
        echo "Last $1 lines of the FILE: $file"
        cat $file | tail -n+$1 # Displaying the last n lines
        echo "**************END OF THE FILE: $file******************"
    done
}


case "$1" in
  head)
        head_file $2
        ;;
  tail)
        tail_file $2
        ;;

    *)
        echo "Invalid Option :-("
        exit 1
esac
exit 0

おすすめ記事