测试 if 语句

测试 if 语句

在我添加 else if/else 之前,当我只有:
if (/Easy: /) .. 部分时

当我输入 awk -f oscp-notes.awk 然后输入输入 &c 时,该程序将启动。
然后它会打印出适当的 Easy 机器;但是,在我添加 else if 部分之后。
它现在不会打印简单或中型机器。我没有任何错误。我认为我需要关闭(文件)以便我可以重新阅读。我被困住了。我希望能够读取/关闭并制作其余的条件语句,以包含用户输入,无论它是简单/中等/困难/疯狂。这可能吗?

编辑:我也尝试过 close(file) 而不是 close("TJnulls.txt")。


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 循环,将 if 条件添加到 for 循环和 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: expression for `<' redirection has null string value
另外,当我删除文件变量(第一个)时,它会对我咆哮。

答案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}
                  }
              }
          }
      }
  }

}

相关内容