![파일의 처음 n 또는 마지막 n 줄을 나열하는 쉘 스크립트](https://linux55.com/image/79561/%ED%8C%8C%EC%9D%BC%EC%9D%98%20%EC%B2%98%EC%9D%8C%20n%20%EB%98%90%EB%8A%94%20%EB%A7%88%EC%A7%80%EB%A7%89%20n%20%EC%A4%84%EC%9D%84%20%EB%82%98%EC%97%B4%ED%95%98%EB%8A%94%20%EC%89%98%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8.png)
지정된 파일의 처음 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) 이 명령으로 무엇을 하려는지 모르겠지만 , 이것이 무엇이든 간에 이는 확실히 올바른 접근 방식이 아닙니다. ;)
확인하다울 엣지 배쉬 튜토리얼더 알아보기.