ファイルの最初のn行または最後のn行をリストするシェルスクリプト

ファイルの最初のn行または最後のn行をリストするシェルスクリプト

指定したファイルの最初のn行または最後のn行をリストするスクリプトを作成したいと思います。

cd
        $1=filename
        $2=string
        $3=lenght

if [ "$filename" == "head" ]

#If the user uses the head command then do the following.

then [ "$filename" == "tail" ]
        head -n 10 /MyDirectoryGoesHere
else
#If the user uses the tail command do this instead.

        tail -n 10 /MyDirectoryGoesHere
fi

このコマンドを実行すると、「予期しないトークンを閉じる別のエラー」が発生し続け、forループを追加するように求められますが、方法と場所がわかりません。ご協力ありがとうございます。

ベストアンサー1

3つ:

(1) if-then-else-fi 構造は次のようになります。

if [ "$myvar" = "value" ]; then
  # do stuff
elif [ "$myvar" = "othervalue" ]; then
  # do other stuff
else
  # do still other stuff
fi

(2) ケーススイッチを使用できます。

case "$myvar" in
  "value")
    # do stuff
    ;;
  "othervalue")
    # do other stuff
    ;;
  *)
    # do still other stuff
    ;;
esac

$1=filename(3)このコマンドで何をしたいのかわかりませんが、これが何であれ、これは確かに正しいアプローチではありません。 ;)

確認するウールエッジバッシュチュートリアルもっと学ぶ。

おすすめ記事