![스크립트가 명령줄 입력을 허용하지 않습니까? 위치 매개변수 도움말[닫기]](https://linux55.com/image/114329/%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EA%B0%80%20%EB%AA%85%EB%A0%B9%EC%A4%84%20%EC%9E%85%EB%A0%A5%EC%9D%84%20%ED%97%88%EC%9A%A9%ED%95%98%EC%A7%80%20%EC%95%8A%EC%8A%B5%EB%8B%88%EA%B9%8C%3F%20%EC%9C%84%EC%B9%98%20%EB%A7%A4%EA%B0%9C%EB%B3%80%EC%88%98%20%EB%8F%84%EC%9B%80%EB%A7%90%5B%EB%8B%AB%EA%B8%B0%5D.png)
안녕하세요. 파일 이름과 선택적으로 출력할 줄 수를 읽는 간단한 파일을 만들려고 하는데, 파일 이름을 유지하기 위해 할당한 위치 인수를 스크립트에서 허용할 수 없습니다.
내 코드:
#
# task_4-1.sh filename [howmany] - optional parameter
#
#
#
# Read a file containing the names of and number of albums of artist in the album collection.
# Program will print the N highest lines of Artist with the highest Album count int he collection
# Default N is 10
# One arguement for the name of the input file and a second optional line for how many lines to print.
#!\bin\bash
#Variable to accept the filename will print an error message if this value is left null
filename=${1:?"missing."}
#Optional parameter that controls how many lines to Output to shell
howmany=${2:-10}
sort -nr $filename | head $howmany
명령줄에서 코드를 실행하려고 하면 다음과 같습니다.
task_4-1.sh albumnumber.txt 10
오류 헤더가 표시됩니다. 10 해당 파일이나 디렉터리가 없습니다. 이 대괄호 구문에 매개변수를 할당하면 다음과 같습니다.
filename=${filename:?"missing."}
그런 다음 파일 이름을 지정했습니다: "missing"이라는 오류 메시지가 나타납니다.
내가 여기서 뭘 잘못하고 있는지 잘 모르겠습니다.
답변1
유일한 문제는 마지막 줄에 "-n" 옵션이 없다는 것입니다. 이는 예상대로 작동합니다.
sort -nr $filename | head -n $howmany
답변2
이유와bashBedlam의 답변하지만 코드가 다릅니다.( -n
2바이트를 절약하는 옵션이 필요하지 않음) 다음 줄을 변경하면 됩니다.
howmany=${2:-10}
이와 관련하여:
howmany=${2:--10}