파일의 처음 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

세개:

(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) 이 명령으로 무엇을 하려는지 모르겠지만 , 이것이 무엇이든 간에 이는 확실히 올바른 접근 방식이 아닙니다. ;)

확인하다울 엣지 배쉬 튜토리얼더 알아보기.

관련 정보