if 문 테스트

if 문 테스트


else if/else를 추가하기 전에는 if (/Easy: /) .. 부분 만 있었습니다 .

awk -f oscp-notes.awk를 입력한 다음 &c를 입력하면 프로그램이 시작됩니다.
그런 다음 else if 부분을 추가한 후에 적절한 Easy 시스템을 인쇄합니다.
이제 단순 또는 중간 규모의 컴퓨터에서는 인쇄되지 않습니다. 오류가 발생하지 않습니다. 다시 읽으려면 (파일)을 닫아야 할 것 같아요. 나는 붙어있다. 읽기/닫기가 가능하고 쉬움/보통/어려움/이상함 여부에 관계없이 사용자 입력을 포함하도록 나머지 조건문을 만들 수 있기를 원합니다. 가능합니까?

편집: close("TJnulls.txt") 대신 close(file)도 시도했습니다.


BEGIN { 
    printf "Please select a chapter by entering chapter number: "
    getline entry < "-"
    $2 == entry
    file = "TJnulls.txt"

if 문 테스트

    if (entry == 2) {
        printf "Please select difficulty (easy, medium, hard, insane): "
        getline difficulty < "-"
        $3 == difficulty
        if (difficulty == easy) {
            {print "\n"}
            {print "Obtaining the list of easy machines:\n"}
            while (( getline<file) > 0) {
            if (/Easy: /) {
                {print $2};
            } else {
                close("TJnulls.txt");
                break;
            }
            }
        } else if (difficulty == medium) {
            {print "\n"}
            {print "Obtaining the list of medium machines:\n"}
            while ( ( getline<file) > 0) {
            if (/Medium: /)
                {print $2}
            }
        }
     }
}

편집: 다음 작업을 시도했습니다.
for 루프를 사용하고 for 루프에 if 조건을 추가하고 if 조건을 추가했습니다.

BEGIN {
printf "Please select a chapter by entering chapter number: "
getline entry < "-"
# For loop
i = 0;
{
   for (i=1; i <= 11; i++) {
       if ( ( entry == i ) &&
          ( i == 2 ) )
           file = "TJnulls.txt"
           {print "\nPlease select a difficulty:\n (easy=1, medium=2, hard=3, insane=4, menu=0)\n"}
           getline difficulty < "-"
           x = 0;
           for (x=1; x <= 4; x++) {
               if ( ( difficulty == x ) &&
                  ( x == 1 ) )
                   file = "TJnulls.txt"
                   while ( (getline<file) > 0 ) {
                   if (/Easy: /)
                       {print $2}
                   }
               if ( ( difficulty == i ) &&
                  ( x == 2 ) )
                   file2 = "TJnulls.txt"
                   while ( (getline<file2) > 0 ) {
                   if (/Medium: /)
                       {print $2}
                   }
           }
   }
}
}

문제는 다음과 같은 오류가 발생한다는 것입니다.
18: fatal: `<' 리디렉션에 대한 표현식에 null 문자열 값이 있습니다.
또한 파일 변수(첫 번째 변수)를 삭제하면 나에게 큰 소리가 납니다.

답변1

작동하는 수정 사항을 찾았습니다. 나는 이것이 대괄호 {에 관한 문제라고 생각합니다.

  BEGIN {
  # prompts user
  # user makes selection for chapter by entering a number 1-11
  {print "Please select a chapter by entering a number: "}
  getline entry < "-"

  # initializing f_1
  f_1 = 0;
  for ( f_1 = 1; f_1 <= 11; f_1++) {
      # if conditional statement if user selects chapter 2: TJ nulls list.
      # prompt user to select difficulty of machine.
      # future updates consider making entering text easy and converting into a number in the background
      if ( ( entry == f_1 ) && ( f_1 == 2 ) ) {
          {print "\nPlease select a difficulty: easy=1, medium=2, hard=3, insane=4, return=0"}
          getline difficulty < "-"

          # initializing f_2
          f_2 = 0;
          for ( f_2 = 1; f_2 <= 4; f_2++ ) {
              if ( ( difficulty == f_2 ) && ( f_2 == 1 ) ) {
                  {print "\nObtaining list of easy machines:\n" dash}
                  while ( ( getline<file02 ) >0 ) {
                  if (/Easy: /)
                      {print $2}
                  }
              }
              if ( ( difficulty == f_2 ) && ( f_2 == 2 ) ) {
                  {print "\nObtaining list of medium machines:\n" dash}
                  while ( ( getline<file02 ) >0 ) {
                  if (/Medium: /)
                      {print $2}
                  }
              }
          }
      }
  }

}

관련 정보