2 need to stop on getline returning < 0 as well.
source | link

If that fits your workflow, you can make awk itself open the pipe. You can't do that for an ARGV element, so you won't get awk's automatic iteration on the lines read from the pipe. Instead awk will read from the files passed as command line arguments, or from standard input if there were no file arguments.

{
    printf "Enter input: "
    getline var
    while (("cat file.txt" | getline) > 0) { … }
}

This structure doesn't fit your toy example well, but I'm mentioning it because it might fit your real problem.

If that fits your workflow, you can make awk itself open the pipe. You can't do that for an ARGV element, so you won't get awk's automatic iteration on the lines read from the pipe. Instead awk will read from the files passed as command line arguments, or from standard input if there were no file arguments.

{
    printf "Enter input: "
    getline var
    while ("cat file.txt" | getline) { … }
}

This structure doesn't fit your toy example well, but I'm mentioning it because it might fit your real problem.

If that fits your workflow, you can make awk itself open the pipe. You can't do that for an ARGV element, so you won't get awk's automatic iteration on the lines read from the pipe. Instead awk will read from the files passed as command line arguments, or from standard input if there were no file arguments.

{
    printf "Enter input: "
    getline var
    while (("cat file.txt" | getline) > 0) { … }
}

This structure doesn't fit your toy example well, but I'm mentioning it because it might fit your real problem.

1
source | link

If that fits your workflow, you can make awk itself open the pipe. You can't do that for an ARGV element, so you won't get awk's automatic iteration on the lines read from the pipe. Instead awk will read from the files passed as command line arguments, or from standard input if there were no file arguments.

{
    printf "Enter input: "
    getline var
    while ("cat file.txt" | getline) { … }
}

This structure doesn't fit your toy example well, but I'm mentioning it because it might fit your real problem.