다음 secret 폴더의 파일 목록에 대해 입력, 출력 결과를 검증해주는 쉘스크립트를 작성해주세요. [code] 프로그램은 test.cpp이고, test로…
- User
다음 secret 폴더의 파일 목록에 대해 입력, 출력 결과를 검증해주는 쉘스크립트를 작성해주세요.
01-meow.ans 40-medium_2.in 01-meow.in 41-medium_2.ans 02-z.ans 41-medium_2.in 02-z.in 42-medium_2.ans 03-o.ans 42-medium_2.in 03-o.in 43-medium_2.ans 04-long_meomeomeow.ans 43-medium_2.in 04-long_meomeomeow.in 44-medium_2.ans 05-small_0.ans 44-medium_2.in 05-small_0.in 45-medium_2.ans 06-small_1.ans 45-medium_2.in 06-small_1.in 46-medium_2.ans 07-small_1.ans 46-medium_2.in 07-small_1.in 47-medium_2.ans 08-small_1.ans 47-medium_2.in 08-small_1.in 48-medium_2.ans 09-small_1.ans 48-medium_2.in 09-small_1.in 49-medium_3.ans 10-small_1.ans 49-medium_3.in 10-small_1.in 50-medium_3.ans 11-small_1.ans 50-medium_3.in 11-small_1.in 51-medium_3.ans 12-small_1.ans 51-medium_3.in 12-small_1.in 52-medium_3.ans 13-small_1.ans 52-medium_3.in 13-small_1.in 53-medium_4.ans 14-small_2.ans 53-medium_4.in 14-small_2.in 54-medium_random.ans 15-small_2.ans 54-medium_random.in 15-small_2.in 55-big_0.ans 16-small_2.ans 55-big_0.in 16-small_2.in 56-big_1.ans 17-small_2.ans 56-big_1.in 17-small_2.in 57-big_1.ans 18-small_2.ans 57-big_1.in 18-small_2.in 58-big_1.ans 19-small_2.ans 58-big_1.in 19-small_2.in 59-big_1.ans 20-small_2.ans 59-big_1.in 20-small_2.in 60-big_1.ans 21-small_2.ans 60-big_1.in 21-small_2.in 61-big_1.ans 22-small_2.ans 61-big_1.in 22-small_2.in 62-big_1.ans 23-small_2.ans 62-big_1.in 23-small_2.in 63-big_1.ans 24-small_3.ans 63-big_1.in 24-small_3.in 64-big_2.ans 25-small_3.ans 64-big_2.in 25-small_3.in 65-big_2.ans 26-small_3.ans 65-big_2.in 26-small_3.in 66-big_2.ans 27-small_3.ans 66-big_2.in 27-small_3.in 67-big_2.ans 28-small_4.ans 67-big_2.in 28-small_4.in 68-big_2.ans 29-small_random.ans 68-big_2.in 29-small_random.in 69-big_2.ans 30-medium_0.ans 69-big_2.in 30-medium_0.in 70-big_2.ans 31-medium_1.ans 70-big_2.in 31-medium_1.in 71-big_2.ans 32-medium_1.ans 71-big_2.in 32-medium_1.in 72-big_2.ans 33-medium_1.ans 72-big_2.in 33-medium_1.in 73-big_2.ans 34-medium_1.ans 73-big_2.in 34-medium_1.in 74-big_3.ans 35-medium_1.ans 74-big_3.in 35-medium_1.in 75-big_3.ans 36-medium_1.ans 75-big_3.in 36-medium_1.in 76-big_3.ans 37-medium_1.ans 76-big_3.in 37-medium_1.in 77-big_3.ans 38-medium_1.ans 77-big_3.in 38-medium_1.in 78-big_4.ans 39-medium_2.ans 78-big_4.in 39-medium_2.in 79-big_random.ans 40-medium_2.ans 79-big_random.in프로그램은 test.cpp이고, test로 컴파일됩니다.
- Assistant
아래는 주어진 파일 목록에 대해 입력 파일(
.in)과 출력 파일(.ans)을 검증하는 쉘 스크립트 예제입니다. 이 스크립트는test.cpp를test로 컴파일한 후, 입력 파일을 실행하고 그 결과를 예상 출력 파일과 비교합니다.먼저,
test.cpp를 컴파일합니다:g++ -o test test.cpp아래는 파일 목록에 대해 작업하는 쉘 스크립트입니다:
#!/bin/bash # 컴파일된 프로그램의 이름 EXECUTABLE=./test # secret 폴더로 이동 cd secret # 파일 이름 패턴 지정 input_pattern="*.in" timeout_duration=5 # 모든 .in 파일 순회 for input_file in $input_pattern do # 각 입력 파일에 대응하는 기대 출력 파일 expected_output_file="${input_file%.in}.ans" # 일치하는 출력 파일이 있는지 확인 if [ ! -f "$expected_output_file" ]; then echo "찾을 수 없는 출력 파일: $expected_output_file" continue fi # 프로그램 실행 후 출력과 기대 출력 비교 output=$(timeout $timeout_duration $EXECUTABLE < $input_file) expected_output=$(cat $expected_output_file) if [ "$output" == "$expected_output" ]; then echo "[통과] $input_file" else echo "[실패] $input_file (정답과 일치하지 않음)" fi done위의 스크립트는 다음을 수행합니다:
- User
timeout: failed to run command ‘./test’: No such file or directory
- Assistant
해당 오류 메시지는
timeout명령이./test실행 파일을 찾을 수 없어서 발생합니다. 이를 해결할 수 있는 방법은 두 가지 가능성이 있습니다:-
컴파일 오류 확인:
test실행 파일이 제대로 컴파일되지 않았을 가능성이 있습니다. 먼저 컴파일 명령어가 올바르게 작동했는지 확인하세요. 컴파일 단계에서 오류가 발생하지 않았는지 확인하려면 컴파일할 때 나온 메시지를 점검하세요:g++ -o test test.cpp위 명령어를 실행했을 때 오류가 발생하지 않았는지 확인합니다. 만약 오류가 있다면 이를 수정해야 합니다.
-
경로 문제 해결:
./test가 스크립트가 있는 곳(즉, 스크립트를 실행하는 현재 디렉토리)에 없거나cd secret명령어로 인해 디렉토리를 변경해서./test를 못 찾는 경우입니다. 이를 해결하려면:- 컴파일된
test파일이 실제로secret폴더에 있는지 확인하세요. 만약 현재 디렉토리에 있다면, 다음처럼 복사하여 사용하거나 아래와 같이 절대 경로를 사용하여 스크립트를 작성할 수 있습니다.
- 컴파일된
-
- User
실행 파일은 secret 폴더에 있지 않습니다. 스크립트를 수정해주세요.
- Assistant
test실행 파일이secret폴더 밖에 존재한다면, 스크립트에서secret폴더로 이동하지 말고, 대신secret폴더 내의 파일을 직접 참조하도록 수정하겠습니다. 이렇게 하면 실행 파일이secret폴더 밖에 있더라도 문제가 발생하지 않습니다.아래는 수정된 스크립트입니다:
#!/bin/bash # 컴파일된 프로그램의 절대 경로 또는 현재 디렉토리에서의 상대 경로 EXECUTABLE="./test" # secret 폴더 경로 SECRET_DIR="secret" # 파일 이름 패턴 지정 input_pattern="*.in" timeout_duration=5 # secret 폴더 내 모든 .in 파일 순회 for input_file in "$SECRET_DIR"/*.in do # 각 입력 파일에 대응하는 기대 출력 파일 expected_output_file="${input_file%.in}.ans" # 예상 출력 파일이 있는지 확인 if [ ! -f "$expected_output_file" ]; then echo "찾을 수 없는 출력 파일: $expected_output_file" continue fi # 프로그램 실행 후 출력과 기대 출력 비교 output=$(timeout $timeout_duration $EXECUTABLE < "$input_file") expected_output=$(cat "$expected_output_file") if [ "$output" == "$expected_output" ]; then echo "[통과] $input_file" else echo "[실패] $input_file (정답과 일치하지 않음)" fi done이 스크립트는 다음을 수행합니다:
secret폴더에 있는.in파일을 직접 참조합니다.- 각
.in파일에 대해 예상 파일을 체크합니다.
- User
정답과 일치하지 않는 경우 출력 결과를 비교하여 보여주세요.
- Assistant
정답과 일치하지 않는 경우 출력 내용을 비교할 수 있도록 스크립트를 수정하겠습니다. 이를 위해
diff명령을 사용하여 실제 출력과 예상 출력의 차이를 보여줄 수 있습니다. 수정된 스크립트는 다음과 같습니다:#!/bin/bash # 컴파일된 프로그램의 절대 경로 또는 현재 디렉토리에서의 상대 경로 EXECUTABLE="./test" # secret 폴더 경로 SECRET_DIR="secret" # 파일 이름 패턴 지정 input_pattern="*.in" timeout_duration=5 # secret 폴더 내 모든 .in 파일 순회 for input_file in "$SECRET_DIR"/*.in do # 각 입력 파일에 대응하는 기대 출력 파일 expected_output_file="${input_file%.in}.ans" # 예상 출력 파일이 있는지 확인 if [ ! -f "$expected_output_file" ]; then echo "찾을 수 없는 출력 파일: $expected_output_file" continue fi # 프로그램 실행 후 출력과 기대 출력 비교 output=$(timeout $timeout_duration $EXECUTABLE < "$input_file") expected_output=$(cat "$expected_output_file") if [ "$output" == "$expected_output" ]; then echo "[통과] $input_file" else echo "[실패] $input_file (정답과 일치하지 않음)" # 실제 출력과 기대 출력의 차이를 비교 diff <(echo "$output") <(echo "$expected_output") fi done이 스크립트는 다음을 수행합니다:
- 각각의 입력 파일에 대해 프로그램을 실행하고, 그 출력을 변수
output에 저장합니다. - 예상 출력 파일의 내용을
expected_output에 저장합니다.
- 각각의 입력 파일에 대해 프로그램을 실행하고, 그 출력을 변수